KickJava   Java API By Example, From Geeks To Geeks.

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


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

40 public class ContainerLocking2 extends NoLocking {
41
42     private final LockFactory lf;
43
44     protected ContainerLocking2()
45     {
46         this.lf = null;
47     }
48
49     protected ContainerLocking2(LockFactory lf)
50     {
51         this.lf = lf;
52     }
53
54     /**
55         Obtain a Container shared or exclusive lock until
56         the end of the nested transaction.
57
58         @exception StandardException Standard Cloudscape error policy
59     */

60     public boolean lockContainer(
61     Transaction t,
62     ContainerHandle container,
63     boolean waitForLock,
64     boolean forUpdate)
65         throws StandardException
66     {
67         Object JavaDoc qualifier = forUpdate ? ContainerLock.CX : ContainerLock.CS;
68
69         // for cursor stability put read locks on a separate lock chain, which
70
// will be released when the container is unlocked.
71
Object JavaDoc group =
72             forUpdate ? t : container.getUniqueId();
73
74         return(
75             lf.lockObject(
76                 t.getCompatibilitySpace(), group, container.getId(), qualifier,
77                 waitForLock ?
78                     C_LockFactory.TIMED_WAIT : C_LockFactory.NO_WAIT));
79     }
80
81     /**
82      * Unlock read locks.
83      * <p>
84      * In Cursor stability release all read locks obtained. unlockContainer()
85      * will be called when the container is closed.
86      * <p>
87      *
88      * @param t The transaction to associate the lock with.
89      * @param container Container to unlock.
90      *
91      **/

92     public void unlockContainer(
93     Transaction t,
94     ContainerHandle container)
95     {
96         // Only release read locks before end of transaction in level 2.
97
if (container.isReadOnly())
98         {
99             lf.unlockGroup(t.getCompatibilitySpace(), container.getUniqueId());
100         }
101     }
102
103     public int getMode() {
104         return MODE_CONTAINER;
105     }
106
107
108     /*
109     ** We can inherit all the others methods of NoLocking since we hold the
110     ** container lock until the end of transaction, and we don't obtain row
111     ** locks.
112     */

113 }
114
Popular Tags