KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*-
2  * See the file LICENSE for redistribution information.
3  *
4  * Copyright (c) 2002,2006 Oracle. All rights reserved.
5  *
6  * $Id: ThreadLocker.java,v 1.14 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.EnvironmentImpl;
13
14 /**
15  * Extends BasicLocker to share locks among all lockers for the same thread.
16  * This locker is used when a JE entry point is called with a null transaction
17  * parameter.
18  */

19 public class ThreadLocker extends BasicLocker {
20
21     /**
22      * Creates a ThreadLocker.
23      */

24     public ThreadLocker(EnvironmentImpl env)
25         throws DatabaseException {
26
27         super(env);
28     }
29
30     /**
31      * Check that this txn is not used in the wrong thread.
32      */

33     protected void checkState(boolean ignoreCalledByAbort)
34         throws DatabaseException {
35
36         if (thread != Thread.currentThread()) {
37             throw new DatabaseException("A per-thread transaction was" +
38                                         " created in " + thread +
39                                         " but used in " +
40                                         Thread.currentThread());
41         }
42     }
43
44     /**
45      * Creates a new instance of this txn for the same environment. No
46      * transactional locks are held by this object, so no locks are retained.
47      */

48     public Locker newNonTxnLocker()
49         throws DatabaseException {
50
51         checkState(false);
52         return new ThreadLocker(envImpl);
53     }
54
55     /**
56      * Returns whether this locker can share locks with the given locker.
57      * Locks are shared when both are txns are ThreadLocker instances for the
58      * same thread.
59      */

60     public boolean sharesLocksWith(Locker other) {
61
62         if (super.sharesLocksWith(other)) {
63             return true;
64         } else if (other instanceof ThreadLocker) {
65             return thread == ((ThreadLocker) other).thread;
66         } else {
67             return false;
68         }
69     }
70 }
71
Popular Tags