1 22 package org.jboss.resource.connectionmanager; 23 24 import java.util.ArrayList ; 25 26 import javax.transaction.RollbackException ; 27 import javax.transaction.Synchronization ; 28 import javax.transaction.SystemException ; 29 import javax.transaction.Transaction ; 30 import javax.transaction.TransactionManager ; 31 32 import org.jboss.logging.Logger; 33 import org.jboss.tm.TransactionLocal; 34 import org.jboss.util.NestedRuntimeException; 35 36 46 public class TransactionSynchronizer implements Synchronization 47 { 48 49 private static final Logger log = Logger.getLogger(TransactionSynchronizer.class); 50 51 52 protected static TransactionLocal txSynchs; 53 54 55 protected Transaction tx; 56 57 58 protected Thread enlistingThread; 59 60 61 protected ArrayList unenlisted; 62 63 64 protected ArrayList enlisted; 65 66 67 protected Synchronization ccmSynch; 68 69 70 public static void setTransactionManager(TransactionManager tm) 71 { 72 txSynchs = new TransactionLocal(tm); 73 } 74 75 80 private TransactionSynchronizer(Transaction tx) 81 { 82 this.tx = tx; 83 } 84 85 90 synchronized void addUnenlisted(Synchronization synch) 91 { 92 if (unenlisted == null) 93 unenlisted = new ArrayList (); 94 unenlisted.add(synch); 95 } 96 97 103 synchronized ArrayList getUnenlisted() 104 { 105 Thread currentThread = Thread.currentThread(); 106 while (enlistingThread != null && enlistingThread != currentThread) 107 { 108 boolean interrupted = false; 109 try 110 { 111 wait(); 112 } 113 catch (InterruptedException e) 114 { 115 interrupted = true; 116 } 117 if (interrupted) 118 currentThread.interrupt(); 119 } 120 ArrayList result = unenlisted; 121 unenlisted = null; 122 if (result != null) 123 enlistingThread = currentThread; 124 return result; 125 } 126 127 132 synchronized void addEnlisted(Synchronization synch) 133 { 134 if (enlisted == null) 135 enlisted = new ArrayList (); 136 enlisted.add(synch); 137 } 138 139 145 synchronized boolean removeEnlisted(Synchronization synch) 146 { 147 return enlisted.remove(synch); 148 } 149 150 153 synchronized void enlisted() 154 { 155 Thread currentThread = Thread.currentThread(); 156 if (enlistingThread == null || enlistingThread != currentThread) 157 { 158 log.warn("Thread " + currentThread + " not the enlisting thread " + enlistingThread, new Exception ("STACKTRACE")); 159 return; 160 } 161 enlistingThread = null; 162 notifyAll(); 163 } 164 165 173 static TransactionSynchronizer getRegisteredSynchronizer(Transaction tx) throws RollbackException , SystemException 174 { 175 TransactionSynchronizer result = (TransactionSynchronizer) txSynchs.get(tx); 176 if (result == null) 177 { 178 result = new TransactionSynchronizer(tx); 179 tx.registerSynchronization(result); 180 txSynchs.set(tx, result); 181 } 182 return result; 183 } 184 185 190 static Synchronization getCCMSynchronization(Transaction tx) 191 { 192 TransactionSynchronizer ts = (TransactionSynchronizer) txSynchs.get(tx); 193 if (ts != null) 194 return ts.ccmSynch; 195 else 196 return null; 197 } 198 199 207 static void registerCCMSynchronization(Transaction tx, Synchronization synch) throws RollbackException , SystemException 208 { 209 TransactionSynchronizer ts = getRegisteredSynchronizer(tx); 210 ts.ccmSynch = synch; 211 } 212 213 218 static void lock(Transaction tx) 219 { 220 try 221 { 222 txSynchs.lock(tx); 223 } 224 catch (InterruptedException e) 225 { 226 throw new NestedRuntimeException("Unable to get synchronization", e); 227 } 228 } 229 230 235 static void unlock(Transaction tx) 236 { 237 txSynchs.unlock(tx); 238 } 239 240 public void beforeCompletion() 241 { 242 if (enlisted != null) 243 { 244 int i = 0; 245 while (i < enlisted.size()) 246 { 247 Synchronization synch = (Synchronization ) enlisted.get(i); 248 invokeBefore(synch); 249 ++i; 250 } 251 } 252 253 if (ccmSynch != null) 254 invokeBefore(ccmSynch); 255 } 256 257 public void afterCompletion(int status) 258 { 259 if (enlisted != null) 260 { 261 int i = 0; 262 while (i < enlisted.size()) 263 { 264 Synchronization synch = (Synchronization ) enlisted.get(i); 265 invokeAfter(synch, status); 266 ++i; 267 } 268 } 269 270 if (ccmSynch != null) 271 invokeAfter(ccmSynch, status); 272 } 273 274 279 protected void invokeBefore(Synchronization synch) 280 { 281 try 282 { 283 synch.beforeCompletion(); 284 } 285 catch (Throwable t) 286 { 287 log.warn("Transaction " + tx + " error in before completion " + synch, t); 288 } 289 } 290 291 297 protected void invokeAfter(Synchronization synch, int status) 298 { 299 try 300 { 301 synch.afterCompletion(status); 302 } 303 catch (Throwable t) 304 { 305 log.warn("Transaction " + tx + " error in after completion " + synch, t); 306 } 307 } 308 } 309 | Popular Tags |