1 29 30 package com.caucho.sql; 31 32 import com.caucho.config.ConfigException; 33 import com.caucho.config.types.InitParam; 34 import com.caucho.config.types.Period; 35 import com.caucho.jca.ConnectionPool; 36 import com.caucho.jca.ResourceManagerImpl; 37 import com.caucho.loader.EnvironmentLocal; 38 import com.caucho.log.Log; 39 import com.caucho.management.j2ee.J2EEManagedObject; 40 import com.caucho.management.j2ee.JDBCDataSource; 41 import com.caucho.management.j2ee.JDBCResource; 42 import com.caucho.naming.Jndi; 43 import com.caucho.transaction.TransactionManagerImpl; 44 import com.caucho.util.L10N; 45 46 import javax.annotation.PostConstruct; 47 import javax.resource.spi.ManagedConnectionFactory ; 48 import javax.sql.ConnectionPoolDataSource ; 49 import javax.sql.DataSource ; 50 import javax.sql.XADataSource ; 51 import java.io.IOException ; 52 import java.io.PrintWriter ; 53 import java.sql.Connection ; 54 import java.sql.Driver ; 55 import java.sql.SQLException ; 56 import java.util.logging.Logger ; 57 58 106 public class DBPool implements DataSource { 107 protected static final Logger log = Log.open(DBPool.class); 108 private static final L10N L = new L10N(DBPool.class); 109 110 private static int _g_id; 111 112 private EnvironmentLocal<DBPoolImpl> _localPoolImpl; 113 private EnvironmentLocal<DataSource> _localDataSourceImpl; 114 115 private String _var; 116 private String _jndiName; 117 private String _tmpName; 118 119 private ResourceManagerImpl _resourceManager; 120 private ConnectionPool _connectionPool; 121 122 private DBPoolImpl _poolImpl; 123 private DataSource _dataSource; 124 private DataSourceImpl _resinDataSource; 125 126 130 public DBPool() 131 { 132 _poolImpl = new DBPoolImpl(); 133 134 _resourceManager = ResourceManagerImpl.createLocalManager(); 135 _connectionPool = _resourceManager.createConnectionPool(); 136 } 137 138 141 public String getJndiName() 142 { 143 return _jndiName; 144 } 145 146 150 public void setJndiName(String name) 151 { 152 _jndiName = name; 153 154 if (_var == null) 155 getPool().setName(name); 156 } 157 158 161 public void setVar(String var) 162 { 163 _var = var; 164 165 getPool().setName(var); 166 } 167 168 171 public String getName() 172 { 173 if (_var != null) 174 return _var; 175 else if (getJndiName() != null) 176 return getJndiName(); 177 else { 178 _tmpName = "dbpool-" + _g_id++; 179 return _tmpName; 180 } 181 182 } 183 184 187 public DriverConfig createDriver() 188 throws ConfigException 189 { 190 return getPool().createDriver(); 191 } 192 193 196 public DriverConfig createBackupDriver() 197 throws ConfigException 198 { 199 return getPool().createBackupDriver(); 200 } 201 202 205 public void setInitParam(InitParam init) 206 { 207 getPool().setInitParam(init); 208 } 209 210 213 public void setJDBCDriver(Driver jdbcDriver) 214 throws SQLException 215 { 216 getPool().setJDBCDriver(jdbcDriver); 217 } 218 219 222 public ConnectionConfig createConnection() 223 throws ConfigException 224 { 225 return getPool().createConnection(); 226 } 227 228 231 public void setPoolDataSource(ConnectionPoolDataSource poolDataSource) 232 throws SQLException 233 { 234 getPool().setPoolDataSource(poolDataSource); 235 } 236 237 240 public void setXADataSource(XADataSource xaDataSource) 241 throws SQLException 242 { 243 getPool().setXADataSource(xaDataSource); 244 } 245 246 249 public void setURL(String url) 250 throws ConfigException 251 { 252 getPool().setURL(url); 253 } 254 255 258 public String getUser() 259 { 260 return getPool().getUser(); 261 } 262 263 266 public void setUser(String user) 267 { 268 getPool().setUser(user); 269 } 270 271 274 public String getPassword() 275 { 276 return getPool().getPassword(); 277 } 278 279 282 public void setPassword(String password) 283 { 284 getPool().setPassword(password); 285 } 286 287 290 public int getMaxConnections() 291 { 292 return _connectionPool.getMaxConnections(); 293 } 294 295 298 public void setMaxConnections(int maxConnections) 299 throws ConfigException 300 { 301 _connectionPool.setMaxConnections(maxConnections); 302 } 303 304 307 public int getTotalConnections() 308 { 309 return _connectionPool.getConnectionCount(); 310 } 311 312 315 public void setConnectionWaitTime(Period waitTime) 316 { 317 _connectionPool.setConnectionWaitTime(waitTime); 318 } 319 320 323 public long getConnectionWaitTime() 324 { 325 return _connectionPool.getConnectionWaitTime(); 326 } 327 328 332 public void setMaxOverflowConnections(int maxOverflowConnections) 333 { 334 _connectionPool.setMaxOverflowConnections(maxOverflowConnections); 335 } 336 337 340 public void setMaxCreateConnections(int maxCreateConnections) 341 throws ConfigException 342 { 343 _connectionPool.setMaxCreateConnections(maxCreateConnections); 344 } 345 346 349 public void setSaveAllocationStackTrace(boolean save) 350 { 351 _connectionPool.setSaveAllocationStackTrace(save); 352 } 353 354 358 public int getMaxOverflowConnections() 359 { 360 return _connectionPool.getMaxOverflowConnections(); 361 } 362 363 366 public int getActiveConnections() 367 { 368 return _connectionPool.getConnectionActiveCount(); 369 } 370 371 375 public long getMaxIdleTime() 376 { 377 return _connectionPool.getMaxIdleTime(); 378 } 379 380 384 public void setMaxIdleTime(Period idleTime) 385 { 386 _connectionPool.setMaxIdleTime(idleTime.getPeriod()); 387 } 388 389 393 public long getMaxPoolTime() 394 { 395 return _connectionPool.getMaxPoolTime(); 396 } 397 398 402 public void setMaxPoolTime(Period maxPoolTime) 403 { 404 _connectionPool.setMaxPoolTime(maxPoolTime.getPeriod()); 405 } 406 407 410 public long getMaxActiveTime() 411 { 412 return _connectionPool.getMaxActiveTime(); 413 } 414 415 418 public void setMaxActiveTime(Period maxActiveTime) 419 { 420 _connectionPool.setMaxActiveTime(maxActiveTime.getPeriod()); 421 } 422 423 426 public String getPingTable() 427 { 428 return getPool().getPingTable(); 429 } 430 431 436 public void setPingTable(String pingTable) 437 { 438 getPool().setPingTable(pingTable); 439 } 440 441 444 public boolean getPingOnReuse() 445 { 446 return getPool().getPingOnReuse(); 447 } 448 449 452 public void setPingOnReuse(boolean pingOnReuse) 453 { 454 getPool().setPingOnReuse(pingOnReuse); 455 } 456 457 460 public boolean getPingOnIdle() 461 { 462 return getPool().getPingOnIdle(); 463 } 464 465 468 public void setPingOnIdle(boolean pingOnIdle) 469 { 470 getPool().setPingOnIdle(pingOnIdle); 471 } 472 473 476 public void setPing(boolean ping) 477 { 478 getPool().setPing(ping); 479 } 480 481 484 public void setPingInterval(Period interval) 485 { 486 getPool().setPingInterval(interval); 487 } 488 489 492 public long getPingInterval() 493 { 494 return getPool().getPingInterval(); 495 } 496 497 500 public int getPreparedStatementCacheSize() 501 { 502 return getPool().getPreparedStatementCacheSize(); 503 } 504 505 508 public void setPreparedStatementCacheSize(int size) 509 { 510 getPool().setPreparedStatementCacheSize(size); 511 } 512 513 516 public void setTransactionManager(TransactionManagerImpl tm) 517 { 518 getPool().setTransactionManager(tm); 519 } 520 521 524 public void setWrapStatements(boolean isWrap) 525 { 526 getPool().setWrapStatements(isWrap); 527 } 528 529 532 538 539 542 public void setTransactionTimeout(Period period) 543 { 544 getPool().setTransactionTimeout(period); 545 } 546 547 550 public boolean isXA() 551 { 552 return getPool().isXA(); 553 } 554 555 558 public void setXA(boolean isTransactional) 559 { 560 getPool().setXA(isTransactional); 561 } 562 563 566 public void setXAForbidSameRM(boolean isXAForbidSameRM) 567 { 568 getPool().setXAForbidSameRM(isXAForbidSameRM); 569 } 570 571 574 public void setSpy(boolean isSpy) 575 throws IOException 576 { 577 getPool().setSpy(isSpy); 578 } 579 580 583 @PostConstruct 584 public void init() 585 throws Exception 586 { 587 _localPoolImpl = new EnvironmentLocal<DBPoolImpl>("caucho.db-pool." + getName()); 588 _localPoolImpl.set(_poolImpl); 589 590 _poolImpl.init(); 591 592 _connectionPool.setName(getName()); 593 594 _connectionPool.setShareable(true); 595 _connectionPool.setXATransaction(_poolImpl.isXATransaction()); 596 _connectionPool.setLocalTransaction(_poolImpl.isLocalTransaction()); 597 598 ManagedConnectionFactory mcf = _poolImpl.getManagedConnectionFactory(); 599 600 601 _dataSource = (DataSource) _connectionPool.init(mcf); 602 _connectionPool.start(); 603 604 _localDataSourceImpl = new EnvironmentLocal<DataSource>("caucho.data-source." + getName()); 605 _localDataSourceImpl.set(_dataSource); 606 607 if (_jndiName != null) { 608 String name = _jndiName; 609 if (! name.startsWith("java:")) 610 name = "java:comp/env/" + name; 611 612 log.config("database " + name + " starting"); 613 614 Jndi.bindDeep(name, this); 615 } 616 617 J2EEManagedObject.register(new JDBCResource(this)); 618 J2EEManagedObject.register(new JDBCDataSource(this)); 619 } 620 621 624 public Connection getConnection() throws SQLException 625 { 626 return getDataSource().getConnection(); 627 } 628 629 638 public Connection getConnection(String user, String password) 639 throws SQLException 640 { 641 return getDataSource().getConnection(user, password); 642 } 643 644 647 public void closeIdleConnections() 648 { 649 ConnectionPool connectionPool = _connectionPool; 650 651 if (connectionPool != null) 652 connectionPool.clear(); 653 } 654 655 658 public int getLoginTimeout() throws SQLException 659 { 660 return getDataSource().getLoginTimeout(); 661 } 662 663 666 public void setLoginTimeout(int timeout) throws SQLException 667 { 668 getDataSource().setLoginTimeout(timeout); 669 } 670 671 674 public PrintWriter getLogWriter() throws SQLException 675 { 676 return getDataSource().getLogWriter(); 677 } 678 679 682 public void setLogWriter(PrintWriter log) throws SQLException 683 { 684 getDataSource().setLogWriter(log); 685 } 686 687 690 private DBPoolImpl getPool() 691 { 692 if (_poolImpl == null || _poolImpl.isClosed()) { 693 _poolImpl = _localPoolImpl.get(); 694 695 if (_poolImpl == null) 696 throw new IllegalStateException (L.l("DBPool `{0}' no longer exists.", 697 getName())); 698 } 699 700 return _poolImpl; 701 } 702 703 706 private DataSource getDataSource() 707 { 708 if (_dataSource == null || 709 _resinDataSource != null && _resinDataSource.isClosed()) { 710 _dataSource = _localDataSourceImpl.get(); 711 712 if (_dataSource instanceof DataSourceImpl) 713 _resinDataSource = (DataSourceImpl) _dataSource; 714 else 715 _resinDataSource = null; 716 717 if (_dataSource == null) 718 throw new IllegalStateException (L.l("DBPool `{0}' no longer exists.", 719 getName())); 720 } 721 722 return _dataSource; 723 } 724 725 728 public String toString() 729 { 730 return "DBPool[" + getName() + "]"; 731 } 732 } 733 734 | Popular Tags |