1 22 package org.jboss.tm; 23 24 import java.util.Collections ; 25 import java.util.HashMap ; 26 import java.util.Map ; 27 28 import javax.transaction.RollbackException ; 29 import javax.transaction.Synchronization ; 30 import javax.transaction.SystemException ; 31 import javax.transaction.Transaction ; 32 import javax.transaction.TransactionManager ; 33 34 import org.jboss.util.NestedRuntimeException; 35 36 import EDU.oswego.cs.dl.util.concurrent.ConcurrentHashMap; 37 import EDU.oswego.cs.dl.util.concurrent.ReentrantLock; 38 39 48 public class TransactionLocalDelegateImpl 49 implements TransactionLocalDelegate 50 { 51 53 54 protected TransactionManager manager; 55 56 58 59 protected static ConcurrentHashMap synchronizationsByTransaction = new ConcurrentHashMap(); 60 61 67 protected static TransactionLocalSynchronization getSynchronization(Transaction tx, boolean create) 68 { 69 synchronized (tx) 70 { 71 TransactionLocalSynchronization result = (TransactionLocalSynchronization) synchronizationsByTransaction.get(tx); 72 if (result == null && create == true) 73 { 74 result = new TransactionLocalSynchronization(tx); 75 try 76 { 77 tx.registerSynchronization(result); 78 } 79 catch (RollbackException e) 80 { 81 throw new IllegalStateException ("Transaction already rolled back or marked for rollback"); 82 } 83 catch (SystemException e) 84 { 85 throw new NestedRuntimeException(e); 86 } 87 synchronizationsByTransaction.put(tx, result); 88 } 89 return result; 90 } 91 } 92 93 98 protected static void removeSynchronization(Transaction tx) 99 { 100 synchronizationsByTransaction.remove(tx); 101 } 102 103 105 110 public TransactionLocalDelegateImpl(TransactionManager manager) 111 { 112 this.manager = manager; 113 } 114 115 public void lock(TransactionLocal local, Transaction tx) throws InterruptedException 116 { 117 TransactionLocalSynchronization sync = getSynchronization(tx, true); 118 sync.lock(local); 119 } 120 121 public void unlock(TransactionLocal local, Transaction tx) 122 { 123 TransactionLocalSynchronization sync = getSynchronization(tx, false); 124 if (sync != null) 125 sync.unlock(local); 126 else 127 throw new IllegalStateException ("No synchronization found tx=" + tx + " local=" + local); 128 } 129 130 public Object getValue(TransactionLocal local, Transaction tx) 131 { 132 TransactionLocalSynchronization sync = getSynchronization(tx, false); 133 if (sync == null) 134 return null; 135 return sync.getValue(local); 136 } 137 138 public void storeValue(TransactionLocal local, Transaction tx, Object value) 139 { 140 TransactionLocalSynchronization sync = getSynchronization(tx, true); 141 sync.setValue(local, value); 142 } 143 144 public boolean containsValue(TransactionLocal local, Transaction tx) 145 { 146 TransactionLocalSynchronization sync = getSynchronization(tx, false); 147 if (sync == null) 148 return false; 149 return sync.containsValue(local); 150 } 151 152 154 protected static class TransactionLocalSynchronization 155 implements Synchronization 156 { 157 protected Transaction tx; 158 159 private Map valuesByLocal = Collections.synchronizedMap(new HashMap ()); 160 161 protected ReentrantLock reentrantLock = new ReentrantLock(); 162 163 public TransactionLocalSynchronization(Transaction tx) 164 { 165 this.tx = tx; 166 } 167 168 public void beforeCompletion() 169 { 170 } 171 172 public void afterCompletion(int status) 173 { 174 removeSynchronization(tx); 175 valuesByLocal.clear(); } 177 178 public void lock(Object local) throws InterruptedException 179 { 180 boolean locked = reentrantLock.attempt(60000); 181 if (locked == false) 182 throw new IllegalStateException ("Failed to acquire lock within 60 seconds."); 183 } 184 185 public void unlock(Object local) 186 { 187 reentrantLock.release(); 188 } 189 190 public Object getValue(Object local) 191 { 192 return valuesByLocal.get(local); 193 } 194 195 public void setValue(Object local, Object value) 196 { 197 valuesByLocal.put(local, value); 198 } 199 200 public boolean containsValue(Object local) 201 { 202 return valuesByLocal.containsKey(local); 203 } 204 } 205 } 206 | Popular Tags |