Coverage Report - org.apache.ojb.otm.lock.isolation.RepeatableReadIsolation
 
Classes in this File Line Coverage Branch Coverage Complexity
RepeatableReadIsolation
N/A
N/A
5
 
 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;
 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  
     /**
 29  
      * @see org.apache.ojb.otm.lock.isolation.TransactionIsolation#readLock(Transaction, ObjectLock)
 30  
      */
 31  
     public void readLock(Transaction tx, ObjectLock lock)
 32  
         throws LockingException
 33  
     {
 34  
         Transaction writer = lock.getWriter();
 35  
         if (writer == null)
 36  
         {
 37  
             lock.readLock(tx);
 38  
             if (lock.getWriter() != null)
 39  
             {
 40  
                 lock.releaseLock(tx);
 41  
                 readLock(tx, lock);
 42  
             }
 43  
         }
 44  
         else if (tx != writer)
 45  
         {
 46  
             lock.waitForTx(writer);
 47  
             readLock(tx, lock);
 48  
         }
 49  
         // else if tx is the writer, it can also read.
 50  
     }
 51  
 
 52  
     /**
 53  
      * @see org.apache.ojb.otm.lock.isolation.TransactionIsolation#writeLock(Transaction, ObjectLock)
 54  
      */
 55  
     public void writeLock(Transaction tx, ObjectLock lock)
 56  
         throws LockingException
 57  
     {
 58  
         Collection readers = lock.getReaders();
 59  
         
 60  
         if (!readers.isEmpty())
 61  
         {
 62  
             for (Iterator it = readers.iterator(); it.hasNext(); )
 63  
             {
 64  
                 Transaction reader = (Transaction) it.next();
 65  
 
 66  
                 if (reader != tx)
 67  
                 {
 68  
                     lock.waitForTx(reader);
 69  
                     writeLock(tx, lock);
 70  
                     return;
 71  
                 }
 72  
             }
 73  
         }
 74  
         
 75  
         lock.writeLock(tx);
 76  
         readers = lock.getReaders();
 77  
         if (readers.size() > 1)
 78  
         {
 79  
             lock.releaseLock(tx);
 80  
             writeLock(tx, lock);
 81  
         }
 82  
     }
 83  
 }