1 16 17 package org.apache.commons.dbcp; 18 19 import java.io.PrintWriter ; 20 import java.sql.CallableStatement ; 21 import java.sql.Connection ; 22 import java.sql.DatabaseMetaData ; 23 import java.sql.PreparedStatement ; 24 import java.sql.SQLException ; 25 import java.sql.SQLWarning ; 26 import java.sql.Statement ; 27 import java.util.Map ; 28 import java.util.NoSuchElementException ; 29 30 import javax.sql.DataSource ; 31 32 import org.apache.commons.pool.ObjectPool; 33 34 44 public class PoolingDataSource implements DataSource { 45 46 47 private boolean accessToUnderlyingConnectionAllowed = false; 48 49 public PoolingDataSource() { 50 this(null); 51 } 52 53 public PoolingDataSource(ObjectPool pool) { 54 _pool = pool; 55 } 56 57 public void setPool(ObjectPool pool) throws IllegalStateException , NullPointerException { 58 if(null != _pool) { 59 throw new IllegalStateException ("Pool already set"); 60 } else if(null == pool) { 61 throw new NullPointerException ("Pool must not be null."); 62 } else { 63 _pool = pool; 64 } 65 } 66 67 72 public boolean isAccessToUnderlyingConnectionAllowed() { 73 return this.accessToUnderlyingConnectionAllowed; 74 } 75 76 83 public void setAccessToUnderlyingConnectionAllowed(boolean allow) { 84 this.accessToUnderlyingConnectionAllowed = allow; 85 } 86 87 89 93 public Connection getConnection() throws SQLException { 94 try { 95 Connection conn = (Connection )(_pool.borrowObject()); 96 if (conn != null) { 97 conn = new PoolGuardConnectionWrapper(conn); 98 } 99 return conn; 100 } catch(SQLException e) { 101 throw e; 102 } catch(NoSuchElementException e) { 103 throw new SQLNestedException("Cannot get a connection, pool exhausted", e); 104 } catch(RuntimeException e) { 105 throw e; 106 } catch(Exception e) { 107 throw new SQLNestedException("Cannot get a connection, general error", e); 108 } 109 } 110 111 115 public Connection getConnection(String uname, String passwd) throws SQLException { 116 throw new UnsupportedOperationException (); 117 } 118 119 124 public PrintWriter getLogWriter() { 125 return _logWriter; 126 } 127 128 133 public int getLoginTimeout() { 134 throw new UnsupportedOperationException (); 135 } 136 137 142 public void setLoginTimeout(int seconds) { 143 throw new UnsupportedOperationException (); 144 } 145 146 150 public void setLogWriter(PrintWriter out) { 151 _logWriter = out; 152 } 153 154 155 protected PrintWriter _logWriter = null; 156 157 protected ObjectPool _pool = null; 158 159 163 private class PoolGuardConnectionWrapper extends DelegatingConnection { 164 165 private Connection delegate; 166 167 PoolGuardConnectionWrapper(Connection delegate) { 168 super(delegate); 169 this.delegate = delegate; 170 } 171 172 protected void checkOpen() throws SQLException { 173 if(delegate == null) { 174 throw new SQLException ("Connection is closed."); 175 } 176 } 177 178 public void close() throws SQLException { 179 checkOpen(); 180 this.delegate.close(); 181 this.delegate = null; 182 super.setDelegate(null); 183 } 184 185 public boolean isClosed() throws SQLException { 186 if (delegate == null) { 187 return true; 188 } 189 return delegate.isClosed(); 190 } 191 192 public void clearWarnings() throws SQLException { 193 checkOpen(); 194 delegate.clearWarnings(); 195 } 196 197 public void commit() throws SQLException { 198 checkOpen(); 199 delegate.commit(); 200 } 201 202 public Statement createStatement() throws SQLException { 203 checkOpen(); 204 return delegate.createStatement(); 205 } 206 207 public Statement createStatement(int resultSetType, int resultSetConcurrency) throws SQLException { 208 checkOpen(); 209 return delegate.createStatement(resultSetType, resultSetConcurrency); 210 } 211 212 public boolean equals(Object obj) { 213 if (delegate == null){ 214 return false; 215 } 216 return delegate.equals(obj); 217 } 218 219 public boolean getAutoCommit() throws SQLException { 220 checkOpen(); 221 return delegate.getAutoCommit(); 222 } 223 224 public String getCatalog() throws SQLException { 225 checkOpen(); 226 return delegate.getCatalog(); 227 } 228 229 public DatabaseMetaData getMetaData() throws SQLException { 230 checkOpen(); 231 return delegate.getMetaData(); 232 } 233 234 public int getTransactionIsolation() throws SQLException { 235 checkOpen(); 236 return delegate.getTransactionIsolation(); 237 } 238 239 public Map getTypeMap() throws SQLException { 240 checkOpen(); 241 return delegate.getTypeMap(); 242 } 243 244 public SQLWarning getWarnings() throws SQLException { 245 checkOpen(); 246 return delegate.getWarnings(); 247 } 248 249 public int hashCode() { 250 if (delegate == null){ 251 return 0; 252 } 253 return delegate.hashCode(); 254 } 255 256 public boolean isReadOnly() throws SQLException { 257 checkOpen(); 258 return delegate.isReadOnly(); 259 } 260 261 public String nativeSQL(String sql) throws SQLException { 262 checkOpen(); 263 return delegate.nativeSQL(sql); 264 } 265 266 public CallableStatement prepareCall(String sql) throws SQLException { 267 checkOpen(); 268 return delegate.prepareCall(sql); 269 } 270 271 public CallableStatement prepareCall(String sql, int resultSetType, int resultSetConcurrency) throws SQLException { 272 checkOpen(); 273 return delegate.prepareCall(sql, resultSetType, resultSetConcurrency); 274 } 275 276 public PreparedStatement prepareStatement(String sql) throws SQLException { 277 checkOpen(); 278 return delegate.prepareStatement(sql); 279 } 280 281 public PreparedStatement prepareStatement(String sql, int resultSetType, int resultSetConcurrency) throws SQLException { 282 checkOpen(); 283 return delegate.prepareStatement(sql, resultSetType, resultSetConcurrency); 284 } 285 286 public void rollback() throws SQLException { 287 checkOpen(); 288 delegate.rollback(); 289 } 290 291 public void setAutoCommit(boolean autoCommit) throws SQLException { 292 checkOpen(); 293 delegate.setAutoCommit(autoCommit); 294 } 295 296 public void setCatalog(String catalog) throws SQLException { 297 checkOpen(); 298 delegate.setCatalog(catalog); 299 } 300 301 public void setReadOnly(boolean readOnly) throws SQLException { 302 checkOpen(); 303 delegate.setReadOnly(readOnly); 304 } 305 306 public void setTransactionIsolation(int level) throws SQLException { 307 checkOpen(); 308 delegate.setTransactionIsolation(level); 309 } 310 311 public void setTypeMap(Map map) throws SQLException { 312 checkOpen(); 313 delegate.setTypeMap(map); 314 } 315 316 public String toString() { 317 if (delegate == null){ 318 return null; 319 } 320 return delegate.toString(); 321 } 322 323 326 327 328 public int getHoldability() throws SQLException { 329 checkOpen(); 330 return delegate.getHoldability(); 331 } 332 333 public void setHoldability(int holdability) throws SQLException { 334 checkOpen(); 335 delegate.setHoldability(holdability); 336 } 337 338 public java.sql.Savepoint setSavepoint() throws SQLException { 339 checkOpen(); 340 return delegate.setSavepoint(); 341 } 342 343 public java.sql.Savepoint setSavepoint(String name) throws SQLException { 344 checkOpen(); 345 return delegate.setSavepoint(name); 346 } 347 348 public void releaseSavepoint(java.sql.Savepoint savepoint) throws SQLException { 349 checkOpen(); 350 delegate.releaseSavepoint(savepoint); 351 } 352 353 public void rollback(java.sql.Savepoint savepoint) throws SQLException { 354 checkOpen(); 355 delegate.rollback(savepoint); 356 } 357 358 public Statement createStatement(int resultSetType, int resultSetConcurrency, int resultSetHoldability) throws SQLException { 359 checkOpen(); 360 return delegate.createStatement(resultSetType, resultSetConcurrency, resultSetHoldability); 361 } 362 363 public CallableStatement prepareCall(String sql, int resultSetType, int resultSetConcurrency, int resultSetHoldability) throws SQLException { 364 checkOpen(); 365 return delegate.prepareCall(sql, resultSetType, resultSetConcurrency, resultSetHoldability); 366 } 367 368 public PreparedStatement prepareStatement(String sql, int autoGeneratedKeys) throws SQLException { 369 checkOpen(); 370 return delegate.prepareStatement(sql, autoGeneratedKeys); 371 } 372 373 public PreparedStatement prepareStatement(String sql, int resultSetType, int resultSetConcurrency, int resultSetHoldability) throws SQLException { 374 checkOpen(); 375 return delegate.prepareStatement(sql, resultSetType, resultSetConcurrency, resultSetHoldability); 376 } 377 378 public PreparedStatement prepareStatement(String sql, int[] columnIndexes) throws SQLException { 379 checkOpen(); 380 return delegate.prepareStatement(sql, columnIndexes); 381 } 382 383 public PreparedStatement prepareStatement(String sql, String [] columnNames) throws SQLException { 384 checkOpen(); 385 return delegate.prepareStatement(sql, columnNames); 386 } 387 388 389 390 393 public Connection getDelegate() { 394 if (isAccessToUnderlyingConnectionAllowed()) { 395 return super.getDelegate(); 396 } else { 397 return null; 398 } 399 } 400 401 404 public Connection getInnermostDelegate() { 405 if (isAccessToUnderlyingConnectionAllowed()) { 406 return super.getInnermostDelegate(); 407 } else { 408 return null; 409 } 410 } 411 } 412 } 413 | Popular Tags |