1 6 7 package org.jfox.tm; 8 9 import javax.transaction.HeuristicMixedException ; 10 import javax.transaction.HeuristicRollbackException ; 11 import javax.transaction.InvalidTransactionException ; 12 import javax.transaction.NotSupportedException ; 13 import javax.transaction.RollbackException ; 14 import javax.transaction.Status ; 15 import javax.transaction.SystemException ; 16 import javax.transaction.Transaction ; 17 import javax.transaction.TransactionManager ; 18 19 22 23 public class TxManager implements TransactionManager { 24 25 private static ThreadLocal threadTx = new ThreadLocal () { 26 protected Object initialValue() { 27 return new TxWrapper(); 28 } 29 30 public Object get() { 31 Object obj = super.get(); 32 if(obj == null) { 33 obj = initialValue(); 34 super.set(obj); 35 } 36 return obj; 37 } 38 }; 39 40 private static TxManager me = new TxManager(); 42 43 private volatile int numTrasactions = 0; 44 private volatile int numSuspendedTransactions = 0; 45 46 public static TxManager getInstance() { 47 return me; 48 } 49 50 public void begin() throws NotSupportedException , SystemException { 51 numTrasactions++; 52 ((TxWrapper) threadTx.get()).begin(); 53 } 54 55 public void commit() throws RollbackException , HeuristicMixedException , HeuristicRollbackException , SecurityException , IllegalStateException , SystemException { 56 numTrasactions--; 57 try { 58 ((TxWrapper) threadTx.get()).commit(); 59 } 60 finally { 61 threadTx.set(null); 62 } 63 } 64 65 public void rollback() throws IllegalStateException , SecurityException , SystemException { 66 numTrasactions--; 67 try { 68 ((TxWrapper) threadTx.get()).rollback(); 69 } 70 finally { 71 threadTx.set(null); 72 } 73 } 74 75 public int getStatus() throws SystemException { 76 Transaction tx = (TxWrapper) threadTx.get(); 77 if(tx==null) { 78 return Status.STATUS_NO_TRANSACTION; 79 } 80 else { 81 return tx.getStatus(); 82 } 83 } 84 85 public Transaction getTransaction() throws SystemException { 86 return (TxWrapper) threadTx.get(); 87 } 88 89 public void setRollbackOnly() throws IllegalStateException , SystemException { 90 ((TxWrapper) threadTx.get()).setRollbackOnly(); 91 } 92 93 public void setTransactionTimeout(int seconds) throws SystemException { 94 ((TxWrapper) threadTx.get()).setTimeout(seconds * 1000); 95 } 96 97 public Transaction suspend() throws SystemException { 98 Transaction trans = ((TxWrapper) threadTx.get()).suspend(); 99 numSuspendedTransactions++; 100 return trans; 101 } 102 103 public void resume(Transaction transaction) throws InvalidTransactionException , IllegalStateException , SystemException { 104 if(transaction == null || !(transaction instanceof TransactionImpl)) { 105 throw new InvalidTransactionException (transaction + " is not a instance of " + TransactionImpl.class.getName()); 106 } 107 ((TxWrapper) threadTx.get()).resume(transaction); 108 numSuspendedTransactions--; 109 } 110 111 int getNumTransactions() { 112 return numTrasactions; 113 } 114 115 int getNumSuspendedTransactions() { 116 return numSuspendedTransactions; 117 } 118 119 public static void main(String [] args) { 120 121 } 122 } | Popular Tags |