1 22 package org.enhydra.jdbc.pool; 23 24 import java.io.PrintWriter ; 25 import java.sql.Connection ; 26 import java.sql.SQLException ; 27 import java.sql.Statement ; 28 import java.util.Hashtable ; 29 30 import javax.naming.Context ; 31 import javax.naming.InitialContext ; 32 import javax.naming.Name ; 33 import javax.naming.NamingException ; 34 import javax.naming.Reference ; 35 import javax.naming.StringRefAddr ; 36 import javax.sql.ConnectionEvent ; 37 import javax.sql.ConnectionEventListener ; 38 import javax.sql.ConnectionPoolDataSource ; 39 import javax.sql.DataSource ; 40 import javax.sql.PooledConnection ; 41 import org.enhydra.jdbc.core.CoreDataSource; 42 import org.enhydra.jdbc.core.JdbcThreadFactory; 43 import org.enhydra.jdbc.util.Logger; 44 import org.apache.commons.logging.Log; 45 import org.apache.commons.logging.LogFactory; 46 47 56 public class StandardPoolDataSource 57 extends CoreDataSource 58 implements DataSource , PoolHelper, ConnectionEventListener { 59 60 public ConnectionPoolDataSource cpds; public GenericPool pool; public String dataSourceName; public String jdbcTestStmt; 64 public boolean onOff; public Context ictx; public Log glog = LogFactory.getLog("org.enhydra.jdbc.xapool"); 68 69 72 public StandardPoolDataSource() { 73 log = new Logger(glog); 74 super.setLogWriter(log); 75 76 pool = new GenericPool(this); 77 pool.setLogger(log); 79 onOff = false; 80 dataSourceName = null; 81 } 82 83 86 public StandardPoolDataSource(int initSize) { log = new Logger(glog); 88 super.setLogWriter(log); 89 90 pool = new GenericPool(this, initSize); 91 pool.setLogger(log); 93 onOff = false; 94 dataSourceName = null; 95 } 96 97 100 public StandardPoolDataSource(ConnectionPoolDataSource cc) { 101 cpds = cc; 102 log = new Logger(glog); 103 104 super.setLogWriter(log); 105 pool = new GenericPool(this); 106 pool.setLogger(log); 108 try { 109 cpds.setLogWriter(log); 110 } catch (SQLException sqle) { 111 112 } 113 114 onOff = false; 115 dataSourceName = null; 116 } 117 118 121 public StandardPoolDataSource( 122 ConnectionPoolDataSource cc, 123 int initSize) { cpds = cc; 125 log = new Logger(glog); 126 super.setLogWriter(log); 127 128 pool = new GenericPool(this, initSize); 129 pool.setLogger(log); 131 132 onOff = false; 133 dataSourceName = null; 134 } 135 136 142 public void setDataSourceName(String dataSourceName) { 143 log.debug("StandardPoolDataSource:setDataSourceName"); 144 this.dataSourceName = dataSourceName; 153 } 154 155 public String getDataSourceName() { 156 return dataSourceName; } 158 159 163 public Connection getConnection() throws SQLException { 164 return getConnection(getUser(), getPassword()); 165 } 166 167 171 public Connection getConnection(String _user, String _password) 172 throws SQLException { 173 log.debug("StandardPoolDataSource:getConnection"); 174 Connection ret = null; 175 PooledConnection con = null; 176 177 synchronized (this) { 178 if (!onOff) { 179 log.debug( 180 "StandardPoolDataSource:getConnection must configure the pool..."); 181 pool.start(); onOff = true; log.debug( 184 "StandardPoolDataSource:getConnection pool config : \n" 185 + pool.toString()); 186 } 187 } 188 189 try { 190 try { 191 log.debug( 192 "StandardPoolDataSource:getConnection Try to give a " 193 + "connection (checkOut)"); 194 con = (PooledConnection ) pool.checkOut(_user, _password); 195 log.debug( 197 "StandardPoolDataSource:getConnection checkOut return" 198 + "a new connection"); 199 } catch (Exception e) { 200 e.printStackTrace(); 201 log.debug( 202 "StandardPoolDataSource:getConnection SQLException in StandardPoolDataSource:getConnection" 203 + e); 204 throw new SQLException ( 205 "SQLException in StandardPoolDataSource:getConnection no connection available " 206 + e); 207 } 208 209 ret = con.getConnection(); 210 } catch (Exception e) { 211 log.debug("StandardPoolDataSource:getConnection exception" + e); 212 e.printStackTrace(); 213 SQLException sqle = 214 new SQLException ( 215 "SQLException in StandardPoolDataSource:getConnection exception: " 216 + e); 217 if (e instanceof SQLException ) 218 sqle.setNextException((SQLException ) e); 219 if (con != null) { 220 pool.checkIn(con); 221 } 222 throw sqle; 223 } 224 log.debug("StandardPoolDataSource:getConnection return a connection"); 225 return ret; 226 } 227 228 235 public void connectionErrorOccurred(ConnectionEvent event) { 236 Object obj = event.getSource(); 237 PooledConnection pc = (PooledConnection ) obj; 238 pool.nextGeneration(pc); 239 pool.removeLockedObject(pc); expire(pc); log.debug( 242 "StandardXAPoolDataSource:connectionErrorOccurred remove the object from the pool"); 243 } 244 245 249 public void connectionClosed(ConnectionEvent event) { 250 log.debug( 251 "StandardPoolDataSource:connectionClosed close the connection"); 252 Object obj = event.getSource(); 253 pool.checkIn(obj); 254 } 255 256 259 public void expire(Object o) { 260 log.debug( 261 "StandardPoolDataSource:expire expire a connection, remove from the pool"); 262 if (o == null) 263 return; 264 try { 265 PooledConnection pooledCon = (PooledConnection ) o; 266 pooledCon.close(); pooledCon.removeConnectionEventListener(this); 268 log.debug("StandardPoolDataSource:expire close the connection"); 269 } catch (java.sql.SQLException e) { 270 log.error( 271 "StandardPoolDataSource:expire Error java.sql.SQLException in StandardPoolDataSource:expire"); 272 } 273 } 274 275 278 public boolean checkThisObject(Object o) { 279 280 PooledConnection con; 281 Connection ret; 282 log.debug( 283 "StandardPoolDataSource:checkThisObject verify the current object"); 284 try { 285 con = (PooledConnection ) o; 286 ret = con.getConnection(); if (ret.isClosed()) { 288 return false; 289 } 290 try { 291 ret.close(); 292 } catch (Exception e) { 293 log.error( 294 "StandardPoolDataSource:checkThisObject can't closed the connection: " 295 + e); 296 } 297 298 return true; 299 } catch (java.sql.SQLException e) { 300 log.error( 301 "StandardPoolDataSource:checkThisObject Error java.sql.SQLException in StandardPoolDataSource:checkThisObject"); 302 return false; 303 } 304 } 305 306 309 public boolean testThisObject(Object o) { 310 Connection ret = null; 311 log.debug( 312 "StandardPoolDataSource:testThisObject verify the current object"); 313 try { 314 PooledConnection con = (PooledConnection ) o; 315 ret = con.getConnection(); 316 Statement s = ret.createStatement(); 317 s.execute(jdbcTestStmt); 318 s.close(); 319 try { 320 ret.close(); 321 } catch (Exception e) { 322 log.error( 323 "StandardPoolDataSource:checkThisObject can't closed the connection: " 324 + e); 325 } 326 return true; 327 } catch (java.sql.SQLException e) { 328 log.error( 329 "StandardPoolDataSource:checkThisObject Error java.sql.SQLException in StandardPoolDataSource:testThisObject"); 330 return false; 331 } 332 } 333 334 public GenerationObject create() throws SQLException { 335 return create(getUser(), getPassword()); 336 } 337 338 public GenerationObject create(String _user, String _password) 339 throws SQLException { 340 log.debug( 341 "StandardPoolDataSource:create create a connection for the pool"); 342 GenerationObject genObject; 343 PooledConnection pooledCon = cpds.getPooledConnection(_user, _password); 344 346 pooledCon.addConnectionEventListener(this); 347 log.debug("StandardPoolDataSource:create create a object for the pool"); 349 genObject = 350 new GenerationObject( 351 pooledCon, 352 pool.getGeneration(), 353 _user, 354 _password); 355 return genObject; } 357 358 361 public void stopPool() { 362 pool.stop(); 363 onOff = false; 364 log.debug("StandardPoolDataSource:stopPool stop now the pool"); 365 } 366 367 public void shutdown(boolean force) { 368 stopPool(); 369 } 370 371 376 public void setLogWriter(PrintWriter logWriter) { 377 pool.setLogger(log); 378 super.setLogger(log); 379 } 380 381 385 public void setDebug(boolean debug) { 386 super.setDebug(debug); 387 pool.setDebug(debug); 388 } 389 390 395 public void setMinSize(int minSize) throws Exception { 396 pool.setMinSize(minSize); 397 } 398 399 404 public void setMaxSize(int maxSize) throws Exception { 405 pool.setMaxSize(maxSize); 406 } 407 408 412 public void setLifeTime(long lifeTime) { 413 pool.setLifeTime(lifeTime); 414 } 415 416 420 public void setSleepTime(long sleepTime) { 421 pool.setSleepTime(sleepTime); 422 } 423 424 429 public void setGC(boolean gc) { 430 pool.setGC(gc); 431 } 432 433 442 public void setCheckLevelObject(int checkLevelObject) { 443 pool.setCheckLevelObject(checkLevelObject); 444 } 445 446 450 public void setJdbcTestStmt(String jdbcTestStmt) { 451 this.jdbcTestStmt = jdbcTestStmt; 452 } 453 454 459 public void setGeneration(int generation) { 460 pool.setGeneration(generation); 461 } 462 463 467 public void setDeadLockMaxWait(long deadLock) { 468 pool.setDeadLockMaxWait(deadLock); 469 } 470 471 475 public void setDeadLockRetryWait(long loopWait) { 476 pool.setDeadLockRetryWait(loopWait); 477 } 478 479 public PrintWriter getLogWriter() { 480 return log; 481 } 482 483 public int getMinSize() { 484 return pool.getMinSize(); 485 } 486 487 public int getMaxSize() { 488 return pool.getMaxSize(); 489 } 490 491 public long getLifeTime() { 492 return pool.getLifeTime(); 493 } 494 495 public long getSleepTime() { 496 return pool.getSleepTime(); 497 } 498 499 public int getGeneration() { 500 return pool.generation; 501 } 502 503 public boolean isGC() { 504 return pool.isGC(); 505 } 506 507 public int getLockedObjectCount() { 508 return pool.getLockedObjectCount(); 509 } 510 511 public int getUnlockedObjectCount() { 512 return pool.getUnlockedObjectCount(); 513 } 514 515 public int getCheckLevelObject() { 516 return pool.getCheckLevelObject(); 517 } 518 519 public String getJdbcTestStmt() { 520 return jdbcTestStmt; 521 } 522 523 public long getDeadLockMaxWait() { 524 return pool.getDeadLockMaxWait(); 525 } 526 527 public long getDeadLockRetryWait() { 528 return pool.getDeadLockRetryWait(); 529 } 530 531 public String toString() { 532 StringBuffer sb = new StringBuffer (); 533 sb.append("StandardPoolDataSource:\n"); 534 sb.append(" data source name=<"+this.dataSourceName+">\n"); 535 sb.append(" jdbc test stmt=<"+this.jdbcTestStmt+">\n"); 536 sb.append(" user=<"+this.user+">\n"); 537 if (this.cpds != null) 538 sb.append(this.cpds.toString()); 539 sb.append(pool.toString()); 540 541 return sb.toString(); 542 } 543 544 552 public Reference getReference() throws NamingException { 553 log.debug( 554 "StandardPoolDataSource:getReference return a reference of the object"); 555 Reference ref = super.getReference(); 556 ref.add( 557 new StringRefAddr ( 558 "checkLevelObject", 559 Integer.toString(getCheckLevelObject()))); 560 ref.add(new StringRefAddr ("lifeTime", Long.toString(getLifeTime()))); 561 ref.add(new StringRefAddr ("jdbcTestStmt", getJdbcTestStmt())); 562 ref.add(new StringRefAddr ("maxSize", Integer.toString(getMaxSize()))); 563 ref.add(new StringRefAddr ("minSize", Integer.toString(getMinSize()))); 564 ref.add(new StringRefAddr ("dataSourceName", getDataSourceName())); 565 return ref; 566 } 567 568 571 public Object getObjectInstance( 572 Object refObj, 573 Name name, 574 Context nameCtx, 575 Hashtable env) 576 throws Exception { 577 578 super.getObjectInstance(refObj, name, nameCtx, env); 579 Reference ref = (Reference ) refObj; 580 this.setLifeTime( 581 Long.parseLong((String ) ref.get("lifeTime").getContent())); 582 this.setJdbcTestStmt((String ) ref.get("jdbcTestStmt").getContent()); 583 this.setMaxSize( 584 Integer.parseInt((String ) ref.get("maxSize").getContent())); 585 this.setMinSize( 586 Integer.parseInt((String ) ref.get("minSize").getContent())); 587 this.setDataSourceName((String ) ref.get("dataSourceName").getContent()); 588 InitialContext ictx = new InitialContext (env); 589 cpds = (ConnectionPoolDataSource ) ictx.lookup(this.dataSourceName); 590 return this; 591 } 592 593 596 public void setThreadFactory(JdbcThreadFactory tf) { 597 super.setThreadFactory(tf); 598 pool.setThreadFactory(tf); 599 } 600 }
| Popular Tags
|