1 21 22 package org.opensubsystems.core.persist.db.connectionpool; 23 24 import java.sql.Connection ; 25 import java.sql.DriverManager ; 26 import java.sql.SQLException ; 27 28 import org.apache.commons.dbcp.ConnectionFactory; 29 import org.apache.commons.dbcp.DriverManagerConnectionFactory; 30 import org.apache.commons.dbcp.PoolableConnectionFactory; 31 import org.apache.commons.pool.KeyedObjectPoolFactory; 32 import org.apache.commons.pool.ObjectPool; 33 import org.apache.commons.pool.impl.GenericKeyedObjectPoolFactory; 34 import org.apache.commons.pool.impl.GenericObjectPool; 35 import org.opensubsystems.core.error.OSSConfigException; 36 import org.opensubsystems.core.error.OSSDatabaseAccessException; 37 import org.opensubsystems.core.error.OSSException; 38 import org.opensubsystems.core.persist.db.DatabaseImpl; 39 import org.opensubsystems.core.persist.db.DatabaseTransactionFactoryImpl; 40 import org.opensubsystems.core.util.GlobalConstants; 41 42 50 public class DBCPDatabaseConnectionFactoryImpl extends PooledDatabaseConnectionFactoryImpl 51 { 52 54 61 public DBCPDatabaseConnectionFactoryImpl( 62 ) throws OSSConfigException, 63 OSSDatabaseAccessException 64 { 65 super(); 66 67 loadDefaultDatabaseProperties(); 69 70 } 75 76 77 86 public DBCPDatabaseConnectionFactoryImpl( 87 DatabaseTransactionFactoryImpl transactionFactory 88 ) throws OSSConfigException, 89 OSSDatabaseAccessException 90 { 91 super(transactionFactory); 92 93 loadDefaultDatabaseProperties(); 95 96 } 101 102 114 public DBCPDatabaseConnectionFactoryImpl( 115 String strDriver, 116 String strURL, 117 String strUser, 118 String strPassword, 119 DatabaseTransactionFactoryImpl transactionFactory 120 ) throws OSSConfigException, 121 OSSDatabaseAccessException 122 { 123 super(strDriver, strURL, strUser, strPassword, transactionFactory); 124 125 } 130 131 133 136 protected Connection getPooledConnection( 137 ConnectionPoolDefinition connectionpool 138 ) throws OSSDatabaseAccessException 139 { 140 Connection conReturn; 141 142 try 143 { 144 conReturn = (Connection )((ObjectPool)connectionpool.getConnectionPool()).borrowObject(); 145 } 146 catch (Exception eExc) 147 { 148 throw new OSSDatabaseAccessException("Cannot get database connection from pool.", 150 eExc); 151 } 152 153 return conReturn; 154 } 155 156 159 protected Connection getPooledConnection( 160 ConnectionPoolDefinition connectionpool, 161 String strUser, 162 String strPassword 163 ) throws OSSDatabaseAccessException 164 { 165 Connection cntDBConnection; 166 167 try 169 { 170 cntDBConnection = DriverManager.getConnection(connectionpool.getUrl(), 171 strUser, 172 strPassword); 173 if (cntDBConnection != null) 174 { 175 m_mpNotPooledConnections.put(cntDBConnection, cntDBConnection); 176 } 177 } 178 catch (SQLException sqleExc) 179 { 180 throw new OSSDatabaseAccessException("Cannot get database connection" + 181 " for explicitely specified user.", 182 sqleExc); 183 } 184 185 return cntDBConnection; 186 } 187 188 191 protected Object createConnectionPool( 192 String strConnectionPoolName, 193 String strDriverName, 194 String strUrl, 195 String strUser, 196 String strPassword 197 ) throws OSSException 198 { 199 ObjectPool connectionPool; 205 ConnectionFactory connectionFactory; 206 PoolableConnectionFactory poolableConnectionFactory; 207 208 PooledDatabaseConnectionFactorySetupReader setupReader 209 = new PooledDatabaseConnectionFactorySetupReader(strConnectionPoolName); 210 211 int iInitialPoolSize = setupReader.getIntegerParameterValue( 212 PooledDatabaseConnectionFactorySetupReader.DBPOOL_INITIAL_SIZE).intValue(); 213 int iMinimalPoolSize = setupReader.getIntegerParameterValue( 214 PooledDatabaseConnectionFactorySetupReader.DBPOOL_MIN_SIZE).intValue(); 215 int iMaximalPoolSize = setupReader.getIntegerParameterValue( 216 PooledDatabaseConnectionFactorySetupReader.DBPOOL_MAX_SIZE).intValue(); 217 boolean bCanGrow = setupReader.getBooleanParameterValue( 218 PooledDatabaseConnectionFactorySetupReader.DBPOOL_CAN_GROW).booleanValue(); 219 long lMaxWaitTimeForConnection = setupReader.getLongParameterValue( 220 PooledDatabaseConnectionFactorySetupReader.DBPOOL_WAIT_PERIOD).longValue(); 221 boolean bValidateOnBorrow = setupReader.getBooleanParameterValue( 222 PooledDatabaseConnectionFactorySetupReader.DBPOOL_VALIDATE_BORROW).booleanValue(); 223 boolean bValidateOnReturn = setupReader.getBooleanParameterValue( 224 PooledDatabaseConnectionFactorySetupReader.DBPOOL_VALIDATE_RETURN).booleanValue(); 225 boolean bValidateOnIdle = setupReader.getBooleanParameterValue( 226 PooledDatabaseConnectionFactorySetupReader.DBPOOL_VALIDATE_IDLE).booleanValue(); 227 long lTimeBetweenEvictionRunsMillis = setupReader.getLongParameterValue( 228 PooledDatabaseConnectionFactorySetupReader.DBPOOL_IDLE_CHECK_PERIOD).longValue(); 229 int iNumTestsPerEvictionRun = setupReader.getIntegerParameterValue( 230 PooledDatabaseConnectionFactorySetupReader.DBPOOL_IDLE_CHECK_SIZE).intValue(); 231 long lMinEvictableIdleTimeMillis = setupReader.getLongParameterValue( 232 PooledDatabaseConnectionFactorySetupReader.DBPOOL_IDLE_PERIOD).longValue(); 233 int iTransactionIsolation 234 = DatabaseImpl.getInstance().getTransactionIsolation( 235 PooledDatabaseConnectionFactorySetupReader.convertTransactionIsolationToConstant( 236 setupReader.getStringParameterValue( 237 PooledDatabaseConnectionFactorySetupReader.DBPOOL_TRANSACTION_ISOLATION 238 ).toString())); 239 int iPreparedStatementCacheSize = setupReader.getIntegerParameterValue( 240 PooledDatabaseConnectionFactorySetupReader.DBPOOL_PREPSTATEMENT_CACHE_SIZE 241 ).intValue(); 242 243 connectionPool = new GenericObjectPool( 247 null, iMaximalPoolSize, 249 bCanGrow ? GenericObjectPool.WHEN_EXHAUSTED_GROW 250 : GenericObjectPool.WHEN_EXHAUSTED_BLOCK, 251 lMaxWaitTimeForConnection, 252 iMaximalPoolSize, iMinimalPoolSize, bValidateOnBorrow, 258 bValidateOnReturn, 259 lTimeBetweenEvictionRunsMillis, 260 iNumTestsPerEvictionRun, 261 lMinEvictableIdleTimeMillis, 262 bValidateOnIdle); 263 264 265 connectionFactory = new DriverManagerConnectionFactory( 270 strUrl, strUser, strPassword); 271 272 KeyedObjectPoolFactory statementPool = null; 278 if (iPreparedStatementCacheSize >= 0) 279 { 280 statementPool = new GenericKeyedObjectPoolFactory(null, 281 iPreparedStatementCacheSize); 282 } 283 284 poolableConnectionFactory = new PoolableConnectionFactory( 288 connectionFactory, 289 connectionPool, 290 statementPool, 291 DatabaseImpl.getInstance().getConnectionTestStatement(), 292 false, false, iTransactionIsolation); 295 296 try 299 { 300 for (int iIndex = 0; iIndex < iInitialPoolSize; iIndex++) 301 { 302 connectionPool.addObject(); 303 } 304 } 305 catch (Exception e) 306 { 307 throw new OSSDatabaseAccessException("Error preloading the connection pool", e); 308 } 309 310 if (GlobalConstants.ERROR_CHECKING) 311 { 312 assert poolableConnectionFactory != null 314 : "Poolable connection factoryh cannot be null."; 315 } 316 317 return connectionPool; 318 } 319 320 323 protected void closeConnectionPool( 324 ConnectionPoolDefinition connectionpool 325 ) throws OSSException 326 { 327 try 328 { 329 ((ObjectPool)connectionpool.getConnectionPool()).close(); 330 } 331 catch (Exception eExc) 332 { 333 throw new OSSDatabaseAccessException("Cannot close connection pool.", eExc); 335 } 336 } 337 } 338 | Popular Tags |