KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ozoneDB > core > ExclusiveLock


1 // You can redistribute this software and/or modify it under the terms of
2
// the Ozone Core License version 1 published by ozone-db.org.
3
//
4
// The original code and portions created by SMB are
5
// Copyright (C) 1997-@year@ by SMB GmbH. All rights reserved.
6
//
7
// $Id: ExclusiveLock.java,v 1.4 2002/09/18 06:54:14 per_nyfelt Exp $
8

9 package org.ozoneDB.core;
10
11 import org.ozoneDB.DxLib.*;
12 import org.ozoneDB.util.LogWriter;
13
14
15 /**
16  * This class implements an exclusive lock policy. Only one transaction can hold
17  * this lock at one time.
18  *
19  *
20  * @author <a HREF="http://www.softwarebuero.de/">SMB</a>
21  * @version $Revision: 1.4 $Date: 2002/09/18 06:54:14 $
22  */

23 public final class ExclusiveLock extends AbstractLock {
24
25     protected final static long serialVersionUID = 1;
26     protected final static byte subSerialVersionUID = 1;
27
28     private int level = LEVEL_NONE;
29
30     /**
31      * ID of the Transaction that holds this lock or null.
32      */

33     public TransactionID locker;
34
35
36     public ExclusiveLock() {
37         reset();
38     }
39
40
41     public synchronized void reset() {
42         level = LEVEL_NONE;
43         locker = null;
44     }
45
46
47     public int tryAcquire( Transaction ta, int newLevel ) {
48         if (false&&ta.env.logWriter.hasTarget( LogWriter.DEBUG3 )) {
49             ta.env.logWriter.newEntry( this, "tryAcquire(): current:" + level + " new:" + newLevel, LogWriter.DEBUG3 );
50         }
51         synchronized (this) {
52             int prevLevel = level( ta );
53             if (locker == null || isAcquiredBy( ta )) {
54                 locker = ta.taID();
55                 level = newLevel > level ? newLevel : level;
56                 return prevLevel;
57             } else {
58                 return NOT_ACQUIRED;
59             }
60         }
61     }
62
63
64     public void release( Transaction ta ) {
65         if (false&&ta.env.logWriter.hasTarget( LogWriter.DEBUG3 )) {
66             ta.env.logWriter.newEntry( this, "release()", LogWriter.DEBUG3 );
67         }
68         synchronized (this) {
69             if (!isAcquiredBy( ta )) {
70                 ta.env.logWriter.newEntry( this, "release(): specified transaction has not aquired lock.", LogWriter.WARN );
71             }
72             locker = null;
73             level = LEVEL_NONE;
74         }
75     }
76
77
78     public boolean isAcquiredBy( Transaction ta ) {
79         return locker != null ? locker.equals( ta.taID() ) : false;
80     }
81
82
83     public DxCollection lockerIDs() {
84         DxBag result = new DxArrayBag();
85         if (locker != null) {
86             result.add( locker );
87         }
88         return result;
89     }
90
91
92     public int level( Transaction ta ) {
93         if (ta != null) {
94             return isAcquiredBy( ta ) ? level : LEVEL_NONE;
95         } else {
96             return level;
97         }
98     }
99
100     public TransactionID getLocker() {
101         return locker;
102     }
103 }
104
Popular Tags