001: package com.quadcap.sql.lock;
002:
003: /* Copyright 1999 - 2003 Quadcap Software. All rights reserved.
004: *
005: * This software is distributed under the Quadcap Free Software License.
006: * This software may be used or modified for any purpose, personal or
007: * commercial. Open Source redistributions are permitted. Commercial
008: * redistribution of larger works derived from, or works which bundle
009: * this software requires a "Commercial Redistribution License"; see
010: * http://www.quadcap.com/purchase.
011: *
012: * Redistributions qualify as "Open Source" under one of the following terms:
013: *
014: * Redistributions are made at no charge beyond the reasonable cost of
015: * materials and delivery.
016: *
017: * Redistributions are accompanied by a copy of the Source Code or by an
018: * irrevocable offer to provide a copy of the Source Code for up to three
019: * years at the cost of materials and delivery. Such redistributions
020: * must allow further use, modification, and redistribution of the Source
021: * Code under substantially the same terms as this license.
022: *
023: * Redistributions of source code must retain the copyright notices as they
024: * appear in each source code file, these license terms, and the
025: * disclaimer/limitation of liability set forth as paragraph 6 below.
026: *
027: * Redistributions in binary form must reproduce this Copyright Notice,
028: * these license terms, and the disclaimer/limitation of liability set
029: * forth as paragraph 6 below, in the documentation and/or other materials
030: * provided with the distribution.
031: *
032: * The Software is provided on an "AS IS" basis. No warranty is
033: * provided that the Software is free of defects, or fit for a
034: * particular purpose.
035: *
036: * Limitation of Liability. Quadcap Software shall not be liable
037: * for any damages suffered by the Licensee or any third party resulting
038: * from use of the Software.
039: */
040:
041: import java.util.ArrayList;
042: import java.util.HashMap;
043: import java.util.Iterator;
044:
045: import java.util.Enumeration;
046:
047: import javax.concurrent.Mutex;
048: import javax.concurrent.Sync;
049:
050: import com.quadcap.util.Debug;
051: import com.quadcap.util.Util;
052:
053: /**
054: * Locks are acquired on behalf of transactions.
055: *
056: * @author Stan Bailes
057: */
058: public class Transaction extends PooledObject {
059: long transactionId;
060:
061: Object osync = new Object();
062: Sync sync = null;
063: volatile HeldLock wait = null;
064: TransactionObserver observer = null;
065:
066: /**
067: * No-arg constructor
068: */
069: public Transaction() {
070: }
071:
072: final public PooledObject create() {
073: return new Transaction();
074: }
075:
076: /**
077: * Initialze this new transaction
078: */
079: final void init(long transactionId) {
080: this .transactionId = transactionId;
081: this .wait = null;
082: }
083:
084: public final Sync getSync() throws InterruptedException {
085: synchronized (osync) {
086: if (sync == null) {
087: sync = new Mutex();
088: sync.acquire();
089: }
090: return sync;
091: }
092: }
093:
094: public final void setWait(HeldLock wait) {
095: if (this .wait != null && wait != null) {
096: throw new RuntimeException(
097: "Transaction already waiting for " + this .wait
098: + ", new wait = " + wait);
099: }
100: this .wait = wait;
101: }
102:
103: public final void clearWait() {
104: this .wait = null;
105: }
106:
107: public final HeldLock getWait() {
108: return wait;
109: }
110:
111: public final Lock getWaitLock() {
112: return wait == null ? null : wait.lock;
113: }
114:
115: public final int getWaitMode() {
116: return wait == null ? LockMode.NL : wait.getWaitMode();
117: }
118:
119: public final long getTransactionId() {
120: //#ifdef DEBUG
121: assertLive();
122: //#endif
123: return transactionId;
124: }
125:
126: //#ifdef DEBUG
127: public String toString() {
128: StringBuffer sb = new StringBuffer("T:");
129: sb.append(String.valueOf((int) (transactionId & 0xffffffffL)));
130: if (wait != null) {
131: sb.append(" WAIT: ");
132: sb.append(wait.lock.toString());
133: sb.append(" mode ");
134: sb.append(LockMode.toString(getWaitMode()));
135: sb.append(" T:");
136: sb.append(wait.getTransaction().transactionId);
137: }
138: return sb.toString();
139: }
140:
141: public String dump() {
142: StringBuffer sb = new StringBuffer(toString());
143: if (wait != null) {
144: sb.append(" waitLock: ");
145: sb.append(wait);
146: }
147: return sb.toString();
148: }
149:
150: //#endif
151:
152: public void setObserver(TransactionObserver obj) {
153: observer = obj;
154: }
155:
156: public TransactionObserver getObserver() {
157: return observer;
158: }
159: }
|