1 6 7 package org.jfox.tm; 8 9 import java.util.Timer ; 10 import java.util.TimerTask ; 11 import javax.transaction.HeuristicMixedException ; 12 import javax.transaction.HeuristicRollbackException ; 13 import javax.transaction.InvalidTransactionException ; 14 import javax.transaction.NotSupportedException ; 15 import javax.transaction.RollbackException ; 16 import javax.transaction.Status ; 17 import javax.transaction.Synchronization ; 18 import javax.transaction.SystemException ; 19 import javax.transaction.Transaction ; 20 import javax.transaction.xa.XAResource ; 21 22 27 28 class TxWrapper implements Transaction { 29 32 private static Timer timer = new Timer (true); 33 private static int timeout = 10 * 24 * 60 * 60 * 1000; 35 private TransactionImpl transaction = null; 36 37 TxWrapper() { 38 39 } 40 41 45 void begin() throws SystemException , NotSupportedException { 46 transaction = new TransactionImpl(); 47 timer.schedule((TimerTask ) transaction, timeout); 48 } 49 50 void setTimeout(int millisecond) { 51 timeout = millisecond; 52 } 53 54 55 64 public void commit() throws RollbackException , HeuristicMixedException , HeuristicRollbackException , SecurityException , SystemException { 65 if(transaction == null) { 66 throw new IllegalStateException ("no transaction associated with current thread."); 67 } 68 try { 69 transaction.commit(); 70 } 71 finally { ((TimerTask ) transaction).cancel(); 73 } 74 } 75 76 public boolean delistResource(XAResource xaResource, int flag) throws IllegalStateException , SystemException { 77 if(transaction == null) { 78 throw new IllegalStateException ("no transaction associated with current thread."); 79 } 80 return transaction.delistResource(xaResource, flag); 81 } 82 83 public boolean enlistResource(XAResource xaResource) throws RollbackException , IllegalStateException , SystemException { 84 if(transaction == null) { 85 throw new IllegalStateException ("no transaction associated with current thread."); 86 } 87 return transaction.enlistResource(xaResource); 88 } 89 90 public int getStatus() throws SystemException { 91 if(transaction == null) { 92 return Status.STATUS_NO_TRANSACTION; 93 } 94 else { 95 return transaction.getStatus(); 96 } 97 } 98 99 public void registerSynchronization(Synchronization synchronization) throws RollbackException , IllegalStateException , SystemException { 100 if(transaction == null) { 101 throw new IllegalStateException ("no transaction associated with current thread."); 102 } 103 if(synchronization == null) return; 104 transaction.registerSynchronization(synchronization); 105 } 106 107 113 public void rollback() throws IllegalStateException , SystemException { 114 if(transaction == null) { 115 throw new IllegalStateException ("no transaction associated with current thread."); 116 } 117 try { 118 transaction.rollback(); 119 } 120 finally { 121 ((TimerTask ) transaction).cancel(); 122 } 123 } 124 125 public void setRollbackOnly() throws IllegalStateException , SystemException { 126 if(transaction == null) { 127 throw new IllegalStateException ("no transaction associated with current thread."); 128 } 129 transaction.setRollbackOnly(); 130 } 131 132 public String toString() { 133 if(transaction == null) { 134 return "No Transaction."; 135 } 136 else { 137 return transaction.toString(); 138 } 139 } 140 141 Transaction suspend() throws SystemException { 142 if(transaction == null) { 143 throw new IllegalStateException ("no transaction associated with current thread."); 144 } 145 Transaction suspendedTransaction = transaction; 146 transaction.suspend(); 147 transaction = null; 148 return suspendedTransaction; 149 } 150 151 void resume(Transaction trans) throws InvalidTransactionException , IllegalStateException , SystemException { 152 if(trans == null || !(trans instanceof TransactionImpl)) { 153 throw new InvalidTransactionException (trans + " is not a instance of " + TransactionImpl.class.getName()); 154 } 155 transaction = (TransactionImpl) trans; 156 try { 157 transaction.resume(); 158 } 159 catch(RollbackException e) { 160 throw new IllegalStateException ("transaction already rolled back, " + e.getMessage()); 161 } 162 } 163 } 164 | Popular Tags |