1 package com.quadcap.sql.lock; 2 3 40 41 import java.util.ArrayList ; 42 import java.util.HashMap ; 43 import java.util.Iterator ; 44 45 import java.util.Enumeration ; 46 47 import javax.concurrent.Mutex; 48 import javax.concurrent.Sync; 49 50 import com.quadcap.util.Debug; 51 import com.quadcap.util.Util; 52 53 58 public class Transaction extends PooledObject { 59 long transactionId; 60 61 Object osync = new Object (); 62 Sync sync = null; 63 volatile HeldLock wait = null; 64 TransactionObserver observer = null; 65 66 69 public Transaction() {} 70 71 final public PooledObject create() { return new Transaction(); } 72 73 76 final void init(long transactionId) { 77 this.transactionId = transactionId; 78 this.wait = null; 79 } 80 81 public final Sync getSync() throws InterruptedException { 82 synchronized (osync) { 83 if (sync == null) { 84 sync = new Mutex(); 85 sync.acquire(); 86 } 87 return sync; 88 } 89 } 90 91 public final void setWait(HeldLock wait) { 92 if (this.wait != null && wait != null) { 93 throw new RuntimeException ("Transaction already waiting for " + 94 this.wait + 95 ", new wait = " + wait); 96 } 97 this.wait = wait; 98 } 99 100 public final void clearWait() { 101 this.wait = null; 102 } 103 104 public final HeldLock getWait() { return wait; } 105 public final Lock getWaitLock() { 106 return wait == null ? null : wait.lock; 107 } 108 public final int getWaitMode() { 109 return wait == null ? LockMode.NL : wait.getWaitMode(); 110 } 111 public final long getTransactionId() { 112 assertLive(); 114 return transactionId; 116 } 117 118 public String toString() { 120 StringBuffer sb = new StringBuffer ("T:"); 121 sb.append(String.valueOf((int)(transactionId & 0xffffffffL))); 122 if (wait != null) { 123 sb.append(" WAIT: "); 124 sb.append(wait.lock.toString()); 125 sb.append(" mode "); 126 sb.append(LockMode.toString(getWaitMode())); 127 sb.append(" T:"); 128 sb.append(wait.getTransaction().transactionId); 129 } 130 return sb.toString(); 131 } 132 133 public String dump() { 134 StringBuffer sb = new StringBuffer (toString()); 135 if (wait != null) { 136 sb.append(" waitLock: "); 137 sb.append(wait); 138 } 139 return sb.toString(); 140 } 141 143 public void setObserver(TransactionObserver obj) { observer = obj; } 144 public TransactionObserver getObserver() { return observer; } 145 } 146 | Popular Tags |