KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sleepycat > je > txn > ReadCommittedLocker


1 /*-
2  * See the file LICENSE for redistribution information.
3  *
4  * Copyright (c) 2002,2006 Oracle. All rights reserved.
5  *
6  * $Id: ReadCommittedLocker.java,v 1.6 2006/10/30 21:14:27 bostic Exp $
7  */

8
9 package com.sleepycat.je.txn;
10
11 import com.sleepycat.je.DatabaseException;
12 import com.sleepycat.je.dbi.CursorImpl;
13 import com.sleepycat.je.dbi.DatabaseImpl;
14 import com.sleepycat.je.dbi.EnvironmentImpl;
15 import com.sleepycat.je.tree.BIN;
16 import com.sleepycat.je.tree.Key;
17
18 /**
19  * Extends BuddyLocker to acquire write locks using the buddy locker (the
20  * transaction locker). This is used for ReadCommitted (Degree 2) isolation.
21  */

22 public class ReadCommittedLocker extends BuddyLocker {
23
24     /**
25      * Creates a ReadCommittedLocker.
26      * @param buddy is a transactional locker that will be used for acquiring
27      * write locks.
28      */

29     public ReadCommittedLocker(EnvironmentImpl env, Locker buddy)
30         throws DatabaseException {
31
32         /*
33          * If the buddy param is a read-committed locker, reach in to get its
34          * transactional buddy locker.
35          */

36         super(env,
37               (buddy instanceof ReadCommittedLocker) ?
38               ((ReadCommittedLocker) buddy).getBuddy() : buddy);
39
40         assert getBuddy().isTransactional();
41     }
42
43     /**
44      * Creates a new instance of this txn for the same environment. No
45      * transactional locks are held by this object, so no locks are retained.
46      * newNonTxnLocker is also called for the BuddyLocker.
47      */

48     public Locker newNonTxnLocker()
49         throws DatabaseException {
50
51         return new ReadCommittedLocker(envImpl, getBuddy().newNonTxnLocker());
52     }
53
54     /**
55      * Forwards write locks to the buddy locker (the transaction locker).
56      *
57      * @see Locker#lockInternal
58      * @Override
59      */

60     LockResult lockInternal(long nodeId,
61                 LockType lockType,
62                             boolean noWait,
63                             DatabaseImpl database)
64         throws DatabaseException {
65
66         if (lockType.isWriteLock()) {
67             return getBuddy().lockInternal(nodeId, lockType, noWait, database);
68         } else {
69             return super.lockInternal(nodeId, lockType, noWait, database);
70         }
71     }
72
73     /**
74      * Releases the lock from this locker, or if not owned by this locker then
75      * releases it from the buddy locker.
76      */

77     public void releaseLock(long nodeId)
78         throws DatabaseException {
79
80         if (!lockManager.release(nodeId, this)) {
81             lockManager.release(nodeId, getBuddy());
82         }
83     }
84
85     /**
86      * Forwards this method to the transactional buddy. Since the buddy
87      * handles write locks, it knows whether this transaction created the node.
88      */

89     public boolean createdNode(long nodeId)
90         throws DatabaseException {
91
92         return getBuddy().createdNode(nodeId);
93     }
94
95     /**
96      * Forwards this method to the transactional buddy. The buddy handles
97      * write locks and therefore handles abort information.
98      */

99     public long getAbortLsn(long nodeId)
100         throws DatabaseException {
101
102         return getBuddy().getAbortLsn(nodeId);
103     }
104
105     /**
106      * @return the WriteLockInfo for this node.
107      */

108     public WriteLockInfo getWriteLockInfo(long nodeId)
109     throws DatabaseException {
110
111     return getBuddy().getWriteLockInfo(nodeId);
112     }
113
114     /**
115      * Forwards this method to the transactional buddy. The buddy handles
116      * write locks and therefore handles delete information.
117      */

118     public void addDeleteInfo(BIN bin, Key deletedKey)
119         throws DatabaseException {
120
121         getBuddy().addDeleteInfo(bin, deletedKey);
122     }
123
124     /**
125      * Forwards this method to the transactional buddy. The buddy Txn tracks
126      * cursors.
127      */

128     public void registerCursor(CursorImpl cursor)
129         throws DatabaseException {
130
131         getBuddy().registerCursor(cursor);
132     }
133
134     /**
135      * Forwards this method to the transactional buddy. The buddy Txn tracks
136      * cursors.
137      */

138     public void unRegisterCursor(CursorImpl cursor)
139         throws DatabaseException {
140
141         getBuddy().unRegisterCursor(cursor);
142     }
143
144     /**
145      * Is always transactional because the buddy locker is transactional.
146      */

147     public boolean isTransactional() {
148         return true;
149     }
150
151     /**
152      * Is always read-committed isolation.
153      */

154     public boolean isReadCommittedIsolation() {
155         return true;
156     }
157 }
158
Popular Tags