KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derby > impl > store > raw > xact > RowLocking3


1 /*
2
3    Derby - Class org.apache.derby.impl.store.raw.xact.RowLocking3
4
5    Licensed to the Apache Software Foundation (ASF) under one or more
6    contributor license agreements. See the NOTICE file distributed with
7    this work for additional information regarding copyright ownership.
8    The ASF licenses this file to you under the Apache License, Version 2.0
9    (the "License"); you may not use this file except in compliance with
10    the License. You may obtain a copy of the License at
11
12       http://www.apache.org/licenses/LICENSE-2.0
13
14    Unless required by applicable law or agreed to in writing, software
15    distributed under the License is distributed on an "AS IS" BASIS,
16    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17    See the License for the specific language governing permissions and
18    limitations under the License.
19
20  */

21
22 package org.apache.derby.impl.store.raw.xact;
23
24 import org.apache.derby.iapi.services.locks.LockFactory;
25 import org.apache.derby.iapi.services.locks.C_LockFactory;
26 import org.apache.derby.iapi.services.locks.Latch;
27
28 import org.apache.derby.iapi.services.sanity.SanityManager;
29
30 import org.apache.derby.iapi.store.raw.ContainerHandle;
31 import org.apache.derby.iapi.store.raw.ContainerLock;
32 import org.apache.derby.iapi.store.raw.RecordHandle;
33 import org.apache.derby.iapi.store.raw.RowLock;
34 import org.apache.derby.iapi.store.raw.Transaction;
35 import org.apache.derby.iapi.store.raw.LockingPolicy;
36
37 import org.apache.derby.iapi.error.StandardException;
38
39
40 /**
41     A locking policy that implements row level locking with isolation degree 3.
42
43     @see org.apache.derby.iapi.store.raw.LockingPolicy
44 */

45 public class RowLocking3 extends NoLocking
46 {
47     // no locking has no state, so it's safe to hold
48
// it as a static
49
private static final LockingPolicy NO_LOCK = new NoLocking();
50
51     protected final LockFactory lf;
52
53     protected RowLocking3(LockFactory lf)
54     {
55         this.lf = lf;
56     }
57
58     /**
59      * Get type of lock to get while reading data.
60      * <p>
61      * This routine is provided so that class's like RowLockingRR can
62      * override just this routine to get RS2 locks vs RS3 locks, and still
63      * reuse all the other code in this class.
64      * <p>
65      *
66      * @return The lock type of a shared lock for this locking policy.
67      **/

68     protected RowLock getReadLockType()
69     {
70         return(RowLock.RS3);
71     }
72
73     /**
74      * Get type of lock to get while requesting "update" lock.
75      * <p>
76      * This routine is provided so that class's like RowLockingRR can
77      * override just this routine to get RU2 locks vs RU3 locks, and still
78      * reuse all the other code in this class.
79      * <p>
80      *
81      * @return The lock type of a shared lock for this locking policy.
82      **/

83     protected RowLock getUpdateLockType()
84     {
85         return(RowLock.RU3);
86     }
87
88     /**
89      * Get type of lock to get while writing data.
90      * <p>
91      * This routine is provided so that class's like RowLockingRR can
92      * override just this routine to get RX2 locks vs RX3 locks, and still
93      * reuse all the other code in this class.
94      * <p>
95      *
96      * @return The lock type of a shared lock for this locking policy.
97      **/

98     protected RowLock getWriteLockType()
99     {
100         return(RowLock.RX3);
101     }
102
103
104     /**
105      * Obtain container level intent lock.
106      * <p>
107      * This implementation of row locking is 2 level, ie. table and row locking.
108      * It will interact correctly with tables opened with ContainerLocking3
109      * locking mode.
110      * <p>
111      * Updater's will get table level IX locks, and X row locks.
112      * <p>
113      * Reader's will get table level IS locks, and S row locks.
114      *
115      * @param t Transaction to associate lock with.
116      * @param container Container to lock.
117      * @param waitForLock Should lock request wait until granted?
118      * @param forUpdate Should container be locked for update, or read?
119      *
120      * @return true if the lock was obtained, false if it wasn't.
121      * False should only be returned if the waitForLock policy was set to
122      * "false," and the lock was unavailable.
123      *
124      * @exception StandardException Standard exception policy.
125      **/

126     public boolean lockContainer(
127     Transaction t,
128     ContainerHandle container,
129     boolean waitForLock,
130     boolean forUpdate)
131         throws StandardException
132     {
133         Object JavaDoc qualifier = forUpdate ? ContainerLock.CIX : ContainerLock.CIS;
134
135         boolean gotLock =
136             lf.lockObject(
137                 t.getCompatibilitySpace(), t, container.getId(), qualifier,
138                 waitForLock ? C_LockFactory.TIMED_WAIT : C_LockFactory.NO_WAIT);
139
140         if (gotLock) {
141             // look for covering table locks
142
// CIS is covered by CX or CS
143
// CIX is covered by CX
144

145             if (lf.isLockHeld(t.getCompatibilitySpace(), t, container.getId(), ContainerLock.CX) ||
146                 ((!forUpdate) && lf.isLockHeld(t.getCompatibilitySpace(), t, container.getId(), ContainerLock.CS))) {
147
148
149                 container.setLockingPolicy(NO_LOCK);
150             }
151         }
152
153         return gotLock;
154
155     }
156
157
158     /**
159      * Obtain lock on record being read.
160      * <p>
161      * Assumes that a table level IS has been acquired. Will acquire a Shared
162      * or Update lock on the row, depending on the "forUpdate" parameter.
163      * <p>
164      *
165      * @param t The transaction to associate the lock with.
166      * @param record The record to be locked.
167      * @param waitForLock Should lock request wait until granted?
168      * @param forUpdate Whether to open for read or write access.
169      *
170      * @return true if the lock was granted, false if waitForLock was false
171      * and the lock could not be granted.
172      *
173      * @exception StandardException Standard exception policy.
174      **/

175     public boolean lockRecordForRead(
176     Transaction t,
177     ContainerHandle container_handle,
178     RecordHandle record,
179     boolean waitForLock,
180     boolean forUpdate)
181         throws StandardException
182     {
183         // RESOLVE - Did I do the right thing with the "forUpdate" variable.
184

185         // For now just lock the row in Shared mode.
186
Object JavaDoc qualifier = forUpdate ? getUpdateLockType() : getReadLockType();
187
188         return(
189             lf.lockObject(
190                 t.getCompatibilitySpace(), t, record, qualifier,
191                 waitForLock ?
192                     C_LockFactory.TIMED_WAIT : C_LockFactory.NO_WAIT));
193     }
194
195     /**
196      * Obtain lock on record being read while holding a latch.
197      * <p>
198      * Assumes that a table level IS has been acquired. Will acquire a Shared
199      * or Update lock on the row, depending on the "forUpdate" parameter.
200      * <p>
201      *
202      * @param latch The latch being held.
203      * @param record The record to be locked.
204      * @param forUpdate Whether to open for read or write access.
205      *
206      * @exception StandardException Standard exception policy.
207      **/

208     public void lockRecordForRead(
209     Latch latch,
210     RecordHandle record,
211     boolean forUpdate)
212         throws StandardException
213     {
214         // RESOLVE - Did I do the right thing with the "forUpdate" variable.
215

216         // For now just lock the row in Shared mode.
217
Object JavaDoc qualifier = forUpdate ? getUpdateLockType() : getReadLockType();
218
219         lf.lockObject(
220             latch.getCompatabilitySpace(), record, qualifier,
221             C_LockFactory.TIMED_WAIT, latch);
222     }
223
224     /**
225      * Obtain lock on record being written.
226      * <p>
227      * Assumes that a table level IX has been acquired. Will acquire an
228      * Exclusive (X) lock on the row.
229      * <p>
230      *
231      * @param t transaction to associate the lock with.
232      * @param record The record to be locked.
233      * @param lockForInsertPreviousKey Lock is for a previous key of a insert.
234      * @param waitForLock Should lock request wait until granted?
235      *
236      * @return true if the lock was granted, false if waitForLock was false
237      * and the lock could not be granted.
238      *
239      * @exception StandardException Standard exception policy.
240      **/

241     public boolean zeroDurationLockRecordForWrite(
242     Transaction t,
243     RecordHandle record,
244     boolean lockForInsertPreviousKey,
245     boolean waitForLock)
246         throws StandardException
247     {
248         return(lf.zeroDurationlockObject(
249                 t.getCompatibilitySpace(), record,
250                 (lockForInsertPreviousKey ? RowLock.RIP : getWriteLockType()),
251                 waitForLock ? C_LockFactory.TIMED_WAIT : C_LockFactory.NO_WAIT));
252     }
253
254     /**
255      * Obtain lock on record being written.
256      * <p>
257      * Assumes that a table level IX has been acquired. Will acquire an
258      * Exclusive (X) lock on the row.
259      * <p>
260      *
261      * @param t The transaction to associate the lock with.
262      * @param record The record to be locked.
263      * @param lockForInsert Lock is for an insert.
264      * @param waitForLock Should lock request wait until granted?
265      *
266      * @return true if the lock was granted, false if waitForLock was false
267      * and the lock could not be granted.
268      *
269      * @exception StandardException Standard exception policy.
270      **/

271     public boolean lockRecordForWrite(
272     Transaction t,
273     RecordHandle record,
274     boolean lockForInsert,
275     boolean waitForLock)
276         throws StandardException
277     {
278         return(lf.lockObject(
279             t.getCompatibilitySpace(), t, record,
280             lockForInsert ? RowLock.RI : getWriteLockType(),
281             waitForLock ? C_LockFactory.TIMED_WAIT : C_LockFactory.NO_WAIT));
282     }
283
284     /**
285      * Obtain lock on record being written while holding a latch.
286      * <p>
287      * Assumes that a table level IX has been acquired. Will acquire an
288      * Exclusive (X) lock on the row.
289      * <p>
290      *
291      * @param latch The latch being held
292      * @param record The record to be locked.
293      *
294      * @exception StandardException Standard exception policy.
295      **/

296     public void lockRecordForWrite(
297     Latch latch,
298     RecordHandle record)
299         throws StandardException
300     {
301         lf.lockObject(
302             latch.getCompatabilitySpace(),
303             record,
304             getWriteLockType(),
305             C_LockFactory.TIMED_WAIT,
306             latch);
307     }
308
309     public int getMode() {
310         return MODE_RECORD;
311     }
312
313
314     /*
315     ** We can inherit all the others methods of NoLocking since we hold the
316     ** container lock and row locks until the end of transaction.
317     */

318 }
319
Popular Tags