1 9 package org.jboss.portal.core.impl.tree.tm; 10 11 import org.jboss.portal.common.transaction.TransactionManagerProvider; 12 import org.jboss.cache.TransactionManagerLookup; 13 import org.apache.log4j.Logger; 14 15 import javax.transaction.TransactionManager ; 16 import javax.transaction.NotSupportedException ; 17 import javax.transaction.SystemException ; 18 import javax.transaction.RollbackException ; 19 import javax.transaction.HeuristicMixedException ; 20 import javax.transaction.HeuristicRollbackException ; 21 import javax.transaction.Transaction ; 22 import javax.transaction.InvalidTransactionException ; 23 import javax.transaction.Synchronization ; 24 import javax.transaction.Status ; 25 import java.util.Map ; 26 27 import EDU.oswego.cs.dl.util.concurrent.ConcurrentHashMap; 28 29 37 public class TransactionManagerObserver implements TransactionManager , TransactionManagerProvider, TransactionManagerLookup 38 { 39 40 private final Logger log; 41 42 private final TransactionManager tm; 43 44 private final Map txToObservation; 45 private final ThreadLocal threadToObservation; 46 47 public TransactionManagerObserver(TransactionManager tm) 48 { 49 this.log = Logger.getLogger(TransactionManagerObserver.class); 50 this.tm = tm; 51 this.txToObservation = new ConcurrentHashMap(); 52 this.threadToObservation = new ThreadLocal (); 53 } 54 55 57 public TransactionObservation getObservation() 58 { 59 return (TransactionObservation)threadToObservation.get(); 60 } 61 62 64 public void begin() throws NotSupportedException , SystemException 65 { 66 tm.begin(); 67 68 Transaction tx = tm.getTransaction(); 70 TransactionObservation observation = new TransactionObservation(this, tx); 71 txToObservation.put(tx, observation); 72 threadToObservation.set(observation); 73 } 74 75 78 public void commit() throws RollbackException , HeuristicMixedException , HeuristicRollbackException , SecurityException , IllegalStateException , SystemException 79 { 80 TransactionObservation observation = (TransactionObservation)getTransaction(); 81 SynchonizationResult result = new SynchonizationResult(); 82 try 83 { 84 Transaction tx = observation.tx; 86 txToObservation.remove(tx); 87 88 tx.registerSynchronization(result); 90 91 tm.commit(); 93 } 94 catch (RollbackException e) 95 { 96 log.error("Unexpected rollback exception", e); 97 } 98 finally 99 { 100 threadToObservation.set(null); 102 103 switch (result.status) 105 { 106 case Status.STATUS_COMMITTED: 107 observation.afterCommit(); 108 break; 109 case Status.STATUS_ROLLEDBACK: 110 observation.afterRollback(); 111 break; 112 default: 113 log.error("Unexpected status code at commit " + result.status); 114 } 115 } 116 } 117 118 121 public void rollback() throws IllegalStateException , SecurityException , SystemException 122 { 123 TransactionObservation observation = (TransactionObservation)threadToObservation.get(); 124 SynchonizationResult result = new SynchonizationResult(); 125 try 126 { 127 Transaction tx = observation.tx; 129 txToObservation.remove(tx); 130 131 tx.registerSynchronization(result); 133 134 tm.rollback(); 136 } 137 catch (RollbackException e) 138 { 139 log.error("Unexpected rollback exception", e); 140 } 141 finally 142 { 143 threadToObservation.set(null); 145 146 switch (result.status) 148 { 149 case Status.STATUS_COMMITTED: 150 observation.afterCommit(); 151 break; 152 case Status.STATUS_ROLLEDBACK: 153 observation.afterRollback(); 154 break; 155 default: 156 log.error("Unexpected status code at rollback " + result.status); 157 } 158 } 159 } 160 161 public void setRollbackOnly() throws IllegalStateException , SystemException 162 { 163 tm.setRollbackOnly(); 164 } 165 166 public int getStatus() throws SystemException 167 { 168 return tm.getStatus(); 169 } 170 171 public Transaction getTransaction() throws SystemException 172 { 173 Transaction tx = tm.getTransaction(); 174 if (tx != null) 175 { 176 tx = (TransactionObservation)txToObservation.get(tx); 177 } 178 return tx; 179 } 180 181 public void setTransactionTimeout(int value) throws SystemException 182 { 183 tm.setTransactionTimeout(value); 184 } 185 186 public Transaction suspend() throws SystemException 187 { 188 Transaction tx = tm.suspend(); 189 if (tx != null) 190 { 191 threadToObservation.set(null); 192 TransactionObservation observation = (TransactionObservation)txToObservation.get(tx); 193 tx = observation; 194 } 195 return tx; 196 } 197 198 public void resume(Transaction tx) throws InvalidTransactionException , IllegalStateException , SystemException 199 { 200 if (tx != null) 201 { 202 TransactionObservation observation = (TransactionObservation)tx; 203 threadToObservation.set(observation); 204 tx = observation.tx; 205 } 206 tm.resume(tx); 207 } 208 209 211 public TransactionManager getTransactionManager() 212 { 213 return this; 214 } 215 216 private static class SynchonizationResult implements Synchronization 217 { 218 int status = -50; public void beforeCompletion() 220 { 221 } 222 public void afterCompletion(int status) 223 { 224 this.status = status; 225 } 226 } 227 } 228 | Popular Tags |