1 21 22 package org.opensubsystems.core.persist.db.transaction; 23 24 import java.sql.Connection ; 25 import java.util.logging.Level ; 26 import java.util.logging.Logger ; 27 28 import javax.naming.InitialContext ; 29 import javax.naming.NameNotFoundException ; 30 import javax.naming.NamingException ; 31 import javax.transaction.TransactionManager ; 32 import javax.transaction.UserTransaction ; 33 34 import org.opensubsystems.core.error.OSSDatabaseAccessException; 35 import org.opensubsystems.core.error.OSSException; 36 import org.opensubsystems.core.persist.db.DatabaseConnectionFactoryImpl; 37 import org.opensubsystems.core.persist.db.DatabaseTransactionFactoryImpl; 38 import org.opensubsystems.core.persist.db.connectionpool.J2EEDatabaseConnectionFactoryImpl; 39 import org.opensubsystems.core.util.GlobalConstants; 40 import org.opensubsystems.core.util.Log; 41 42 51 public class J2EETransactionFactoryImpl extends DatabaseTransactionFactoryImpl 52 { 53 55 62 public static final String [] ARR_TRAN_MANAGER = {"java:/TransactionManager", 63 "java:comp/UserTransaction", 64 "javax.transaction.TransactionManager", 65 }; 66 67 73 public static final String [] ARR_USER_TRAN = {"UserTransaction", 74 "java:comp/UserTransaction", 75 }; 76 77 79 83 protected UserTransaction m_currentUserTransaction = null; 84 85 89 protected TransactionManager m_currentTransactionManager = null; 90 91 94 private static Logger s_logger = Log.getInstance(SimpleLocalTransactionFactoryImpl.class); 95 96 98 101 public UserTransaction requestTransaction( 102 ) throws OSSException 103 { 104 UserTransaction transaction = m_currentUserTransaction; 107 int iIndex = 0; 108 109 115 while ((iIndex < ARR_USER_TRAN.length) && (transaction == null)) 117 { 118 transaction = lookupUserTransaction(ARR_USER_TRAN[iIndex]); 119 if (transaction != null) 120 { 121 m_currentUserTransaction = transaction; 122 break; 123 } 124 iIndex++; 125 } 126 127 if (transaction == null) 128 { 129 s_logger.log(Level.FINE, "Cannot get UserTransaction because " + 131 "unknown or unsupported J2EE server is running."); 132 } 133 else 134 { 135 if (s_bTransactionMonitor) 136 { 137 transaction = new DelegatingUserTransaction(transaction); 138 } 139 } 140 141 return transaction; 143 } 144 145 148 public TransactionManager getTransactionManager( 149 ) 150 { 151 TransactionManager actualTM = m_currentTransactionManager; 154 int iIndex = 0; 155 156 162 while ((iIndex < ARR_TRAN_MANAGER.length) && (actualTM == null)) 164 { 165 actualTM = lookupTransactionManager(ARR_TRAN_MANAGER[iIndex]); 166 if (actualTM != null) 167 { 168 m_currentTransactionManager = actualTM; 169 break; 170 } 171 iIndex++; 172 } 173 174 if (actualTM == null) 175 { 176 s_logger.log(Level.FINE, "Cannot get TransactionManager because " + 178 "unknown or unsupported J2EE server is running."); 179 } 180 181 return actualTM; 183 } 184 185 188 public void reset( 189 ) throws OSSException 190 { 191 } 194 195 198 public void stop( 199 ) throws OSSException 200 { 201 } 204 205 207 213 protected UserTransaction lookupUserTransaction( 214 String userTransactionName 215 ) 216 { 217 Object jndiObject = null; 218 UserTransaction userTran = null; 219 InitialContext context = null; 220 221 s_logger.finest("Looking up UserTransaction at location " 222 + userTransactionName); 223 224 try 231 { 232 context = new InitialContext (); 233 jndiObject = context.lookup(userTransactionName); 234 235 if (jndiObject instanceof UserTransaction ) 236 { 237 userTran = (UserTransaction ) jndiObject; 238 s_logger.fine("UserTransaction found at " + userTransactionName); 239 } 240 else 241 { 242 s_logger.log(Level.WARNING, "Object " + jndiObject 245 + " available at JNDI location " + userTransactionName 246 + " does not implement javax.transaction.UserTransaction." 247 + " This may be caused by class loading conflict between" 248 + " multiple jar files containing this interface."); 249 } 250 } 251 catch (NameNotFoundException nnfExc) 252 { 253 s_logger.log(Level.FINEST, "UserTransaction is not available at " + 257 "JNDI location " + userTransactionName); 258 } 259 catch (NamingException nExc) 260 { 261 s_logger.log(Level.FINEST, "UserTransaction is not available at " + 265 "JNDI location " + userTransactionName); 266 } 267 finally 268 { 269 try 270 { 271 context.close(); 272 } 273 catch (NamingException nExc) 274 { 275 s_logger.log(Level.FINE, "Unable to close context for user transaction", nExc); 276 } 277 } 278 279 return userTran; 280 } 281 282 288 protected TransactionManager lookupTransactionManager( 289 String transactionManagerName 290 ) 291 { 292 Object jndiObject = null; 293 TransactionManager tranManager = null; 294 InitialContext context = null; 295 296 s_logger.finest("Looking up TransactionManager at location " 297 + transactionManagerName); 298 299 try 306 { 307 context = new InitialContext (); 308 jndiObject = context.lookup(transactionManagerName); 309 310 if (jndiObject instanceof TransactionManager ) 311 { 312 tranManager = (TransactionManager ) jndiObject; 313 s_logger.fine("TransactionManager found at " + transactionManagerName); 314 } 315 else 316 { 317 s_logger.log(Level.WARNING, "Object " + jndiObject 320 + " available at JNDI location " + transactionManagerName 321 + " does not implement javax.transaction.TransactionManager" 322 + " This may be caused by class loading conflict between" 323 + " multiple jar files containing this interface."); 324 } 325 } 326 catch (NameNotFoundException nnfExc) 327 { 328 s_logger.log(Level.FINEST, "TransactionManager is not available at " + 332 "JNDI location " + transactionManagerName); 333 } 334 catch (NamingException nExc) 335 { 336 s_logger.log(Level.FINEST, "TransactionManager is not available at " + 340 "JNDI location " + transactionManagerName); 341 } 342 finally 343 { 344 try 345 { 346 context.close(); 347 } 348 catch (NamingException nExc) 349 { 350 s_logger.log(Level.FINE, "Unable to close context for transaction manager", nExc); 351 } 352 } 353 354 return tranManager; 355 } 356 357 360 protected Connection requestTransactionalConnection( 361 boolean bAutoCommit, 362 String strDataSourceName, 363 String strUser, 364 String strPassword, 365 DatabaseConnectionFactoryImpl connectionFactory 366 ) throws OSSDatabaseAccessException 367 { 368 if (GlobalConstants.ERROR_CHECKING) 369 { 370 assert connectionFactory instanceof J2EEDatabaseConnectionFactoryImpl 375 : "J2EE transaction factory can manage only J2EE connections" + 376 " at this time."; 377 } 378 379 return super.requestTransactionalConnection(bAutoCommit, strDataSourceName, 382 strUser, strPassword, 383 connectionFactory); 384 } 385 386 387 390 protected void returnTransactionalConnection( 391 Connection cntDBConnection, 392 DatabaseConnectionFactoryImpl connectionFactory 393 ) 394 { 395 if (GlobalConstants.ERROR_CHECKING) 396 { 397 assert connectionFactory instanceof J2EEDatabaseConnectionFactoryImpl 402 : "J2EE TM can manage only J2EE CF connections at this time."; 403 } 404 405 super.returnTransactionalConnection(cntDBConnection, connectionFactory); 407 } 408 } 409 | Popular Tags |