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 import java.util.Properties ; 28 29 import org.logicalcobwebs.proxool.ProxoolConstants; 30 import org.logicalcobwebs.proxool.ProxoolException; 31 import org.logicalcobwebs.proxool.ProxoolFacade; 32 import org.opensubsystems.core.error.OSSConfigException; 33 import org.opensubsystems.core.error.OSSDatabaseAccessException; 34 import org.opensubsystems.core.error.OSSException; 35 import org.opensubsystems.core.persist.db.DatabaseImpl; 36 import org.opensubsystems.core.persist.db.DatabaseTransactionFactoryImpl; 37 38 56 public class ProxoolDatabaseConnectionFactoryImpl extends PooledDatabaseConnectionFactoryImpl 57 { 58 60 67 public ProxoolDatabaseConnectionFactoryImpl( 68 ) throws OSSConfigException, 69 OSSDatabaseAccessException 70 { 71 super(); 72 73 loadDefaultDatabaseProperties(); 75 76 } 81 82 91 public ProxoolDatabaseConnectionFactoryImpl( 92 DatabaseTransactionFactoryImpl transactionFactory 93 ) throws OSSConfigException, 94 OSSDatabaseAccessException 95 { 96 super(transactionFactory); 97 98 loadDefaultDatabaseProperties(); 100 101 } 106 107 119 public ProxoolDatabaseConnectionFactoryImpl( 120 String strDriver, 121 String strURL, 122 String strUser, 123 String strPassword, 124 DatabaseTransactionFactoryImpl transactionFactory 125 ) throws OSSConfigException, 126 OSSDatabaseAccessException 127 { 128 super(strDriver, strURL, strUser, strPassword, transactionFactory); 129 130 } 135 136 138 141 protected Connection getPooledConnection( 142 ConnectionPoolDefinition connectionpool 143 ) throws OSSDatabaseAccessException 144 { 145 Connection conReturn; 146 147 try 148 { 149 conReturn = DriverManager.getConnection(connectionpool.m_connectionPool.toString()); 150 } 151 catch (Exception eExc) 152 { 153 throw new OSSDatabaseAccessException("Cannot get database connection from pool.", 155 eExc); 156 } 157 158 return conReturn; 159 } 160 161 164 protected Connection getPooledConnection( 165 ConnectionPoolDefinition connectionpool, 166 String strUser, 167 String strPassword 168 ) throws OSSDatabaseAccessException 169 { 170 Connection cntDBConnection; 171 172 try 174 { 175 cntDBConnection = DriverManager.getConnection(connectionpool.m_connectionPool.toString(), 176 strUser, 177 strPassword); 178 if (cntDBConnection != null) 179 { 180 m_mpNotPooledConnections.put(cntDBConnection, cntDBConnection); 181 } 182 } 183 catch (SQLException sqleExc) 184 { 185 throw new OSSDatabaseAccessException("Cannot get database connection" + 186 " for explicitely specified user.", 187 sqleExc); 188 } 189 190 return cntDBConnection; 191 } 192 193 196 protected Object createConnectionPool( 197 String strConnectionPoolName, 198 String strDriverName, 199 String strUrl, 200 String strUser, 201 String strPassword 202 ) throws OSSException 203 { 204 PooledDatabaseConnectionFactorySetupReader setupReader 205 = new PooledDatabaseConnectionFactorySetupReader(strConnectionPoolName); 206 207 int iMinimalPoolSize = setupReader.getIntegerParameterValue( 208 PooledDatabaseConnectionFactorySetupReader.DBPOOL_MIN_SIZE).intValue(); 209 int iMaximalPoolSize = setupReader.getIntegerParameterValue( 210 PooledDatabaseConnectionFactorySetupReader.DBPOOL_MAX_SIZE).intValue(); 211 boolean bValidateOnBorrow = setupReader.getBooleanParameterValue( 221 PooledDatabaseConnectionFactorySetupReader.DBPOOL_VALIDATE_BORROW).booleanValue(); 222 boolean bValidateOnReturn = setupReader.getBooleanParameterValue( 223 PooledDatabaseConnectionFactorySetupReader.DBPOOL_VALIDATE_RETURN).booleanValue(); 224 long lTimeBetweenEvictionRunsMillis = setupReader.getLongParameterValue( 225 PooledDatabaseConnectionFactorySetupReader.DBPOOL_IDLE_CHECK_PERIOD).longValue(); 226 236 try 237 { 238 Class.forName("org.logicalcobwebs.proxool.ProxoolDriver"); 239 } 240 catch (ClassNotFoundException cnfeExc) 241 { 242 throw new OSSDatabaseAccessException("Cannot found proxool driver " + 243 "'org.logicalcobwebs.proxool.ProxoolDriver'.", 244 cnfeExc); 245 } 246 247 Properties info = new Properties (); 248 249 info.setProperty(ProxoolConstants.HOUSE_KEEPING_SLEEP_TIME_PROPERTY, 255 Integer.toString((int)lTimeBetweenEvictionRunsMillis)); 256 info.setProperty(ProxoolConstants.HOUSE_KEEPING_TEST_SQL_PROPERTY, 257 DatabaseImpl.getInstance().getConnectionTestStatement()); 258 info.setProperty(ProxoolConstants.MAXIMUM_ACTIVE_TIME_PROPERTY, 268 Integer.toString(Integer.MAX_VALUE)); 269 info.setProperty(ProxoolConstants.MAXIMUM_CONNECTION_COUNT_PROPERTY, 270 Integer.toString(iMaximalPoolSize)); 271 info.setProperty(ProxoolConstants.MAXIMUM_CONNECTION_LIFETIME_PROPERTY, 274 Integer.toString(Integer.MAX_VALUE)); 275 info.setProperty(ProxoolConstants.MINIMUM_CONNECTION_COUNT_PROPERTY, 276 Integer.toString(iMinimalPoolSize)); 277 info.setProperty(ProxoolConstants.TEST_BEFORE_USE_PROPERTY, 284 Boolean.toString(bValidateOnBorrow)); 285 info.setProperty(ProxoolConstants.TEST_AFTER_USE_PROPERTY, 286 Boolean.toString(bValidateOnReturn)); 287 info.setProperty(ProxoolConstants.USER_PROPERTY, strUser); 288 info.setProperty(ProxoolConstants.PASSWORD_PROPERTY, strPassword); 289 292 StringBuffer sbProxoolURL = new StringBuffer (); 293 294 sbProxoolURL.append("proxool."); 295 sbProxoolURL.append(strConnectionPoolName); 296 sbProxoolURL.append(":"); 297 sbProxoolURL.append(strDriverName); 298 sbProxoolURL.append(":"); 299 sbProxoolURL.append(strUrl); 300 301 try 302 { 303 ProxoolFacade.registerConnectionPool(sbProxoolURL.toString(), info); 304 } 305 catch (ProxoolException peExc) 306 { 307 throw new OSSDatabaseAccessException("Unexpected error when registering" + 308 " connection pool.", peExc); 309 } 310 311 return sbProxoolURL.toString(); 312 } 313 314 317 protected void closeConnectionPool( 318 ConnectionPoolDefinition connectionpool 319 ) throws OSSException 320 { 321 try 322 { 323 ProxoolFacade.removeConnectionPool(connectionpool.getConnectionPoolName()); 324 } 325 catch (Exception eExc) 326 { 327 throw new OSSDatabaseAccessException("Cannot close connection pool.", eExc); 329 } 330 } 331 } 332 | Popular Tags |