01: /*-
02: * See the file LICENSE for redistribution information.
03: *
04: * Copyright (c) 2002,2008 Oracle. All rights reserved.
05: *
06: * $Id: ThreadLocker.java,v 1.14.2.4 2008/01/07 15:14:17 cwl Exp $
07: */
08:
09: 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) throws DatabaseException {
25:
26: super (env);
27: }
28:
29: /**
30: * Check that this txn is not used in the wrong thread.
31: */
32: protected void checkState(boolean ignoreCalledByAbort)
33: throws DatabaseException {
34:
35: if (thread != Thread.currentThread()) {
36: throw new DatabaseException(
37: "Non-transactional Cursors may not be used in multiple "
38: + "threads; Cursor was created in "
39: + thread + " 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() throws DatabaseException {
49:
50: checkState(false);
51: return new ThreadLocker(envImpl);
52: }
53:
54: /**
55: * Returns whether this locker can share locks with the given locker.
56: * Locks are shared when both are txns are ThreadLocker instances for the
57: * same thread.
58: */
59: public boolean sharesLocksWith(Locker other) {
60:
61: if (super .sharesLocksWith(other)) {
62: return true;
63: } else if (other instanceof ThreadLocker) {
64: return thread == ((ThreadLocker) other).thread;
65: } else {
66: return false;
67: }
68: }
69: }
|