KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > ojb > otm > lock > isolation > SerializableIsolation


1 package org.apache.ojb.otm.lock.isolation;
2
3 /* Copyright 2003-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 java.util.Collection JavaDoc;
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     /**
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     {
33         Collection JavaDoc readers = lock.getReaders();
34         if (readers.isEmpty())
35         {
36             lock.readLock(tx);
37             readers = lock.getReaders();
38             if (readers.size() > 1)
39             {
40                 lock.releaseLock(tx);
41                 readLock(tx, lock);
42             }
43         }
44         else
45         {
46             Transaction reader = (Transaction) readers.iterator().next();
47
48             if (reader != tx) {
49                 lock.waitForTx(reader);
50             }
51         }
52     }
53
54     /**
55      * @see org.apache.ojb.otm.lock.isolation.TransactionIsolation#writeLock(Transaction, ObjectLock)
56      */

57     public void writeLock (Transaction tx, ObjectLock lock)
58         throws LockingException
59     {
60         Collection JavaDoc readers = lock.getReaders();
61         if (readers.isEmpty())
62         {
63             readLock(tx, lock);
64             writeLock(tx, lock);
65         }
66         else
67         {
68             Transaction reader = (Transaction) readers.iterator().next();
69             if (reader == tx)
70             {
71                 lock.writeLock(tx);
72             }
73             else
74             {
75                 lock.waitForTx(reader);
76                 writeLock(tx, lock);
77             }
78         }
79     }
80
81 }
82
Popular Tags