01: package org.apache.ojb.otm.lock.isolation;
02:
03: /* Copyright 2003-2005 The Apache Software Foundation
04: *
05: * Licensed under the Apache License, Version 2.0 (the "License");
06: * you may not use this file except in compliance with the License.
07: * You may obtain a copy of the License at
08: *
09: * 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 java.util.Collection;
19: import java.util.Iterator;
20:
21: import org.apache.ojb.otm.core.Transaction;
22: import org.apache.ojb.otm.lock.LockingException;
23: import org.apache.ojb.otm.lock.ObjectLock;
24:
25: public class RepeatableReadIsolation extends AbstractIsolation {
26:
27: /**
28: * @see org.apache.ojb.otm.lock.isolation.TransactionIsolation#readLock(Transaction, ObjectLock)
29: */
30: public void readLock(Transaction tx, ObjectLock lock)
31: throws LockingException {
32: Transaction writer = lock.getWriter();
33: if (writer == null) {
34: lock.readLock(tx);
35: if (lock.getWriter() != null) {
36: lock.releaseLock(tx);
37: readLock(tx, lock);
38: }
39: } else if (tx != writer) {
40: lock.waitForTx(writer);
41: readLock(tx, lock);
42: }
43: // else if tx is the writer, it can also read.
44: }
45:
46: /**
47: * @see org.apache.ojb.otm.lock.isolation.TransactionIsolation#writeLock(Transaction, ObjectLock)
48: */
49: public void writeLock(Transaction tx, ObjectLock lock)
50: throws LockingException {
51: Collection readers = lock.getReaders();
52:
53: if (!readers.isEmpty()) {
54: for (Iterator it = readers.iterator(); it.hasNext();) {
55: Transaction reader = (Transaction) it.next();
56:
57: if (reader != tx) {
58: lock.waitForTx(reader);
59: writeLock(tx, lock);
60: return;
61: }
62: }
63: }
64:
65: lock.writeLock(tx);
66: readers = lock.getReaders();
67: if (readers.size() > 1) {
68: lock.releaseLock(tx);
69: writeLock(tx, lock);
70: }
71: }
72: }
|