1 16 17 package org.springframework.transaction.jta; 18 19 import java.lang.reflect.InvocationTargetException ; 20 import java.lang.reflect.Method ; 21 22 import javax.transaction.NotSupportedException ; 23 import javax.transaction.SystemException ; 24 import javax.transaction.Transaction ; 25 import javax.transaction.UserTransaction ; 26 27 import org.springframework.transaction.TransactionDefinition; 28 import org.springframework.transaction.TransactionSystemException; 29 import org.springframework.util.ClassUtils; 30 31 67 public class OC4JJtaTransactionManager extends JtaTransactionManager { 68 69 private static final String TRANSACTION_UTILITY_CLASS_NAME = 70 "oracle.j2ee.transaction.TransactionUtility"; 71 72 private static final String TRANSACTION_MANAGER_CLASS_NAME = 73 "com.evermind.server.ApplicationServerTransactionManager"; 74 75 private static final String TRANSACTION_CLASS_NAME = 76 "com.evermind.server.ApplicationServerTransaction"; 77 78 79 private Method beginWithNameMethod; 80 81 private Method setTransactionIsolationMethod; 82 83 84 public void afterPropertiesSet() throws TransactionSystemException { 85 super.afterPropertiesSet(); 86 loadOC4JTransactionClasses(); 87 } 88 89 protected UserTransaction retrieveUserTransaction() throws TransactionSystemException { 90 try { 91 Class transactionUtilityClass = getClass().getClassLoader().loadClass(TRANSACTION_UTILITY_CLASS_NAME); 92 Method getInstanceMethod = transactionUtilityClass.getMethod("getInstance", new Class [0]); 93 Object transactionUtility = getInstanceMethod.invoke(null, new Object [0]); 94 logger.debug("Retrieving JTA UserTransaction from OC4J TransactionUtility"); 95 Method getUserTransactionMethod = 96 transactionUtility.getClass().getMethod("getOC4JUserTransaction", new Class [0]); 97 return (UserTransaction ) getUserTransactionMethod.invoke(transactionUtility, new Object [0]); 98 } 99 catch (ClassNotFoundException ex) { 100 logger.debug("Could not find OC4J 10.1.3.2 TransactionUtility: " + ex); 101 return null; 104 } 105 catch (InvocationTargetException ex) { 106 throw new TransactionSystemException( 107 "OC4J's TransactionUtility.getOC4JUserTransaction() method failed", ex.getTargetException()); 108 } 109 catch (Exception ex) { 110 throw new TransactionSystemException( 111 "Could not invoke OC4J's TransactionUtility.getOC4JUserTransaction() method", ex); 112 } 113 } 114 115 private void loadOC4JTransactionClasses() throws TransactionSystemException { 116 try { 117 Class transactionManagerClass = getClass().getClassLoader().loadClass(TRANSACTION_MANAGER_CLASS_NAME); 118 if (transactionManagerClass.isInstance(getUserTransaction())) { 119 Class transactionClass = getClass().getClassLoader().loadClass(TRANSACTION_CLASS_NAME); 120 this.beginWithNameMethod = ClassUtils.getMethodIfAvailable( 121 transactionManagerClass, "begin", new Class [] {String .class}); 122 this.setTransactionIsolationMethod = ClassUtils.getMethodIfAvailable( 123 transactionClass, "setTransactionIsolation", new Class [] {int.class}); 124 logger.info("Support for OC4J transaction names and isolation levels available"); 125 } 126 else { 127 logger.info("Support for OC4J transaction names and isolation levels not available"); 128 } 129 } 130 catch (Exception ex) { 131 throw new TransactionSystemException( 132 "Could not initialize OC4JJtaTransactionManager because OC4J API classes are not available", ex); 133 } 134 } 135 136 137 protected void doJtaBegin(JtaTransactionObject txObject, TransactionDefinition definition) 138 throws NotSupportedException , SystemException { 139 140 int timeout = determineTimeout(definition); 141 applyTimeout(txObject, timeout); 142 143 if (this.beginWithNameMethod != null && definition.getName() != null) { 145 150 try { 151 this.beginWithNameMethod.invoke(txObject.getUserTransaction(), new Object [] {definition.getName()}); 152 } 153 catch (InvocationTargetException ex) { 154 throw new TransactionSystemException( 155 "OC4J's UserTransaction.begin(String) method failed", ex.getTargetException()); 156 } 157 catch (Exception ex) { 158 throw new TransactionSystemException( 159 "Could not invoke OC4J's UserTransaction.begin(String) method", ex); 160 } 161 } 162 else { 163 txObject.getUserTransaction().begin(); 166 } 167 168 if (this.setTransactionIsolationMethod != null) { 170 if (definition.getIsolationLevel() != TransactionDefinition.ISOLATION_DEFAULT) { 171 try { 172 Transaction tx = getTransactionManager().getTransaction(); 173 178 Integer isolationLevel = new Integer (definition.getIsolationLevel()); 179 this.setTransactionIsolationMethod.invoke(tx, new Object [] {isolationLevel}); 180 } 181 catch (InvocationTargetException ex) { 182 throw new TransactionSystemException( 183 "OC4J's Transaction.setTransactionIsolation(int) method failed", ex.getTargetException()); 184 } 185 catch (Exception ex) { 186 throw new TransactionSystemException( 187 "Could not invoke OC4J's Transaction.setTransactionIsolation(int) method", ex); 188 } 189 } 190 } 191 else { 192 applyIsolationLevel(txObject, definition.getIsolationLevel()); 193 } 194 } 195 196 } 197 | Popular Tags |