KickJava   Java API By Example, From Geeks To Geeks.

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


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

8
9 package com.sleepycat.je.txn;
10
11 import com.sleepycat.je.DatabaseException;
12 import com.sleepycat.je.dbi.EnvironmentImpl;
13
14 /**
15  * Extends BasicLocker to share locks with another specific locker.
16  *
17  * <p>In general, a BuddyLocker can be used whenever the primary (API) locker
18  * is in use, and we need to lock a node and release that lock before the
19  * primary locker transaction ends. In other words, for this particular lock
20  * we don't want to use two-phase locking. To accomplish that we use a
21  * separate BuddyLocker instance to hold the lock, while sharing locks with the
22  * primary locker. The BuddyLocker can be closed to release this particular
23  * lock, without releasing the other locks held by the primary locker.</p>
24  *
25  * <p>In particular, a BuddyLocker is used when acquiring a RANGE_INSERT lock.
26  * RANGE_INSERT only needs to be held until the point we have inserted the new
27  * node into the BIN. A separate locker is therefore used so we can release
28  * that lock separately when the insertion into the BIN is complete. But the
29  * RANGE_INSERT lock must not conflict with locks held by the primary locker.
30  * So a BuddyLocker is used that shares locks with the primary locker.</p>
31  */

32 public class BuddyLocker extends BasicLocker {
33
34     private Locker buddy;
35
36     /**
37      * Creates a BuddyLocker.
38      */

39     public BuddyLocker(EnvironmentImpl env, Locker buddy)
40         throws DatabaseException {
41
42         super(env);
43         this.buddy = buddy;
44     }
45
46     /**
47      * Returns the buddy locker.
48      */

49     Locker getBuddy() {
50         return buddy;
51     }
52
53     /**
54      * Forwards this call to the buddy locker.
55      */

56     public Txn getTxnLocker() {
57         return buddy.getTxnLocker();
58     }
59
60     /**
61      * Creates a new instance of this txn for the same environment. No
62      * transactional locks are held by this object, so no locks are retained.
63      * newNonTxnLocker is also called for the BuddyLocker.
64      */

65     public Locker newNonTxnLocker()
66         throws DatabaseException {
67
68         return new BuddyLocker(envImpl, buddy.newNonTxnLocker());
69     }
70
71     /**
72      * Forwards this call to the base class and to the buddy locker.
73      */

74     public void releaseNonTxnLocks()
75         throws DatabaseException {
76
77         super.releaseNonTxnLocks();
78         buddy.releaseNonTxnLocks();
79     }
80
81     /**
82      * Returns whether this locker can share locks with the given locker.
83      */

84     public boolean sharesLocksWith(Locker other) {
85
86         if (super.sharesLocksWith(other)) {
87             return true;
88         } else {
89             return buddy == other;
90         }
91     }
92 }
93
Popular Tags