1 21 22 package org.opensubsystems.core.persist.db.connectionpool; 23 24 import java.sql.Connection ; 25 26 import org.opensubsystems.core.error.OSSConfigException; 27 import org.opensubsystems.core.error.OSSDatabaseAccessException; 28 import org.opensubsystems.core.error.OSSException; 29 import org.opensubsystems.core.persist.db.DatabaseImpl; 30 import org.opensubsystems.core.persist.db.DatabaseTransactionFactoryImpl; 31 32 import com.mchange.v2.c3p0.ComboPooledDataSource; 33 import com.mchange.v2.c3p0.PooledDataSource; 34 35 44 public class C3P0DatabaseConnectionFactoryImpl extends PooledDatabaseConnectionFactoryImpl 45 { 46 48 55 public C3P0DatabaseConnectionFactoryImpl( 56 ) throws OSSConfigException, 57 OSSDatabaseAccessException 58 { 59 super(); 60 61 loadDefaultDatabaseProperties(); 63 64 } 69 70 79 public C3P0DatabaseConnectionFactoryImpl( 80 DatabaseTransactionFactoryImpl transactionFactory 81 ) throws OSSConfigException, 82 OSSDatabaseAccessException 83 { 84 super(transactionFactory); 85 86 loadDefaultDatabaseProperties(); 88 89 } 94 95 107 public C3P0DatabaseConnectionFactoryImpl( 108 String strDriver, 109 String strURL, 110 String strUser, 111 String strPassword, 112 DatabaseTransactionFactoryImpl transactionFactory 113 ) throws OSSConfigException, 114 OSSDatabaseAccessException 115 { 116 super(strDriver, strURL, strUser, strPassword, transactionFactory); 117 118 } 123 124 126 129 protected Connection getPooledConnection( 130 ConnectionPoolDefinition connectionpool 131 ) throws OSSDatabaseAccessException 132 { 133 Connection conReturn; 134 135 try 136 { 137 conReturn = (Connection )((PooledDataSource) 138 connectionpool.getConnectionPool()).getConnection(); 139 } 140 catch (Exception eExc) 141 { 142 throw new OSSDatabaseAccessException("Cannot get database connection from pool.", 144 eExc); 145 } 146 147 return conReturn; 148 } 149 150 153 protected Connection getPooledConnection( 154 ConnectionPoolDefinition connectionpool, 155 String strUser, 156 String strPassword 157 ) throws OSSDatabaseAccessException 158 { 159 Connection conReturn; 160 161 try 162 { 163 conReturn = (Connection )((PooledDataSource) 164 connectionpool.getConnectionPool()).getConnection(strUser, 165 strPassword); 166 } 167 catch (Exception eExc) 168 { 169 throw new OSSDatabaseAccessException("Cannot get database connection from pool.", 171 eExc); 172 } 173 174 return conReturn; 175 } 176 177 180 protected Object createConnectionPool( 181 String strConnectionPoolName, 182 String strDriverName, 183 String strUrl, 184 String strUser, 185 String strPassword 186 ) throws OSSException 187 { 188 PooledDatabaseConnectionFactorySetupReader setupReader 189 = new PooledDatabaseConnectionFactorySetupReader(strConnectionPoolName); 190 191 int iInitialPoolSize = setupReader.getIntegerParameterValue( 192 PooledDatabaseConnectionFactorySetupReader.DBPOOL_INITIAL_SIZE).intValue(); 193 int iMinimalPoolSize = setupReader.getIntegerParameterValue( 194 PooledDatabaseConnectionFactorySetupReader.DBPOOL_MIN_SIZE).intValue(); 195 int iMaximalPoolSize = setupReader.getIntegerParameterValue( 196 PooledDatabaseConnectionFactorySetupReader.DBPOOL_MAX_SIZE).intValue(); 197 long lMaxWaitTimeForConnection = setupReader.getLongParameterValue( 200 PooledDatabaseConnectionFactorySetupReader.DBPOOL_WAIT_PERIOD).longValue(); 201 boolean bValidateOnBorrow = setupReader.getBooleanParameterValue( 202 PooledDatabaseConnectionFactorySetupReader.DBPOOL_VALIDATE_BORROW).booleanValue(); 203 boolean bValidateOnReturn = setupReader.getBooleanParameterValue( 204 PooledDatabaseConnectionFactorySetupReader.DBPOOL_VALIDATE_RETURN).booleanValue(); 205 int iRetryCountForConnection = setupReader.getIntegerParameterValue( 206 PooledDatabaseConnectionFactorySetupReader.DBPOOL_RETRY_COUNT).intValue(); 207 long lRetryTimeForConnection = setupReader.getLongParameterValue( 208 PooledDatabaseConnectionFactorySetupReader.DBPOOL_RETRY_PERIOD).longValue(); 209 long lTimeBetweenEvictionRunsMillis = setupReader.getLongParameterValue( 210 PooledDatabaseConnectionFactorySetupReader.DBPOOL_IDLE_CHECK_PERIOD).longValue(); 211 long lMinEvictableIdleTimeMillis = setupReader.getLongParameterValue( 212 PooledDatabaseConnectionFactorySetupReader.DBPOOL_IDLE_PERIOD).longValue(); 213 int iPreparedStatementCacheSize = setupReader.getIntegerParameterValue( 220 PooledDatabaseConnectionFactorySetupReader.DBPOOL_PREPSTATEMENT_CACHE_SIZE 221 ).intValue(); 222 223 ComboPooledDataSource pooledDS = null; 224 225 pooledDS = new ComboPooledDataSource(); 228 pooledDS.setJdbcUrl(strUrl); 229 pooledDS.setUser(strUser); 230 pooledDS.setPassword(strPassword); 231 232 pooledDS.setInitialPoolSize(iInitialPoolSize); 234 pooledDS.setMinPoolSize(iMinimalPoolSize); 235 pooledDS.setMaxPoolSize(iMaximalPoolSize); 236 pooledDS.setMaxStatements(iPreparedStatementCacheSize); 237 239 pooledDS.setAcquireIncrement(1); 242 pooledDS.setAcquireRetryAttempts(iRetryCountForConnection); 243 pooledDS.setAcquireRetryDelay((int)lRetryTimeForConnection); 244 pooledDS.setAutoCommitOnClose(false); 246 pooledDS.setBreakAfterAcquireFailure(false); 248 pooledDS.setCheckoutTimeout((int)lMaxWaitTimeForConnection); 249 pooledDS.setIdleConnectionTestPeriod((int)lTimeBetweenEvictionRunsMillis / 1000); 254 pooledDS.setMaxIdleTime((int)lMinEvictableIdleTimeMillis / 1000); 256 pooledDS.setPreferredTestQuery(DatabaseImpl.getInstance().getConnectionTestStatement()); 258 pooledDS.setTestConnectionOnCheckin(bValidateOnReturn); 260 pooledDS.setTestConnectionOnCheckout(bValidateOnBorrow); 261 263 return pooledDS; 264 } 265 266 269 protected void closeConnectionPool( 270 ConnectionPoolDefinition connectionpool 271 ) throws OSSException 272 { 273 try 274 { 275 ((PooledDataSource)connectionpool.getConnectionPool()).close(); 276 Thread.sleep(1000); 282 } 283 catch (Exception eExc) 284 { 285 throw new OSSDatabaseAccessException("Cannot close connection pool.", eExc); 287 } 288 } 289 } 290 | Popular Tags |