1 21 22 package org.opensubsystems.core.persist.db.connectionpool; 23 24 import java.sql.Connection ; 25 import java.sql.SQLException ; 26 27 import org.enhydra.jdbc.pool.StandardXAPoolDataSource; 28 import org.enhydra.jdbc.standard.StandardXADataSource; 29 import org.opensubsystems.core.error.OSSConfigException; 30 import org.opensubsystems.core.error.OSSDatabaseAccessException; 31 import org.opensubsystems.core.error.OSSException; 32 import org.opensubsystems.core.persist.db.DatabaseImpl; 33 import org.opensubsystems.core.persist.db.DatabaseTransactionFactoryImpl; 34 35 44 public class XAPoolDatabaseConnectionFactoryImpl extends PooledDatabaseConnectionFactoryImpl 45 { 46 48 54 public XAPoolDatabaseConnectionFactoryImpl( 55 ) throws OSSException 56 { 57 super(); 58 59 m_transactionFactory = null; 64 65 loadDefaultDatabaseProperties(); 67 68 } 73 74 83 public XAPoolDatabaseConnectionFactoryImpl( 84 DatabaseTransactionFactoryImpl transactionFactory 85 ) throws OSSConfigException, 86 OSSDatabaseAccessException 87 { 88 super(transactionFactory); 89 90 loadDefaultDatabaseProperties(); 92 93 } 98 99 111 public XAPoolDatabaseConnectionFactoryImpl( 112 String strDriver, 113 String strURL, 114 String strUser, 115 String strPassword, 116 DatabaseTransactionFactoryImpl transactionFactory 117 ) throws OSSConfigException, 118 OSSDatabaseAccessException 119 { 120 super(strDriver, strURL, strUser, strPassword, transactionFactory); 121 122 } 127 128 130 133 protected Connection getPooledConnection( 134 ConnectionPoolDefinition connectionpool 135 ) throws OSSDatabaseAccessException 136 { 137 Connection conReturn; 138 139 try 140 { 141 conReturn = (Connection )((StandardXAPoolDataSource) 142 connectionpool.getConnectionPool()).getConnection(); 143 } 144 catch (Exception eExc) 145 { 146 throw new OSSDatabaseAccessException("Cannot get database connection from pool.", 148 eExc); 149 } 150 151 return conReturn; 152 } 153 154 157 protected Connection getPooledConnection( 158 ConnectionPoolDefinition connectionpool, 159 String strUser, 160 String strPassword 161 ) throws OSSDatabaseAccessException 162 { 163 Connection conReturn; 164 165 try 166 { 167 conReturn = (Connection )((StandardXAPoolDataSource) 168 connectionpool.getConnectionPool()).getConnection(strUser, 169 strPassword); 170 } 171 catch (Exception eExc) 172 { 173 throw new OSSDatabaseAccessException("Cannot get database connection from pool.", 175 eExc); 176 } 177 178 return conReturn; 179 } 180 181 184 protected Object createConnectionPool( 185 String strConnectionPoolName, 186 String strDriverName, 187 String strUrl, 188 String strUser, 189 String strPassword 190 ) throws OSSException 191 { 192 PooledDatabaseConnectionFactorySetupReader setupReader 193 = new PooledDatabaseConnectionFactorySetupReader(strConnectionPoolName); 194 195 int iInitialPoolSize = setupReader.getIntegerParameterValue( 196 PooledDatabaseConnectionFactorySetupReader.DBPOOL_INITIAL_SIZE).intValue(); 197 int iMinimalPoolSize = setupReader.getIntegerParameterValue( 198 PooledDatabaseConnectionFactorySetupReader.DBPOOL_MIN_SIZE).intValue(); 199 int iMaximalPoolSize = setupReader.getIntegerParameterValue( 200 PooledDatabaseConnectionFactorySetupReader.DBPOOL_MAX_SIZE).intValue(); 201 long lMaxWaitTimeForConnection = setupReader.getLongParameterValue( 204 PooledDatabaseConnectionFactorySetupReader.DBPOOL_WAIT_PERIOD).longValue(); 205 long lRetryTimeForConnection = setupReader.getLongParameterValue( 206 PooledDatabaseConnectionFactorySetupReader.DBPOOL_RETRY_PERIOD).longValue(); 207 int iCheckLevel = setupReader.getIntegerParameterValue( 208 PooledDatabaseConnectionFactorySetupReader.DBPOOL_CHECK_LEVEL).intValue(); 209 int iTransactionIsolation 210 = DatabaseImpl.getInstance().getTransactionIsolation( 211 PooledDatabaseConnectionFactorySetupReader.convertTransactionIsolationToConstant( 212 setupReader.getStringParameterValue( 213 PooledDatabaseConnectionFactorySetupReader.DBPOOL_TRANSACTION_ISOLATION 214 ).toString())); 215 int iPreparedStatementCacheSize = setupReader.getIntegerParameterValue( 216 PooledDatabaseConnectionFactorySetupReader.DBPOOL_PREPSTATEMENT_CACHE_SIZE 217 ).intValue(); 218 219 StandardXADataSource xadsDataSource; 220 221 if (iPreparedStatementCacheSize < 0) 223 { 224 iPreparedStatementCacheSize = 0; 225 } 226 227 xadsDataSource = new StandardXADataSource(); 229 try 230 { 231 xadsDataSource.setDriverName(strDriverName); 232 } 233 catch (SQLException sqleExc) 234 { 235 throw new OSSDatabaseAccessException("Unexpected error when creating" + 236 " connection pool.", sqleExc); 237 } 238 xadsDataSource.setUrl(strUrl); 239 xadsDataSource.setUser(strUser); 240 xadsDataSource.setPassword(strPassword); 241 xadsDataSource.setPreparedStmtCacheSize(iPreparedStatementCacheSize); 242 if (iTransactionIsolation >= 0) 243 { 244 xadsDataSource.setTransactionIsolation(iTransactionIsolation); 246 } 247 248 if (m_transactionFactory != null) 249 { 250 xadsDataSource.setTransactionManager(m_transactionFactory.getTransactionManager()); 251 } 252 253 StandardXAPoolDataSource xapdsPoolDataSource; 254 255 xapdsPoolDataSource = new StandardXAPoolDataSource(iInitialPoolSize); 257 xapdsPoolDataSource.setDataSourceName("XA" + strConnectionPoolName); 258 xapdsPoolDataSource.setDataSource(xadsDataSource); 259 if (m_transactionFactory != null) 260 { 261 xapdsPoolDataSource.setTransactionManager(m_transactionFactory.getTransactionManager()); 262 } 263 xapdsPoolDataSource.setUser(strUser); 264 xapdsPoolDataSource.setPassword(strPassword); 265 266 try 269 { 270 xapdsPoolDataSource.setMinSize(iMinimalPoolSize); 271 xapdsPoolDataSource.setMaxSize(iMaximalPoolSize); 272 } 273 catch (Exception eExc) 274 { 275 throw new OSSDatabaseAccessException("Unexpected error when creating" + 276 " connection pool.", eExc); 277 } 278 xapdsPoolDataSource.setDeadLockMaxWait(lMaxWaitTimeForConnection); 279 xapdsPoolDataSource.setDeadLockRetryWait(lRetryTimeForConnection); 280 xapdsPoolDataSource.setCheckLevelObject(iCheckLevel); 281 xapdsPoolDataSource.setJdbcTestStmt( 282 DatabaseImpl.getInstance().getConnectionTestStatement()); 283 284 return xapdsPoolDataSource; 285 } 286 287 290 protected void closeConnectionPool( 291 ConnectionPoolDefinition connectionpool 292 ) throws OSSException 293 { 294 try 295 { 296 ((StandardXAPoolDataSource)connectionpool.getConnectionPool()).shutdown(true); 297 } 298 catch (Exception eExc) 299 { 300 throw new OSSDatabaseAccessException("Cannot close connection pool.", eExc); 302 } 303 } 304 } 305 | Popular Tags |