1 22 23 24 package com.mchange.v2.c3p0; 25 26 import com.mchange.v2.log.*; 27 import java.beans.PropertyChangeEvent ; 28 import java.beans.PropertyVetoException ; 29 import java.util.Arrays ; 30 import java.util.Collections ; 31 import java.util.HashMap ; 32 import java.util.HashSet ; 33 import java.util.Iterator ; 34 import java.util.Map ; 35 import java.util.Properties ; 36 import java.util.Set ; 37 import java.sql.SQLException ; 38 import javax.sql.DataSource ; 39 import javax.sql.ConnectionPoolDataSource ; 40 import com.mchange.v2.sql.SqlUtils; 41 import com.mchange.v2.beans.BeansUtils; 42 43 57 public final class DataSources 58 { 59 final static MLogger logger = MLog.getLogger( DataSources.class ); 60 61 final static Set WRAPPER_CXN_POOL_DATA_SOURCE_OVERWRITE_PROPS; final static Set POOL_BACKED_DATA_SOURCE_OVERWRITE_PROPS; 64 static 65 { 66 String [] props = new String [] 72 { 73 "checkoutTimeout", "acquireIncrement", "acquireRetryAttempts", "acquireRetryDelay", "autoCommitOnClose", "connectionTesterClassName", "forceIgnoreUnresolvedTransactions", "idleConnectionTestPeriod", "initialPoolSize", "maxIdleTime", "maxPoolSize", "maxStatements", "maxStatementsPerConnection", "minPoolSize", "propertyCycle", "breakAfterAcquireFailure", "testConnectionOnCheckout", "testConnectionOnCheckin", "usesTraditionalReflectiveProxies", "preferredTestQuery", "automaticTestTable", "factoryClassLocation" }; 96 97 WRAPPER_CXN_POOL_DATA_SOURCE_OVERWRITE_PROPS = Collections.unmodifiableSet( new HashSet ( Arrays.asList( props ) ) ); 98 99 props = new String [] 105 { 106 "numHelperThreads", 107 "factoryClassLocation" 108 }; 109 110 POOL_BACKED_DATA_SOURCE_OVERWRITE_PROPS = Collections.unmodifiableSet( new HashSet ( Arrays.asList( props ) ) ); 111 } 112 113 117 public static DataSource unpooledDataSource() throws SQLException 118 { 119 DriverManagerDataSource out = new DriverManagerDataSource(); 120 return out; 121 } 122 123 public static DataSource unpooledDataSource(String jdbcUrl) throws SQLException 124 { 125 DriverManagerDataSource out = new DriverManagerDataSource(); 126 out.setJdbcUrl( jdbcUrl ); 127 return out; 128 } 129 130 133 public static DataSource unpooledDataSource(String jdbcUrl, String user, String password) throws SQLException 134 { 135 Properties props = new Properties (); 136 props.put(SqlUtils.DRIVER_MANAGER_USER_PROPERTY, user); 137 props.put(SqlUtils.DRIVER_MANAGER_PASSWORD_PROPERTY, password); 138 return unpooledDataSource( jdbcUrl, props ); 139 } 140 141 150 public static DataSource unpooledDataSource(String jdbcUrl, Properties driverProps) throws SQLException 151 { 152 DriverManagerDataSource out = new DriverManagerDataSource(); 153 out.setJdbcUrl( jdbcUrl ); 154 out.setProperties( driverProps ); 155 return out; 156 } 157 158 166 public static DataSource pooledDataSource( DataSource unpooledDataSource ) throws SQLException 167 { return pooledDataSource( unpooledDataSource, null, (Map ) null ); } 168 169 176 public static DataSource pooledDataSource( DataSource unpooledDataSource, int statement_cache_size ) throws SQLException 177 { 178 181 Map overrideProps = new HashMap (); 182 overrideProps.put( "maxStatements", new Integer ( statement_cache_size ) ); 183 return pooledDataSource( unpooledDataSource, null, overrideProps ); 184 } 185 186 195 public static DataSource pooledDataSource( DataSource unpooledDataSource, PoolConfig pcfg ) throws SQLException 196 { 197 try 198 { 199 WrapperConnectionPoolDataSource wcpds = new WrapperConnectionPoolDataSource(); 200 wcpds.setNestedDataSource( unpooledDataSource ); 201 202 BeansUtils.overwriteSpecificAccessibleProperties( pcfg, wcpds, WRAPPER_CXN_POOL_DATA_SOURCE_OVERWRITE_PROPS ); 204 205 PoolBackedDataSource nascent_pbds = new PoolBackedDataSource(); 206 nascent_pbds.setConnectionPoolDataSource( wcpds ); 207 BeansUtils.overwriteSpecificAccessibleProperties( pcfg, nascent_pbds, POOL_BACKED_DATA_SOURCE_OVERWRITE_PROPS ); 208 209 return nascent_pbds; 210 } 211 catch ( Exception e ) 218 { 219 SQLException sqle = SqlUtils.toSQLException("Exception configuring pool-backed DataSource: " + e, e); 221 if (Debug.DEBUG && Debug.TRACE >= Debug.TRACE_MED && logger.isLoggable( MLevel.FINE ) && e != sqle) 222 logger.log( MLevel.FINE, "Converted exception to throwable SQLException", e ); 223 throw sqle; 224 } 225 } 226 227 244 245 public static DataSource pooledDataSource( DataSource unpooledDataSource, String configName ) throws SQLException 246 { return pooledDataSource( unpooledDataSource, configName, null ); } 247 248 public static DataSource pooledDataSource( DataSource unpooledDataSource, Map overrideProps ) throws SQLException 249 { return pooledDataSource( unpooledDataSource, null, overrideProps ); } 250 251 public static DataSource pooledDataSource( DataSource unpooledDataSource, String configName, Map overrideProps ) throws SQLException 252 { 253 try 254 { 255 WrapperConnectionPoolDataSource wcpds = new WrapperConnectionPoolDataSource(configName); 256 wcpds.setNestedDataSource( unpooledDataSource ); 257 if (overrideProps != null) 258 BeansUtils.overwriteAccessiblePropertiesFromMap( overrideProps, 259 wcpds, 260 false, 261 null, 262 true, 263 MLevel.WARNING, 264 MLevel.WARNING, 265 false); 266 267 PoolBackedDataSource nascent_pbds = new PoolBackedDataSource(configName); 268 nascent_pbds.setConnectionPoolDataSource( wcpds ); 269 if (overrideProps != null) 270 BeansUtils.overwriteAccessiblePropertiesFromMap( overrideProps, 271 nascent_pbds, 272 false, 273 null, 274 true, 275 MLevel.WARNING, 276 MLevel.WARNING, 277 false); 278 279 return nascent_pbds; 280 } 281 catch ( Exception e ) 288 { 289 SQLException sqle = SqlUtils.toSQLException("Exception configuring pool-backed DataSource: " + e, e); 291 if (Debug.DEBUG && Debug.TRACE >= Debug.TRACE_MED && logger.isLoggable( MLevel.FINE ) && e != sqle) 292 logger.log( MLevel.FINE, "Converted exception to throwable SQLException", e ); 293 throw sqle; 294 } 295 } 296 297 304 public static DataSource pooledDataSource( DataSource unpooledDataSource, Properties props ) throws SQLException 305 { 306 308 Properties peeledProps = new Properties (); 309 for (Iterator ii = props.keySet().iterator(); ii.hasNext(); ) 310 { 311 String propKey = (String ) ii.next(); 312 String propVal = props.getProperty( propKey ); 313 String peeledKey = (propKey.startsWith("c3p0.") ? propKey.substring(5) : propKey ); 314 peeledProps.put( peeledKey, propVal ); 315 } 316 return pooledDataSource( unpooledDataSource, null, peeledProps ); 317 } 318 319 334 public static void destroy( DataSource pooledDataSource ) throws SQLException 335 { destroy( pooledDataSource, false ); } 336 337 338 347 public static void forceDestroy( DataSource pooledDataSource ) throws SQLException 348 { destroy( pooledDataSource, true ); } 349 350 private static void destroy( DataSource pooledDataSource, boolean force ) throws SQLException 351 { 352 if ( pooledDataSource instanceof PoolBackedDataSource) 353 { 354 ConnectionPoolDataSource cpds = ((PoolBackedDataSource) pooledDataSource).getConnectionPoolDataSource(); 355 if (cpds instanceof WrapperConnectionPoolDataSource) 356 destroy( ((WrapperConnectionPoolDataSource) cpds).getNestedDataSource(), force ); 357 } 358 if ( pooledDataSource instanceof PooledDataSource ) 359 ((PooledDataSource) pooledDataSource).close( force ); 360 } 361 362 private DataSources() 363 {} 364 } 365 366 367 368 369 | Popular Tags |