001 package org.apache.ojb.otm.lock.isolation;
002
003 /* Copyright 2003-2005 The Apache Software Foundation
004 *
005 * Licensed under the Apache License, Version 2.0 (the "License");
006 * you may not use this file except in compliance with the License.
007 * You may obtain a copy of the License at
008 *
009 * http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017
018 import java.util.Collection;
019
020 import org.apache.ojb.otm.core.Transaction;
021 import org.apache.ojb.otm.lock.LockingException;
022 import org.apache.ojb.otm.lock.ObjectLock;
023
024 public class SerializableIsolation extends AbstractIsolation
025 {
026
027 /**
028 * @see org.apache.ojb.otm.lock.isolation.TransactionIsolation#readLock(Transaction, ObjectLock)
029 */
030 public void readLock (Transaction tx, ObjectLock lock)
031 throws LockingException
032 {
033 Collection readers = lock.getReaders();
034 if (readers.isEmpty())
035 {
036 lock.readLock(tx);
037 readers = lock.getReaders();
038 if (readers.size() > 1)
039 {
040 lock.releaseLock(tx);
041 readLock(tx, lock);
042 }
043 }
044 else
045 {
046 Transaction reader = (Transaction) readers.iterator().next();
047
048 if (reader != tx) {
049 lock.waitForTx(reader);
050 }
051 }
052 }
053
054 /**
055 * @see org.apache.ojb.otm.lock.isolation.TransactionIsolation#writeLock(Transaction, ObjectLock)
056 */
057 public void writeLock (Transaction tx, ObjectLock lock)
058 throws LockingException
059 {
060 Collection readers = lock.getReaders();
061 if (readers.isEmpty())
062 {
063 readLock(tx, lock);
064 writeLock(tx, lock);
065 }
066 else
067 {
068 Transaction reader = (Transaction) readers.iterator().next();
069 if (reader == tx)
070 {
071 lock.writeLock(tx);
072 }
073 else
074 {
075 lock.waitForTx(reader);
076 writeLock(tx, lock);
077 }
078 }
079 }
080
081 }