001: // You can redistribute this software and/or modify it under the terms of
002: // the Ozone Core License version 1 published by ozone-db.org.
003: //
004: // The original code and portions created by SMB are
005: // Copyright (C) 1997-@year@ by SMB GmbH. All rights reserved.
006: //
007: // $Id: ExclusiveLock.java,v 1.2 2002/06/08 00:49:38 mediumnet Exp $
008:
009: package org.ozoneDB.core;
010:
011: import java.io.*;
012: import org.ozoneDB.*;
013: import org.ozoneDB.DxLib.*;
014: import org.ozoneDB.util.*;
015:
016: /**
017: * This class implements an exclusive lock policy. Only one transaction can hold
018: * this lock at one time.
019: *
020: *
021: * @author <a href="http://www.softwarebuero.de/">SMB</a>
022: * @version $Revision: 1.2 $Date: 2002/06/08 00:49:38 $
023: */
024: public final class ExclusiveLock extends AbstractLock {
025:
026: protected final static long serialVersionUID = 1;
027: protected final static byte subSerialVersionUID = 1;
028:
029: private int level = LEVEL_NONE;
030:
031: /**
032: * ID of the Transaction that holds this lock or null.
033: */
034: public TransactionID locker;
035:
036: public ExclusiveLock() {
037: reset();
038: }
039:
040: public synchronized void reset() {
041: level = LEVEL_NONE;
042: locker = null;
043: }
044:
045: public int tryAcquire(Transaction ta, int newLevel) {
046: if (false && ta.env.logWriter.hasTarget(LogWriter.DEBUG3)) {
047: ta.env.logWriter.newEntry(this , "tryAcquire(): current:"
048: + level + " new:" + newLevel, LogWriter.DEBUG3);
049: }
050: synchronized (this ) {
051: int prevLevel = level(ta);
052: if (locker == null || isAcquiredBy(ta)) {
053: locker = ta.taID();
054: level = newLevel > level ? newLevel : level;
055: return prevLevel;
056: } else {
057: return NOT_ACQUIRED;
058: }
059: }
060: }
061:
062: public void release(Transaction ta) {
063: if (false && ta.env.logWriter.hasTarget(LogWriter.DEBUG3)) {
064: ta.env.logWriter.newEntry(this , "release()",
065: LogWriter.DEBUG3);
066: }
067: synchronized (this ) {
068: if (!isAcquiredBy(ta)) {
069: ta.env.logWriter
070: .newEntry(
071: this ,
072: "release(): specified transaction has not aquired lock.",
073: LogWriter.WARN);
074: }
075: locker = null;
076: level = LEVEL_NONE;
077: }
078: }
079:
080: public boolean isAcquiredBy(Transaction ta) {
081: return locker != null ? locker.equals(ta.taID()) : false;
082: }
083:
084: public DxCollection lockerIDs() {
085: DxBag result = new DxArrayBag();
086: if (locker != null) {
087: result.add(locker);
088: }
089: return result;
090: }
091:
092: public int level(Transaction ta) {
093: if (ta != null) {
094: return isAcquiredBy(ta) ? level : LEVEL_NONE;
095: } else {
096: return level;
097: }
098: }
099:
100: public TransactionID getLocker() {
101: return locker;
102: }
103: }
|