KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > ojb > odmg > locking > ReadCommittedStrategy


1 package org.apache.ojb.odmg.locking;
2
3 /* Copyright 2002-2005 The Apache Software Foundation
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 import org.apache.ojb.odmg.TransactionImpl;
19
20 /**
21  * The implementation of the Commited Reads Locking stra
22  * ReadCommitted - Reads and Writes require locks.
23  *
24  * Locks are acquired for reading and modifying the database.
25  * Locks are released after reading but locks on modified objects
26  * are held until EOT.
27  *
28  * Allows:
29  * Non-Repeatable Reads
30  * Phantom Readstegy.
31  *
32  * @author Thomas Mahler & David Dixon-Peugh
33  */

34 public class ReadCommittedStrategy extends AbstractLockStrategy
35 {
36
37     /**
38      * acquire a read lock on Object obj for Transaction tx.
39      * @param tx the transaction requesting the lock
40      * @param obj the Object to be locked
41      * @return true if successful, else false
42      *
43      */

44     public boolean readLock(TransactionImpl tx, Object JavaDoc obj)
45     {
46
47         LockEntry writer = getWriter(obj);
48         if (writer == null)
49         {
50             addReader(tx, obj);
51             // if there has been a successful write locking, try again
52
if (getWriter(obj) == null)
53                 return true;
54             else
55             {
56                 removeReader(tx, obj);
57                 return readLock(tx, obj);
58             }
59         }
60         if (writer.isOwnedBy(tx))
61         {
62             return true; // If I'm the writer, I can read.
63
}
64         else
65         {
66             return false;
67         }
68     }
69
70     /**
71      * acquire a write lock on Object obj for Transaction tx.
72      * @param tx the transaction requesting the lock
73      * @param obj the Object to be locked
74      * @return true if successful, else false
75      *
76      */

77     public boolean writeLock(TransactionImpl tx, Object JavaDoc obj)
78     {
79         LockEntry writer = getWriter(obj);
80         // if there is no writer yet we can try to get the global write lock
81
if (writer == null)
82         {
83             // if lock could be acquired return true
84
if (setWriter(tx, obj))
85                 return true;
86             // else try again
87
else
88                 return writeLock(tx, obj);
89         }
90         if (writer.isOwnedBy(tx))
91         {
92             return true; // If I'm the writer, then I can write.
93
}
94
95         return false;
96     }
97
98     /**
99      * acquire a lock upgrade (from read to write) lock on Object obj for Transaction tx.
100      * @param tx the transaction requesting the lock
101      * @param obj the Object to be locked
102      * @return true if successful, else false
103      *
104      */

105     public boolean upgradeLock(TransactionImpl tx, Object JavaDoc obj)
106     {
107         LockEntry writer = getWriter(obj);
108         if (writer == null)
109         {
110             // if lock could be acquired return true
111
if (setWriter(tx, obj))
112                 return true;
113             // else try again
114
else
115                 return upgradeLock(tx, obj);
116         }
117         if (writer.isOwnedBy(tx))
118         {
119             return true; // If I already have Write, then I've upgraded.
120
}
121
122         return false;
123     }
124
125     /**
126      * release a lock on Object obj for Transaction tx.
127      * @param tx the transaction releasing the lock
128      * @param obj the Object to be unlocked
129      * @return true if successful, else false
130      *
131      */

132     public boolean releaseLock(TransactionImpl tx, Object JavaDoc obj)
133     {
134         LockEntry writer = getWriter(obj);
135
136         if (writer != null && writer.isOwnedBy(tx))
137         {
138             removeWriter(writer);
139             return true;
140         }
141
142         if (hasReadLock(tx, obj))
143         {
144             removeReader(tx, obj);
145             return true;
146         }
147         return false;
148     }
149
150     /**
151      * checks whether the specified Object obj is read-locked by Transaction tx.
152      * @param tx the transaction
153      * @param obj the Object to be checked
154      * @return true if lock exists, else false
155      */

156     public boolean checkRead(TransactionImpl tx, Object JavaDoc obj)
157     {
158         if (hasReadLock(tx, obj))
159         {
160             return true;
161         }
162         LockEntry writer = getWriter(obj);
163         if (writer.isOwnedBy(tx))
164         {
165             return true;
166         }
167         return false;
168     }
169
170     /**
171      * checks whether the specified Object obj is write-locked by Transaction tx.
172      * @param tx the transaction
173      * @param obj the Object to be checked
174      * @return true if lock exists, else false
175      */

176     public boolean checkWrite(TransactionImpl tx, Object JavaDoc obj)
177     {
178         LockEntry writer = getWriter(obj);
179         if (writer == null)
180             return false;
181         else if (writer.isOwnedBy(tx))
182             return true;
183         else
184             return false;
185     }
186 }
187
Popular Tags