1 28 29 package com.caucho.transaction; 30 31 import com.caucho.log.Log; 32 import com.caucho.util.L10N; 33 34 import javax.naming.InitialContext ; 35 import javax.transaction.Status ; 36 import javax.transaction.Transaction ; 37 import javax.transaction.TransactionManager ; 38 import javax.transaction.UserTransaction ; 39 import java.util.logging.Level ; 40 import java.util.logging.Logger ; 41 42 45 public class TransactionContainer { 46 private static final L10N L = new L10N(TransactionContainer.class); 47 private static final Logger log = Log.open(TransactionContainer.class); 48 49 private static TransactionContainer _container; 50 51 private UserTransaction _userTM; 52 private TransactionManager _tm; 53 54 public static TransactionContainer getTransactionContainer() 55 { 56 if (_container == null) { 57 _container = new TransactionContainer(); 58 59 try { 60 InitialContext ic = new InitialContext (); 61 62 UserTransaction userTM; 63 userTM = (UserTransaction ) ic.lookup("java:comp/UserTransaction"); 64 65 _container.setUserTransaction(userTM); 66 67 TransactionManager tm; 68 tm = (TransactionManager ) ic.lookup("java:comp/TransactionManager"); 69 70 _container.setTransactionManager(tm); 71 } catch (Throwable e) { 72 log.log(Level.WARNING, e.toString(), e); 73 } 74 } 75 76 return _container; 77 } 78 79 82 public void setUserTransaction(UserTransaction userTM) 83 { 84 _userTM = userTM; 85 } 86 87 90 public void setTransactionManager(TransactionManager tm) 91 { 92 _tm = tm; 93 } 94 95 102 public Transaction beginRequired() 103 { 104 try { 105 Transaction currentTrans = _tm.getTransaction(); 106 107 if (currentTrans != null) 108 return currentTrans; 109 110 _userTM.begin(); 112 113 return null; 114 } catch (RuntimeException e) { 115 throw e; 116 } catch (Exception e) { 117 throw new TransactionRuntimeException(e); 118 } 119 } 120 121 127 public Transaction beginRequiresNew() 128 { 129 try { 130 Transaction oldTrans = _tm.getTransaction(); 131 132 if (oldTrans != null) 133 oldTrans = _tm.suspend(); 134 135 _userTM.begin(); 137 138 return oldTrans; 139 } catch (RuntimeException e) { 140 throw e; 141 } catch (Exception e) { 142 throw new TransactionRuntimeException(e); 143 } 144 } 145 146 151 public void beginMandatory() 152 { 153 try { 154 Transaction oldTrans = _tm.getTransaction(); 155 156 if (oldTrans == null) 157 throw new TransactionRuntimeException(L.l("'Mandatory' transaction boundary requires a transaction.")); 158 } catch (RuntimeException e) { 159 throw e; 160 } catch (Exception e) { 161 throw new TransactionRuntimeException(e); 162 } 163 } 164 165 168 public void beginNever() 169 { 170 try { 171 Transaction oldTrans = _tm.getTransaction(); 172 173 if (oldTrans != null) 174 throw new TransactionRuntimeException(L.l("'Never' transaction boundary must not have a transaction.")); 175 } catch (RuntimeException e) { 176 throw e; 177 } catch (Exception e) { 178 throw new TransactionRuntimeException(e); 179 } 180 } 181 182 187 public Transaction beginSuspend() 188 { 189 try { 190 Transaction oldTrans = _tm.getTransaction(); 191 192 if (oldTrans != null) 193 oldTrans = _tm.suspend(); 194 195 return oldTrans; 196 } catch (RuntimeException e) { 197 throw e; 198 } catch (Exception e) { 199 throw new TransactionRuntimeException(e); 200 } 201 } 202 203 206 public void setRollbackOnly(Throwable e) 207 { 208 } 209 210 213 public void commit(Transaction oldTransaction) 214 { 215 try { 216 Transaction currentTrans = _tm.getTransaction(); 217 218 if (currentTrans == null) { 219 } 220 else if (currentTrans.getStatus() != Status.STATUS_MARKED_ROLLBACK) 221 _userTM.commit(); 222 else 223 _userTM.rollback(); 224 } catch (RuntimeException e) { 225 throw e; 226 } catch (Exception e) { 227 throw new TransactionRuntimeException(e); 228 } finally { 229 if (oldTransaction != null) { 230 try { 231 _tm.resume(oldTransaction); 232 } catch (Exception e) { 233 throw new TransactionRuntimeException(e); 234 } 235 } 236 } 237 } 238 239 242 public void rollback(Transaction oldTransaction) 243 { 244 try { 245 Transaction currentTrans = _tm.getTransaction(); 246 247 if (currentTrans != null) 248 _userTM.rollback(); 249 } catch (RuntimeException e) { 250 throw e; 251 } catch (Exception e) { 252 throw new TransactionRuntimeException(e); 253 } finally { 254 if (oldTransaction != null) { 255 try { 256 _tm.resume(oldTransaction); 257 } catch (Exception e) { 258 throw new TransactionRuntimeException(e); 259 } 260 } 261 } 262 } 263 } 264 | Popular Tags |