1 | /* |
2 | |
3 | Derby - Class org.apache.derby.impl.store.raw.xact.ContainerLocking2 |
4 | |
5 | Copyright 1998, 2004 The Apache Software Foundation or its licensors, as applicable. |
6 | |
7 | Licensed under the Apache License, Version 2.0 (the "License"); |
8 | you may not use this file except in compliance with the License. |
9 | You may obtain a copy of the License at |
10 | |
11 | http://www.apache.org/licenses/LICENSE-2.0 |
12 | |
13 | Unless required by applicable law or agreed to in writing, software |
14 | distributed under the License is distributed on an "AS IS" BASIS, |
15 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
16 | See the License for the specific language governing permissions and |
17 | limitations under the License. |
18 | |
19 | */ |
20 | |
21 | package org.apache.derby.impl.store.raw.xact; |
22 | |
23 | import org.apache.derby.iapi.services.locks.LockFactory; |
24 | import org.apache.derby.iapi.services.locks.C_LockFactory; |
25 | |
26 | import org.apache.derby.iapi.store.raw.ContainerHandle; |
27 | import org.apache.derby.iapi.store.raw.ContainerLock; |
28 | import org.apache.derby.iapi.store.raw.Transaction; |
29 | |
30 | import org.apache.derby.iapi.error.StandardException; |
31 | |
32 | |
33 | /** |
34 | A locking policy that implements container level locking with |
35 | isolation degree 2. |
36 | |
37 | @see org.apache.derby.iapi.store.raw.LockingPolicy |
38 | */ |
39 | public class ContainerLocking2 extends NoLocking { |
40 | |
41 | private final LockFactory lf; |
42 | |
43 | protected ContainerLocking2() |
44 | { |
45 | this.lf = null; |
46 | } |
47 | |
48 | protected ContainerLocking2(LockFactory lf) |
49 | { |
50 | this.lf = lf; |
51 | } |
52 | |
53 | /** |
54 | Obtain a Container shared or exclusive lock until |
55 | the end of the nested transaction. |
56 | |
57 | @exception StandardException Standard Cloudscape error policy |
58 | */ |
59 | public boolean lockContainer( |
60 | Transaction t, |
61 | ContainerHandle container, |
62 | boolean waitForLock, |
63 | boolean forUpdate) |
64 | throws StandardException |
65 | { |
66 | Object qualifier = forUpdate ? ContainerLock.CX : ContainerLock.CS; |
67 | |
68 | // for cursor stability put read locks on a separate lock chain, which |
69 | // will be released when the container is unlocked. |
70 | Object group = |
71 | forUpdate ? t : container.getUniqueId(); |
72 | |
73 | return( |
74 | lf.lockObject( |
75 | t.getCompatibilitySpace(), group, container.getId(), qualifier, |
76 | waitForLock ? |
77 | C_LockFactory.TIMED_WAIT : C_LockFactory.NO_WAIT)); |
78 | } |
79 | |
80 | /** |
81 | * Unlock read locks. |
82 | * <p> |
83 | * In Cursor stability release all read locks obtained. unlockContainer() |
84 | * will be called when the container is closed. |
85 | * <p> |
86 | * |
87 | * @param t The transaction to associate the lock with. |
88 | * @param container Container to unlock. |
89 | * |
90 | **/ |
91 | public void unlockContainer( |
92 | Transaction t, |
93 | ContainerHandle container) |
94 | { |
95 | // Only release read locks before end of transaction in level 2. |
96 | if (container.isReadOnly()) |
97 | { |
98 | lf.unlockGroup(t.getCompatibilitySpace(), container.getUniqueId()); |
99 | } |
100 | } |
101 | |
102 | public int getMode() { |
103 | return MODE_CONTAINER; |
104 | } |
105 | |
106 | |
107 | /* |
108 | ** We can inherit all the others methods of NoLocking since we hold the |
109 | ** container lock until the end of transaction, and we don't obtain row |
110 | ** locks. |
111 | */ |
112 | } |