1 22 23 24 package com.mchange.v2.c3p0; 25 26 import java.beans.*; 27 import java.io.*; 28 import java.sql.*; 29 import java.util.*; 30 import javax.naming.*; 31 import com.mchange.v2.log.*; 32 import com.mchange.v2.naming.*; 33 import com.mchange.v2.c3p0.impl.*; 34 35 import javax.sql.DataSource ; 36 import com.mchange.v2.beans.BeansUtils; 37 import com.mchange.v2.c3p0.cfg.C3P0Config; 38 39 42 public final class ComboPooledDataSource extends AbstractPoolBackedDataSource implements PooledDataSource, Serializable, Referenceable 43 { 44 final static MLogger logger = MLog.getLogger( ComboPooledDataSource.class ); 45 46 final static Set TO_STRING_IGNORE_PROPS = new HashSet( Arrays.asList( new String [] { 47 "connection", 48 "lastCheckinFailureDefaultUser", 49 "lastCheckoutFailureDefaultUser", 50 "lastConnectionTestFailureDefaultUser", 51 "lastIdleTestFailureDefaultUser", 52 "logWriter", 53 "loginTimeout", 54 "numBusyConnections", 55 "numBusyConnectionsAllUsers", 56 "numBusyConnectionsDefaultUser", 57 "numConnections", 58 "numConnectionsAllUsers", 59 "numConnectionsDefaultUser", 60 "numFailedCheckinsDefaultUser", 61 "numFailedCheckoutsDefaultUser", 62 "numFailedIdleTestsDefaultUser", 63 "numIdleConnections", 64 "numIdleConnectionsAllUsers", 65 "numIdleConnectionsDefaultUser", 66 "numUnclosedOrphanedConnections", 67 "numUnclosedOrphanedConnectionsAllUsers", 68 "numUnclosedOrphanedConnectionsDefaultUser", 69 "numUserPools", 70 "effectivePropertyCycleDefaultUser", 71 "startTimeMillisDefaultUser", 72 "statementCacheNumCheckedOutDefaultUser", 73 "statementCacheNumCheckedOutStatementsAllUsers", 74 "statementCacheNumConnectionsWithCachedStatementsAllUsers", 75 "statementCacheNumConnectionsWithCachedStatementsDefaultUser", 76 "statementCacheNumStatementsAllUsers", 77 "statementCacheNumStatementsDefaultUser", 78 "threadPoolSize", 79 "threadPoolNumActiveThreads", 80 "threadPoolNumIdleThreads", 81 "threadPoolNumTasksPending", 82 "threadPoolStackTraces", 83 "threadPoolStatus", 84 "overrideDefaultUser", 85 "overrideDefaultPassword", 86 "password", 87 "reference", 88 "upTimeMillisDefaultUser", 89 "user", 90 "userOverridesAsString", 91 "allUsers", 92 "connectionPoolDataSource" 93 } ) ); 94 95 98 transient DriverManagerDataSource dmds; 101 transient WrapperConnectionPoolDataSource wcpds; 102 103 public ComboPooledDataSource() 104 { this( true ); } 105 106 public ComboPooledDataSource( boolean autoregister ) 107 { 108 super( autoregister ); 109 110 112 dmds = new DriverManagerDataSource(); 113 wcpds = new WrapperConnectionPoolDataSource(); 114 115 wcpds.setNestedDataSource( dmds ); 116 117 try 118 { this.setConnectionPoolDataSource( wcpds ); } 119 catch (PropertyVetoException e) 120 { 121 logger.log(MLevel.WARNING, "Hunh??? This can't happen. We haven't set up any listeners to veto the property change yet!", e); 122 throw new RuntimeException ("Hunh??? This can't happen. We haven't set up any listeners to veto the property change yet! " + e); 123 } 124 125 setUpPropertyEvents(); 128 } 129 130 private void setUpPropertyEvents() 131 { 132 VetoableChangeListener wcpdsConsistencyEnforcer = new VetoableChangeListener() 133 { 134 public void vetoableChange( PropertyChangeEvent evt ) throws PropertyVetoException 136 { 137 String propName = evt.getPropertyName(); 138 Object val = evt.getNewValue(); 139 140 if ( "connectionPoolDataSource".equals( propName ) ) 141 { 142 if (val instanceof WrapperConnectionPoolDataSource) 143 { 144 DataSource nested = (DataSource ) ((WrapperConnectionPoolDataSource)val).getNestedDataSource(); 145 if (! (nested instanceof DriverManagerDataSource) ) 146 throw new PropertyVetoException("ComboPooledDataSource requires that its unpooled DataSource " + 147 " be set at all times, and that it be a" + 148 " com.mchange.v2.c3p0.DriverManagerDataSource. Bad: " + nested, evt); 149 } 150 else 151 throw new PropertyVetoException("ComboPooledDataSource requires that its ConnectionPoolDataSource " + 152 " be set at all times, and that it be a" + 153 " com.mchange.v2.c3p0.WrapperConnectionPoolDataSource. Bad: " + val, evt); 154 } 155 } 156 }; 157 this.addVetoableChangeListener( wcpdsConsistencyEnforcer ); 158 159 PropertyChangeListener wcpdsStateUpdater = new PropertyChangeListener() 160 { 161 public void propertyChange( PropertyChangeEvent evt ) 162 { updateLocalVarsFromCpdsProp(); } 163 }; 164 this.addPropertyChangeListener( wcpdsStateUpdater ); 165 } 166 167 private void updateLocalVarsFromCpdsProp() 168 { 169 this.wcpds = (WrapperConnectionPoolDataSource) this.getConnectionPoolDataSource(); 170 this.dmds = (DriverManagerDataSource) wcpds.getNestedDataSource(); 171 } 172 173 public ComboPooledDataSource(String configName) 174 { 175 this(); 176 initializeNamedConfig( configName ); 177 } 178 179 186 public String getDescription() 188 { return dmds.getDescription(); } 189 190 public void setDescription( String description ) 191 { dmds.setDescription( description ); } 192 193 public String getDriverClass() 194 { return dmds.getDriverClass(); } 195 196 public void setDriverClass( String driverClass ) throws PropertyVetoException 197 { 198 dmds.setDriverClass( driverClass ); 199 } 201 202 public String getJdbcUrl() 203 { 204 return dmds.getJdbcUrl(); 206 } 207 208 public void setJdbcUrl( String jdbcUrl ) 209 { 210 dmds.setJdbcUrl( jdbcUrl ); 211 this.resetPoolManager( false ); 212 } 216 217 public Properties getProperties() 218 { 219 return dmds.getProperties(); 221 } 222 223 public void setProperties( Properties properties ) 224 { 225 dmds.setProperties( properties ); 227 this.resetPoolManager(false); 228 } 229 230 public String getUser() 232 { return dmds.getUser(); } 233 234 public void setUser( String user ) 235 { 236 dmds.setUser( user ); 237 this.resetPoolManager( false ); 238 } 239 240 public String getPassword() 241 { return dmds.getPassword(); } 242 243 public void setPassword( String password ) 244 { 245 dmds.setPassword( password ); 246 this.resetPoolManager( false ); 247 } 248 249 public int getCheckoutTimeout() 251 { return wcpds.getCheckoutTimeout(); } 252 253 public void setCheckoutTimeout( int checkoutTimeout ) 254 { 255 wcpds.setCheckoutTimeout( checkoutTimeout ); 256 this.resetPoolManager( false ); 257 } 258 259 public int getAcquireIncrement() 260 { return wcpds.getAcquireIncrement(); } 261 262 public void setAcquireIncrement( int acquireIncrement ) 263 { 264 wcpds.setAcquireIncrement( acquireIncrement ); 265 this.resetPoolManager( false ); 266 } 267 268 public int getAcquireRetryAttempts() 269 { return wcpds.getAcquireRetryAttempts(); } 270 271 public void setAcquireRetryAttempts( int acquireRetryAttempts ) 272 { 273 wcpds.setAcquireRetryAttempts( acquireRetryAttempts ); 274 this.resetPoolManager( false ); 275 } 276 277 public int getAcquireRetryDelay() 278 { return wcpds.getAcquireRetryDelay(); } 279 280 public void setAcquireRetryDelay( int acquireRetryDelay ) 281 { 282 wcpds.setAcquireRetryDelay( acquireRetryDelay ); 283 this.resetPoolManager( false ); 284 } 285 286 public boolean isAutoCommitOnClose() 287 { return wcpds.isAutoCommitOnClose(); } 288 289 public void setAutoCommitOnClose( boolean autoCommitOnClose ) 290 { 291 wcpds.setAutoCommitOnClose( autoCommitOnClose ); 292 this.resetPoolManager( false ); 293 } 294 295 public String getConnectionTesterClassName() 296 { return wcpds.getConnectionTesterClassName(); } 297 298 public void setConnectionTesterClassName( String connectionTesterClassName ) throws PropertyVetoException 299 { 300 wcpds.setConnectionTesterClassName( connectionTesterClassName ); 301 this.resetPoolManager( false ); 302 } 303 304 public String getAutomaticTestTable() 305 { return wcpds.getAutomaticTestTable(); } 306 307 public void setAutomaticTestTable( String automaticTestTable ) 308 { 309 wcpds.setAutomaticTestTable( automaticTestTable ); 310 this.resetPoolManager( false ); 311 } 312 313 public boolean isForceIgnoreUnresolvedTransactions() 314 { return wcpds.isForceIgnoreUnresolvedTransactions(); } 315 316 public void setForceIgnoreUnresolvedTransactions( boolean forceIgnoreUnresolvedTransactions ) 317 { 318 wcpds.setForceIgnoreUnresolvedTransactions( forceIgnoreUnresolvedTransactions ); 319 this.resetPoolManager( false ); 320 } 321 322 public int getIdleConnectionTestPeriod() 323 { return wcpds.getIdleConnectionTestPeriod(); } 324 325 public void setIdleConnectionTestPeriod( int idleConnectionTestPeriod ) 326 { 327 wcpds.setIdleConnectionTestPeriod( idleConnectionTestPeriod ); 328 this.resetPoolManager( false ); 329 } 330 331 public int getInitialPoolSize() 332 { return wcpds.getInitialPoolSize(); } 333 334 public void setInitialPoolSize( int initialPoolSize ) 335 { 336 wcpds.setInitialPoolSize( initialPoolSize ); 337 this.resetPoolManager( false ); 338 } 339 340 public int getMaxIdleTime() 341 { return wcpds.getMaxIdleTime(); } 342 343 public void setMaxIdleTime( int maxIdleTime ) 344 { 345 wcpds.setMaxIdleTime( maxIdleTime ); 346 this.resetPoolManager( false ); 347 } 348 349 public int getMaxPoolSize() 350 { return wcpds.getMaxPoolSize(); } 351 352 public void setMaxPoolSize( int maxPoolSize ) 353 { 354 wcpds.setMaxPoolSize( maxPoolSize ); 355 this.resetPoolManager( false ); 356 } 357 358 public int getMaxStatements() 359 { return wcpds.getMaxStatements(); } 360 361 public void setMaxStatements( int maxStatements ) 362 { 363 wcpds.setMaxStatements( maxStatements ); 364 this.resetPoolManager( false ); 365 } 366 367 public int getMaxStatementsPerConnection() 368 { return wcpds.getMaxStatementsPerConnection(); } 369 370 public void setMaxStatementsPerConnection( int maxStatementsPerConnection ) 371 { 372 wcpds.setMaxStatementsPerConnection( maxStatementsPerConnection ); 373 this.resetPoolManager( false ); 374 } 375 376 public int getMinPoolSize() 377 { return wcpds.getMinPoolSize(); } 378 379 public void setMinPoolSize( int minPoolSize ) 380 { 381 wcpds.setMinPoolSize( minPoolSize ); 382 this.resetPoolManager( false ); 383 } 384 385 public String getOverrideDefaultUser() 386 { return wcpds.getOverrideDefaultUser(); } 387 388 public void setOverrideDefaultUser(String overrideDefaultUser) 389 { 390 wcpds.setOverrideDefaultUser( overrideDefaultUser ); 391 this.resetPoolManager( false ); 392 } 393 394 public String getOverrideDefaultPassword() 395 { return wcpds.getOverrideDefaultPassword(); } 396 397 public void setOverrideDefaultPassword(String overrideDefaultPassword) 398 { 399 wcpds.setOverrideDefaultPassword( overrideDefaultPassword ); 400 this.resetPoolManager( false ); 401 } 402 403 public int getPropertyCycle() 404 { return wcpds.getPropertyCycle(); } 405 406 public void setPropertyCycle( int propertyCycle ) 407 { 408 wcpds.setPropertyCycle( propertyCycle ); 409 this.resetPoolManager( false ); 410 } 411 412 public boolean isBreakAfterAcquireFailure() 413 { return wcpds.isBreakAfterAcquireFailure(); } 414 415 public void setBreakAfterAcquireFailure( boolean breakAfterAcquireFailure ) 416 { 417 wcpds.setBreakAfterAcquireFailure( breakAfterAcquireFailure ); 418 this.resetPoolManager( false ); 419 } 420 421 public boolean isTestConnectionOnCheckout() 422 { return wcpds.isTestConnectionOnCheckout(); } 423 424 public void setTestConnectionOnCheckout( boolean testConnectionOnCheckout ) 425 { 426 wcpds.setTestConnectionOnCheckout( testConnectionOnCheckout ); 427 this.resetPoolManager( false ); 428 } 429 430 public boolean isTestConnectionOnCheckin() 431 { return wcpds.isTestConnectionOnCheckin(); } 432 433 public void setTestConnectionOnCheckin( boolean testConnectionOnCheckin ) 434 { 435 wcpds.setTestConnectionOnCheckin( testConnectionOnCheckin ); 436 this.resetPoolManager( false ); 437 } 438 439 public boolean isUsesTraditionalReflectiveProxies() 440 { return wcpds.isUsesTraditionalReflectiveProxies(); } 441 442 public void setUsesTraditionalReflectiveProxies( boolean usesTraditionalReflectiveProxies ) 443 { 444 wcpds.setUsesTraditionalReflectiveProxies( usesTraditionalReflectiveProxies ); 445 this.resetPoolManager( false ); 446 } 447 448 public String getPreferredTestQuery() 449 { return wcpds.getPreferredTestQuery(); } 450 451 public void setPreferredTestQuery( String preferredTestQuery ) 452 { 453 wcpds.setPreferredTestQuery( preferredTestQuery ); 454 this.resetPoolManager( false ); 455 } 456 457 public String getUserOverridesAsString() 458 { return wcpds.getUserOverridesAsString(); } 459 460 public void setUserOverridesAsString( String userOverridesAsString ) throws PropertyVetoException 461 { 462 wcpds.setUserOverridesAsString( userOverridesAsString ); 463 this.resetPoolManager( false ); 464 } 465 466 public int getMaxAdministrativeTaskTime() 467 { return wcpds.getMaxAdministrativeTaskTime(); } 468 469 public void setMaxAdministrativeTaskTime( int maxAdministrativeTaskTime ) 470 { 471 wcpds.setMaxAdministrativeTaskTime( maxAdministrativeTaskTime ); 472 this.resetPoolManager( false ); 473 } 474 475 public int getMaxIdleTimeExcessConnections() 476 { return wcpds.getMaxIdleTimeExcessConnections(); } 477 478 public void setMaxIdleTimeExcessConnections( int maxIdleTimeExcessConnections ) 479 { 480 wcpds.setMaxIdleTimeExcessConnections( maxIdleTimeExcessConnections ); 481 this.resetPoolManager( false ); 482 } 483 484 public int getMaxConnectionAge() 485 { return wcpds.getMaxConnectionAge(); } 486 487 public void setMaxConnectionAge( int maxConnectionAge ) 488 { 489 wcpds.setMaxConnectionAge( maxConnectionAge ); 490 this.resetPoolManager( false ); 491 } 492 493 public String getConnectionCustomizerClassName() 494 { return wcpds.getConnectionCustomizerClassName(); } 495 496 public void setConnectionCustomizerClassName( String connectionCustomizerClassName ) 497 { 498 wcpds.setConnectionCustomizerClassName( connectionCustomizerClassName ); 499 this.resetPoolManager( false ); 500 } 501 502 public int getUnreturnedConnectionTimeout() 503 { return wcpds.getUnreturnedConnectionTimeout(); } 504 505 public void setUnreturnedConnectionTimeout(int unreturnedConnectionTimeout) 506 { 507 wcpds.setUnreturnedConnectionTimeout( unreturnedConnectionTimeout ); 508 this.resetPoolManager( false ); 509 } 510 511 public boolean isDebugUnreturnedConnectionStackTraces() 512 { return wcpds.isDebugUnreturnedConnectionStackTraces(); } 513 514 public void setDebugUnreturnedConnectionStackTraces(boolean debugUnreturnedConnectionStackTraces) 515 { 516 wcpds.setDebugUnreturnedConnectionStackTraces( debugUnreturnedConnectionStackTraces ); 517 this.resetPoolManager( false ); 518 } 519 520 public String getFactoryClassLocation() 522 { return super.getFactoryClassLocation(); } 523 524 public void setFactoryClassLocation( String factoryClassLocation ) 525 { 526 dmds.setFactoryClassLocation( factoryClassLocation ); 527 wcpds.setFactoryClassLocation( factoryClassLocation ); 528 super.setFactoryClassLocation( factoryClassLocation ); 529 } 530 531 public String toString() 532 { 533 535 StringBuffer sb = new StringBuffer (512); 536 sb.append( this.getClass().getName() ); 537 sb.append(" [ "); 538 try { BeansUtils.appendPropNamesAndValues(sb, this, TO_STRING_IGNORE_PROPS); } 539 catch (Exception e) 540 { 541 sb.append( e.toString() ); 542 } 544 sb.append(" ]"); 545 546 550 return sb.toString(); 551 } 552 553 private static final long serialVersionUID = 1; 555 private static final short VERSION = 0x0001; 556 557 private void writeObject( ObjectOutputStream oos ) throws IOException 558 { 559 oos.writeShort( VERSION ); 560 } 561 562 private void readObject( ObjectInputStream ois ) throws IOException, ClassNotFoundException 563 { 564 short version = ois.readShort(); 565 switch (version) 566 { 567 case VERSION: 568 updateLocalVarsFromCpdsProp(); 569 setUpPropertyEvents(); 570 break; 571 default: 572 throw new IOException("Unsupported Serialized Version: " + version); 573 } 574 } 575 } 576 577 580 684 | Popular Tags |