1 17 18 package org.apache.geronimo.transaction.context; 19 20 import java.io.Serializable ; 21 import javax.transaction.HeuristicMixedException ; 22 import javax.transaction.HeuristicRollbackException ; 23 import javax.transaction.NotSupportedException ; 24 import javax.transaction.RollbackException ; 25 import javax.transaction.SystemException ; 26 import javax.transaction.UserTransaction ; 27 28 import org.apache.geronimo.transaction.TrackedConnectionAssociator; 29 30 37 public class UserTransactionImpl implements UserTransaction , Serializable { 38 private final ThreadLocal state = new StateThreadLocal(); 39 private static class StateThreadLocal extends ThreadLocal implements Serializable { 40 protected Object initialValue() { 41 return OFFLINE; 42 } 43 }; 44 45 public UserTransactionImpl() { 46 state.set(OFFLINE); 47 } 48 49 public void setUp(TransactionContextManager transactionContextManager, TrackedConnectionAssociator trackedConnectionAssociator) { 50 assert !isOnline() : "Only set the tx manager when UserTransaction is stop"; 51 this.ONLINE.setUp(transactionContextManager, trackedConnectionAssociator); 52 } 53 54 public boolean isOnline() { 55 return state.get() == ONLINE; 56 } 57 58 public void setOnline(boolean online) { 59 assert !online || ONLINE.isActive() : "online requires a tx manager"; 62 state.set(online ? ONLINE : OFFLINE); 63 } 64 65 private UserTransaction getUserTransaction() { 66 return (UserTransaction ) state.get(); 67 } 68 69 public void begin() throws NotSupportedException , SystemException { 70 getUserTransaction().begin(); 71 } 72 73 public void commit() throws HeuristicMixedException , HeuristicRollbackException , IllegalStateException , RollbackException , SecurityException , SystemException { 74 getUserTransaction().commit(); 75 } 76 77 public int getStatus() throws SystemException { 78 return getUserTransaction().getStatus(); 79 } 80 81 public void rollback() throws IllegalStateException , SecurityException , SystemException { 82 getUserTransaction().rollback(); 83 } 84 85 public void setRollbackOnly() throws IllegalStateException , SystemException { 86 getUserTransaction().setRollbackOnly(); 87 } 88 89 public void setTransactionTimeout(int timeout) throws SystemException { 90 getUserTransaction().setTransactionTimeout(timeout); 91 } 92 93 private final OnlineUserTransaction ONLINE = new OnlineUserTransaction(); 94 95 private static final UserTransaction OFFLINE = new OfflineUserTransaction(); 96 private static final class OfflineUserTransaction implements UserTransaction , Serializable { 97 public void begin() { 98 throw new IllegalStateException ("Cannot use UserTransaction methods in this state"); 99 } 100 101 public void commit() { 102 throw new IllegalStateException ("Cannot use UserTransaction methods in this state"); 103 } 104 105 public int getStatus() { 106 throw new IllegalStateException ("Cannot use UserTransaction methods in this state"); 107 } 108 109 public void rollback() { 110 throw new IllegalStateException ("Cannot use UserTransaction methods in this state"); 111 } 112 113 public void setRollbackOnly() { 114 throw new IllegalStateException ("Cannot use UserTransaction methods in this state"); 115 } 116 117 public void setTransactionTimeout(int seconds) { 118 throw new IllegalStateException ("Cannot use UserTransaction methods in this state"); 119 } 120 }; 121 } 122 | Popular Tags |