1 21 22 package org.opensubsystems.core.persist.db.connectionpool; 23 24 import java.sql.Connection ; 25 import java.sql.SQLException ; 26 import java.util.logging.Level ; 27 import java.util.logging.Logger ; 28 29 import javax.naming.InitialContext ; 30 import javax.naming.NamingException ; 31 import javax.sql.DataSource ; 32 import javax.transaction.SystemException ; 33 34 import org.opensubsystems.core.error.OSSDatabaseAccessException; 35 import org.opensubsystems.core.error.OSSException; 36 import org.opensubsystems.core.util.J2EEUtils; 37 import org.opensubsystems.core.util.Log; 38 39 48 public class J2EEDatabaseConnectionFactoryImpl extends PooledDatabaseConnectionFactoryImpl 49 { 50 52 55 private static Logger s_logger = Log.getInstance(J2EEDatabaseConnectionFactoryImpl.class); 56 57 61 public static final String DATASOURCE_NAME_PREFIX_JBOSS = "java:/"; 62 63 76 public static final String DATASOURCE_NAME_PREFIX_WEBSPHERE = "jdbc/"; 77 78 80 83 public String getRealDatabaseDriver( 84 ) 85 { 86 return super.getRealDatabaseDriver(); 92 } 93 94 97 public String getDatabaseDriver( 98 ) 99 { 100 return super.getDatabaseDriver(); 104 } 105 106 109 public String getDatabaseURL( 110 ) 111 { 112 return super.getDatabaseURL(); 116 } 117 118 121 public String getDatabaseAdminUser( 122 ) 123 { 124 return super.getDatabaseAdminUser(); 126 } 127 128 131 public String getDatabaseAdminPassword( 132 ) 133 { 134 return super.getDatabaseAdminPassword(); 136 } 137 138 141 public String getDatabaseUser( 142 ) 143 { 144 return super.getDatabaseUser(); 148 } 149 150 153 public String getDatabasePassword( 154 ) 155 { 156 return super.getDatabasePassword(); 158 } 159 160 162 165 protected Connection getPooledConnection( 166 ConnectionPoolDefinition connectionpool 167 ) throws OSSDatabaseAccessException 168 { 169 Connection conReturn; 170 171 try 172 { 173 conReturn = ((DataSource )connectionpool.getConnectionPool()).getConnection(); 174 } 175 catch (SQLException sqlExc) 176 { 177 throw new OSSDatabaseAccessException("Cannot get database connection from pool.", 178 sqlExc); 179 } 180 181 return conReturn; 182 } 183 184 187 protected Connection getPooledConnection( 188 ConnectionPoolDefinition connectionpool, 189 String strUser, 190 String strPassword 191 ) throws OSSDatabaseAccessException 192 { 193 Connection conReturn; 194 195 try 196 { 197 DataSource source; 198 199 if (connectionpool != null) 200 { 201 source = (DataSource )connectionpool.getConnectionPool(); 202 if (source != null) 203 { 204 conReturn = source.getConnection(strUser, strPassword); 205 } 206 else 207 { 208 throw new OSSDatabaseAccessException( 211 "Connection pool " 212 + connectionpool.getConnectionPoolName() 213 + " doesn't exist. Maybe it wasn't initialized yet."); 214 } 215 } 216 else 217 { 218 throw new OSSDatabaseAccessException("Connection pool doesn't exist." 221 + " Maybe it wasn't initialized yet."); 222 } 223 } 224 catch (SQLException sqlExc) 225 { 226 throw new OSSDatabaseAccessException( 227 "Cannot get database connection from pool for specified" 228 + " user/password.", sqlExc); 229 } 230 231 return conReturn; 232 } 233 234 237 protected Object createConnectionPool( 238 String strConnectionPoolName, 239 String strDriverName, 240 String strUrl, 241 String strUser, 242 String strPassword 243 ) throws OSSException 244 { 245 InitialContext context = null; 249 DataSource dsDataSource = null; 250 StringBuffer dataSourceName = new StringBuffer (); 251 252 try 253 { 254 255 int iActualServerType = J2EEUtils.getJ2EEServerType(); 256 257 switch (iActualServerType) 258 { 259 case J2EEUtils.J2EE_SERVER_JBOSS: 260 { 261 dataSourceName.append(DATASOURCE_NAME_PREFIX_JBOSS); 263 break; 264 } 265 case J2EEUtils.J2EE_SERVER_WEBSPHERE: 266 { 267 dataSourceName.append(DATASOURCE_NAME_PREFIX_WEBSPHERE); 269 break; 270 } 271 default: 272 { 273 break; 276 } 277 } 278 279 dataSourceName.append(strConnectionPoolName); 280 281 s_logger.finest("Looking up datasource " + dataSourceName.toString()); 282 283 context = new InitialContext (); 285 dsDataSource = (DataSource ) context.lookup(dataSourceName.toString()); 286 287 s_logger.fine("Found datasource " + dataSourceName.toString()); 288 291 } 293 catch (NamingException neExc) 294 { 295 s_logger.finest("Datasource " + dataSourceName + " not found."); 296 throw new OSSDatabaseAccessException( 301 "Error occured while looking up data source.", 302 neExc); 303 } 304 finally 305 { 306 try 307 { 308 context.close(); 309 } 310 catch (NamingException nExc) 311 { 312 s_logger.log(Level.WARNING, "Unable to close context", nExc); 313 } 314 } 315 316 return dsDataSource; 317 } 318 319 322 protected void closeConnectionPool( 323 ConnectionPoolDefinition connectionpool 324 ) throws OSSException 325 { 326 } 330 331 334 protected void initializeConnection( 335 Connection cntDBConnection, 336 boolean bAutoCommit 337 ) throws SQLException 338 { 339 int iActiveServerType = J2EEUtils.getJ2EEServerType(); 340 341 if (iActiveServerType == J2EEUtils.J2EE_SERVER_JBOSS) 342 { 343 try 344 { 345 if (!m_transactionFactory.isTransactionInProgress()) 349 { 350 super.initializeConnection(cntDBConnection, bAutoCommit); 351 } 352 else 353 { 354 s_logger.finest("Ignoring request to set autocommit to " 355 + bAutoCommit + " since we are running inside of" 356 + " JBoss and transaction is in progress."); 357 } 358 } 359 catch (SystemException sExc) 360 { 361 throw new SQLException ("Error while getting transaction status."); 362 } 363 catch (OSSException ossExc) 364 { 365 throw new SQLException ("Error while getting transaction status."); 366 } 367 } 368 else 369 { 370 super.initializeConnection(cntDBConnection, bAutoCommit); 371 } 372 } 373 } 374 | Popular Tags |