KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > quadcap > sql > lock > Transaction


1 package com.quadcap.sql.lock;
2
3 /* Copyright 1999 - 2003 Quadcap Software. All rights reserved.
4  *
5  * This software is distributed under the Quadcap Free Software License.
6  * This software may be used or modified for any purpose, personal or
7  * commercial. Open Source redistributions are permitted. Commercial
8  * redistribution of larger works derived from, or works which bundle
9  * this software requires a "Commercial Redistribution License"; see
10  * http://www.quadcap.com/purchase.
11  *
12  * Redistributions qualify as "Open Source" under one of the following terms:
13  *
14  * Redistributions are made at no charge beyond the reasonable cost of
15  * materials and delivery.
16  *
17  * Redistributions are accompanied by a copy of the Source Code or by an
18  * irrevocable offer to provide a copy of the Source Code for up to three
19  * years at the cost of materials and delivery. Such redistributions
20  * must allow further use, modification, and redistribution of the Source
21  * Code under substantially the same terms as this license.
22  *
23  * Redistributions of source code must retain the copyright notices as they
24  * appear in each source code file, these license terms, and the
25  * disclaimer/limitation of liability set forth as paragraph 6 below.
26  *
27  * Redistributions in binary form must reproduce this Copyright Notice,
28  * these license terms, and the disclaimer/limitation of liability set
29  * forth as paragraph 6 below, in the documentation and/or other materials
30  * provided with the distribution.
31  *
32  * The Software is provided on an "AS IS" basis. No warranty is
33  * provided that the Software is free of defects, or fit for a
34  * particular purpose.
35  *
36  * Limitation of Liability. Quadcap Software shall not be liable
37  * for any damages suffered by the Licensee or any third party resulting
38  * from use of the Software.
39  */

40
41 import java.util.ArrayList JavaDoc;
42 import java.util.HashMap JavaDoc;
43 import java.util.Iterator JavaDoc;
44
45 import java.util.Enumeration JavaDoc;
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 /**
54  * Locks are acquired on behalf of transactions.
55  *
56  * @author Stan Bailes
57  */

58 public class Transaction extends PooledObject {
59     long transactionId;
60
61     Object JavaDoc osync = new Object JavaDoc();
62     Sync sync = null;
63     volatile HeldLock wait = null;
64     TransactionObserver observer = null;
65
66     /**
67      * No-arg constructor
68      */

69     public Transaction() {}
70
71     final public PooledObject create() { return new Transaction(); }
72
73     /**
74      * Initialze this new transaction
75      */

76     final void init(long transactionId) {
77         this.transactionId = transactionId;
78         this.wait = null;
79     }
80
81     public final Sync getSync() throws InterruptedException JavaDoc {
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 JavaDoc("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         //#ifdef DEBUG
113
assertLive();
114         //#endif
115
return transactionId;
116     }
117
118     //#ifdef DEBUG
119
public String JavaDoc toString() {
120         StringBuffer JavaDoc sb = new StringBuffer JavaDoc("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 JavaDoc dump() {
134         StringBuffer JavaDoc sb = new StringBuffer JavaDoc(toString());
135         if (wait != null) {
136             sb.append(" waitLock: ");
137             sb.append(wait);
138         }
139         return sb.toString();
140     }
141     //#endif
142

143     public void setObserver(TransactionObserver obj) { observer = obj; }
144     public TransactionObserver getObserver() { return observer; }
145 }
146
Popular Tags