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:
20: import org.apache.ojb.otm.core.Transaction;
21: import org.apache.ojb.otm.lock.LockingException;
22: import org.apache.ojb.otm.lock.ObjectLock;
23:
24: public class SerializableIsolation extends AbstractIsolation {
25:
26: /**
27: * @see org.apache.ojb.otm.lock.isolation.TransactionIsolation#readLock(Transaction, ObjectLock)
28: */
29: public void readLock(Transaction tx, ObjectLock lock)
30: throws LockingException {
31: Collection readers = lock.getReaders();
32: if (readers.isEmpty()) {
33: lock.readLock(tx);
34: readers = lock.getReaders();
35: if (readers.size() > 1) {
36: lock.releaseLock(tx);
37: readLock(tx, lock);
38: }
39: } else {
40: Transaction reader = (Transaction) readers.iterator()
41: .next();
42:
43: if (reader != tx) {
44: lock.waitForTx(reader);
45: }
46: }
47: }
48:
49: /**
50: * @see org.apache.ojb.otm.lock.isolation.TransactionIsolation#writeLock(Transaction, ObjectLock)
51: */
52: public void writeLock(Transaction tx, ObjectLock lock)
53: throws LockingException {
54: Collection readers = lock.getReaders();
55: if (readers.isEmpty()) {
56: readLock(tx, lock);
57: writeLock(tx, lock);
58: } else {
59: Transaction reader = (Transaction) readers.iterator()
60: .next();
61: if (reader == tx) {
62: lock.writeLock(tx);
63: } else {
64: lock.waitForTx(reader);
65: writeLock(tx, lock);
66: }
67: }
68: }
69:
70: }
|