View Javadoc

1   package org.apache.ojb.odmg.locking;
2   
3   /* Copyright 2002-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 org.apache.ojb.odmg.TransactionImpl;
19  
20  /**
21   * The implementation of the Uncommited Reads Locking strategy.
22   * This strategy is the loosest of them all.  It says
23   * you shouldn't need to get any Read locks whatsoever,
24   * but since it will probably try to get them, it will
25   * always give it to them.
26   *
27   * Locks are obtained on modifications to the database and held until end of
28   * transaction (EOT). Reading from the database does not involve any locking.
29   *
30   * Allows:
31   * Dirty Reads
32   * Non-Repeatable Reads
33   * Phantom Reads
34   *
35   * @author Thomas Mahler & David Dixon-Peugh
36   */
37  public class ReadUncommittedStrategy extends AbstractLockStrategy
38  {
39      /**
40       * acquire a read lock on Object obj for Transaction tx.
41       * @param tx the transaction requesting the lock
42       * @param obj the Object to be locked
43       * @return true if successful, else false
44       * When we read Uncommitted, we don't care about Reader locks
45       */
46      public boolean readLock(TransactionImpl tx, Object obj)
47      {
48          return true;
49      }
50  
51      /**
52       * acquire a write lock on Object obj for Transaction tx.
53       * @param tx the transaction requesting the lock
54       * @param obj the Object to be locked
55       * @return true if successful, else false
56       *
57       */
58      public boolean writeLock(TransactionImpl tx, Object obj)
59      {
60          LockEntry writer = getWriter(obj);
61          if (writer == null)
62          {
63              if (setWriter(tx, obj))
64                  return true;
65              else
66                  return writeLock(tx, obj);
67          }
68          if (writer.isOwnedBy(tx))
69          {
70              return true;    // If I'm the writer, then I can write.
71          }
72          return false;
73      }
74  
75      /**
76       * acquire a lock upgrade (from read to write) lock on Object obj for Transaction tx.
77       * @param tx the transaction requesting the lock
78       * @param obj the Object to be locked
79       * @return true if successful, else false
80       *
81       */
82      public boolean upgradeLock(TransactionImpl tx, Object obj)
83      {
84          LockEntry writer = getWriter(obj);
85          if (writer == null)
86          {
87              if (setWriter(tx, obj))
88                  return true;
89              else
90                  return upgradeLock(tx, obj);
91          }
92          if (writer.isOwnedBy(tx))
93          {
94              return true;    // If I already have Write, then I've upgraded.
95          }
96          return false;
97      }
98  
99      /**
100      * release a lock on Object obj for Transaction tx.
101      * @param tx the transaction releasing the lock
102      * @param obj the Object to be unlocked
103      * @return true if successful, else false
104      *
105      */
106     public boolean releaseLock(TransactionImpl tx, Object obj)
107     {
108         LockEntry writer = getWriter(obj);
109         if (writer != null && writer.isOwnedBy(tx))
110         {
111             removeWriter(writer);
112             return true;
113         }
114         // readlocks cannot (and need not) be released, thus:
115         return true;
116     }
117 
118     /**
119      * checks whether the specified Object obj is read-locked by Transaction tx.
120      * @param tx the transaction
121      * @param obj the Object to be checked
122      * @return true if lock exists, else false
123      */
124     public boolean checkRead(TransactionImpl tx, Object obj)
125     {
126         return true;
127     }
128 
129     /**
130      * checks whether the specified Object obj is write-locked by Transaction tx.
131      * @param tx the transaction
132      * @param obj the Object to be checked
133      * @return true if lock exists, else false
134      */
135     public boolean checkWrite(TransactionImpl tx, Object obj)
136     {
137         LockEntry writer = getWriter(obj);
138         return (writer != null && writer.isOwnedBy(tx));
139     }
140 }