KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2
3    Derby - Class org.apache.derby.impl.store.raw.xact.RowLocking3Escalate
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.Latch;
26
27 import org.apache.derby.iapi.services.sanity.SanityManager;
28
29 import org.apache.derby.iapi.store.raw.ContainerHandle;
30 import org.apache.derby.iapi.store.raw.ContainerLock;
31 import org.apache.derby.iapi.store.raw.RecordHandle;
32 import org.apache.derby.iapi.store.raw.Transaction;
33
34 import org.apache.derby.iapi.error.StandardException;
35
36
37 /**
38     A locking policy that implements row level locking with isolation degree 3.
39
40     @see org.apache.derby.iapi.store.raw.LockingPolicy
41 */

42 public class RowLocking3Escalate extends ContainerLocking3
43 {
44     protected RowLocking3Escalate(LockFactory lf)
45     {
46         super(lf);
47     }
48
49     /**
50      * Escalates Row Locking 3 to Container Locking 3.
51      * <p>
52      * This call is made by code which tracks the number of locks on a
53      * container. When the number of locks exceeds the escalate threshold
54      * the caller creates this new locking policy, calls lockContainer(),
55      * and substitues it for the old locking policy. The lockContainer call
56      * determines which table lock to get (S or X), gets that table lock, and
57      * then releases the row locks on the table.
58      *
59      * It is assumed that this is called on a open container for lock only.
60      * <p>
61      *
62      * @param t Transaction to associate lock with.
63      * @param container Container to lock.
64      * @param waitForLock Ignored - will never wait for a lock.
65      * @param forUpdate Ignored, mode determined from current lock state.
66      *
67      * @return true if the lock was obtained, false if it wasn't.
68      * False should only be returned if the waitForLock policy was set to
69      * "false," and the lock was unavailable.
70      *
71      * @exception StandardException Standard exception policy.
72      **/

73     public boolean lockContainer(
74     Transaction t,
75     ContainerHandle container,
76     boolean waitForLock,
77     boolean forUpdate)
78         throws StandardException
79     {
80         forUpdate = false;
81
82         // If an IX lock exists then escalate to X table lock, else escalate
83
// to S table lock.
84
if (lf.isLockHeld(
85                 t.getCompatibilitySpace(), t,
86                 container.getId(), ContainerLock.CIX))
87         {
88             forUpdate = true;
89         }
90
91         // Get the new X or S table lock.
92
boolean gotLock =
93             super.lockContainer(t, container, waitForLock, forUpdate);
94
95         if (!gotLock)
96             return false;
97
98         // now remove all matching ROW locks, this is done using the special
99
// EscalateContainerKey() class which through the Matchable interface
100
// only matches row locks of this container.
101
lf.unlockGroup(
102             t.getCompatibilitySpace(), t,
103             new EscalateContainerKey(container.getId()));
104
105         if (SanityManager.DEBUG)
106         {
107             SanityManager.ASSERT(
108                 lf.isLockHeld(
109                     t.getCompatibilitySpace(), t,
110                     container.getId(),
111                     (forUpdate ? ContainerLock.CX : ContainerLock.CS)),
112                 "Covering table lock (" +
113                 (forUpdate ? ContainerLock.CX : ContainerLock.CS) +
114                 " is not held after lock escalation.");
115         }
116
117         return true;
118     }
119 }
120
Popular Tags