1 2 18 19 22 package org.quartz.ee.jta; 23 24 import javax.naming.InitialContext ; 25 import javax.transaction.HeuristicMixedException ; 26 import javax.transaction.HeuristicRollbackException ; 27 import javax.transaction.NotSupportedException ; 28 import javax.transaction.RollbackException ; 29 import javax.transaction.SystemException ; 30 import javax.transaction.UserTransaction ; 31 32 import org.apache.commons.logging.Log; 33 import org.apache.commons.logging.LogFactory; 34 import org.quartz.SchedulerException; 35 36 48 public class UserTransactionHelper { 49 50 57 58 public static final String DEFAULT_USER_TX_LOCATION = "java:comp/UserTransaction"; 59 60 67 68 private static String userTxURL = DEFAULT_USER_TX_LOCATION; 69 70 77 78 81 private UserTransactionHelper() { 82 } 83 84 91 92 public static String getUserTxLocation() { 93 return userTxURL; 94 } 95 96 101 public static void setUserTxLocation(String userTxURL) { 102 if (userTxURL != null) { 103 UserTransactionHelper.userTxURL = userTxURL; 104 } 105 } 106 107 111 public static UserTransaction lookupUserTransaction() throws SchedulerException { 112 return new UserTransactionWithContext(); 113 } 114 115 120 public static void returnUserTransaction(UserTransaction userTransaction) { 121 if ((userTransaction != null) && 122 (userTransaction instanceof UserTransactionWithContext)) { 123 UserTransactionWithContext userTransactionWithContext = 124 (UserTransactionWithContext)userTransaction; 125 126 userTransactionWithContext.closeContext(); 127 } 128 } 129 130 135 private static class UserTransactionWithContext implements UserTransaction { 136 InitialContext context; 137 UserTransaction userTransaction; 138 139 public UserTransactionWithContext() throws SchedulerException { 140 try { 141 context = new InitialContext (); 142 } catch (Throwable t) { 143 throw new SchedulerException( 144 "UserTransactionHelper failed to create InitialContext to lookup/create UserTransaction.", t); 145 } 146 147 try { 148 userTransaction = (UserTransaction )context.lookup(userTxURL); 149 } catch (Throwable t) { 150 closeContext(); 151 throw new SchedulerException( 152 "UserTransactionHelper could not lookup/create UserTransaction.", 153 t); 154 } 155 156 if (userTransaction == null) { 157 closeContext(); 158 throw new SchedulerException( 159 "UserTransactionHelper could not lookup/create UserTransaction from the InitialContext."); 160 } 161 } 162 163 167 public void closeContext() { 168 try { 169 if (context != null) { 170 context.close(); 171 } 172 } catch (Throwable t) { 173 getLog().warn("Failed to close InitialContext used to get a UserTransaction.", t); 174 } 175 context = null; 176 } 177 178 182 protected void finalize() throws Throwable { 183 try { 184 if (context != null) { 185 getLog().warn("UserTransaction was never returned to the UserTransactionHelper."); 186 closeContext(); 187 } 188 } finally { 189 super.finalize(); 190 } 191 } 192 193 private static Log getLog() { 194 return LogFactory.getLog(UserTransactionWithContext.class); 195 } 196 197 199 public void begin() throws NotSupportedException , SystemException { 200 userTransaction.begin(); 201 } 202 203 public void commit() throws RollbackException , HeuristicMixedException , HeuristicRollbackException , SecurityException , IllegalStateException , SystemException { 204 userTransaction.commit(); 205 } 206 207 public void rollback() throws IllegalStateException , SecurityException , SystemException { 208 userTransaction.rollback(); 209 } 210 211 public void setRollbackOnly() throws IllegalStateException , SystemException { 212 userTransaction.setRollbackOnly(); 213 } 214 215 public int getStatus() throws SystemException { 216 return userTransaction.getStatus(); 217 } 218 219 public void setTransactionTimeout(int seconds) throws SystemException { 220 userTransaction.setTransactionTimeout(seconds); 221 } 222 } 223 } | Popular Tags |