1 22 package org.jboss.mq.pm; 23 24 import java.io.Externalizable ; 25 import java.util.ArrayList ; 26 27 import javax.jms.JMSException ; 28 import javax.transaction.xa.Xid ; 29 30 36 public class Tx implements Comparable , Externalizable 37 { 38 39 static final long serialVersionUID = -5428592493990463362L; 40 41 42 private static final int OPEN = 0; 43 44 private static final int ENDED = 2; 45 46 47 public static final int UNKNOWN = 0; 48 49 50 public static final int ADD = 1; 51 52 53 public static final int REMOVE = -1; 54 55 56 long value = 0; 57 58 59 boolean persisted = false; 60 61 transient Xid xid; 62 63 64 transient ArrayList postCommitTasks; 65 66 transient ArrayList postRollbackTasks; 67 68 69 transient int status = OPEN; 70 71 72 transient Object lock = new Object (); 73 74 77 public Tx() 78 { 79 } 80 81 86 public Tx(long value) 87 { 88 this.value = value; 89 } 90 91 96 public void setValue(long tx) 97 { 98 value = tx; 99 } 100 101 106 public long longValue() 107 { 108 return value; 109 } 110 111 116 public Xid getXid() 117 { 118 return xid; 119 } 120 121 126 public void setXid(Xid xid) 127 { 128 this.xid = xid; 129 } 130 131 136 public synchronized boolean checkPersisted() 137 { 138 boolean result = persisted; 139 persisted = true; 140 return result; 141 } 142 143 148 public synchronized boolean wasPersisted() 149 { 150 return persisted; 151 } 152 153 159 public int compareTo(Tx anotherLong) 160 { 161 long thisVal = this.value; 162 long anotherVal = anotherLong.value; 163 return (thisVal < anotherVal ? -1 : (thisVal == anotherVal ? 0 : 1)); 164 } 165 166 public int compareTo(Object o) 167 { 168 return compareTo((Tx) o); 169 } 170 171 public void readExternal(java.io.ObjectInput in) throws java.io.IOException 172 { 173 value = in.readLong(); 174 } 175 176 public void writeExternal(java.io.ObjectOutput out) throws java.io.IOException 177 { 178 out.writeLong(value); 179 } 180 181 public String toString() 182 { 183 return String.valueOf(value); 184 } 185 186 public int hashCode() 187 { 188 return (int) (value ^ (value >> 32)); 189 } 190 191 197 void commit(PersistenceManager pm) throws JMSException 198 { 199 synchronized (lock) 200 { 201 if (status == ENDED) 202 throw new JMSException ("Transaction is not active for commit"); 203 status = ENDED; 204 postRollbackTasks = null; 205 } 206 207 pm.commitPersistentTx(this); 208 209 synchronized (lock) 210 { 211 if (postCommitTasks == null) 212 return; 213 214 for (int i = 0; i < postCommitTasks.size(); ++i) 215 { 216 Runnable task = (Runnable ) postCommitTasks.get(i); 217 task.run(); 218 } 219 220 postCommitTasks = null; 221 } 222 } 223 224 230 void addPostCommitTask(Runnable task) throws JMSException 231 { 232 synchronized (lock) 233 { 234 if (status == ENDED) 235 throw new JMSException ("Transacation is not active."); 236 if (postCommitTasks == null) 237 postCommitTasks = new ArrayList (); 238 postCommitTasks.add(task); 239 } 240 } 241 242 248 public void rollback(PersistenceManager pm) throws JMSException 249 { 250 synchronized (lock) 251 { 252 if (status == ENDED) 253 throw new JMSException ("Transaction is not active for rollback"); 254 status = ENDED; 255 postCommitTasks = null; 256 } 257 258 pm.rollbackPersistentTx(this); 259 260 synchronized (lock) 261 { 262 if (postRollbackTasks == null) 263 return; 264 265 for (int i = 0; i < postRollbackTasks.size(); ++i) 266 { 267 Runnable task = (Runnable ) postRollbackTasks.get(i); 268 task.run(); 269 } 270 271 postRollbackTasks = null; 272 } 273 } 274 275 281 public void addPostRollbackTask(Runnable task) throws JMSException 282 { 283 synchronized (lock) 284 { 285 if (status == ENDED) 286 throw new JMSException ("Transacation is not active."); 287 if (postRollbackTasks == null) 288 postRollbackTasks = new ArrayList (); 289 postRollbackTasks.add(task); 290 } 291 } 292 } | Popular Tags |