001: /*
002: * Jalisto - JAva LIght STOrage
003: * Copyright (C) 2000-2005 Xcalia http://www.xcalia.com
004: *
005: * This library is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU Lesser General Public
007: * License as published by the Free Software Foundation; either
008: * version 2.1 of the License, or (at your option) any later version.
009: *
010: * This library is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013: * Lesser General Public License for more details.
014: *
015: * You should have received a copy of the GNU Lesser General Public
016: * License along with this library; if not, write to the Free Software
017: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
018: *
019: * Xcalia
020: * 71, rue Desnouettes
021: * 75014 Paris - France
022: * http://www.xcalia.com
023: */
024: package org.objectweb.jalisto.se.impl.lock;
025:
026: import org.objectweb.jalisto.se.impl.LogicalOid;
027: import org.objectweb.jalisto.se.api.internal.DataWrapper;
028:
029: import java.util.Iterator;
030:
031: public class LockManagerPessimistic extends LockManagerDefault {
032:
033: public void prepareCreateObject(LogicalOid floid,
034: DataWrapper objectToCreate) {
035: takeLock(floid, WRITE);
036: }
037:
038: public void prepareDeleteObjectByOid(LogicalOid floid, short update) {
039: takeLock(floid, WRITE).addTransactionnalValue(sessionId, null,
040: ObjectTransactionnalState.DELETE_ACTION, (short) 0);
041: }
042:
043: public void prepareReadObjectByOid(LogicalOid floid) {
044: takeLock(floid, READ);
045: }
046:
047: public void prepareUpdateObjectByOid(LogicalOid floid,
048: DataWrapper objectToUpdate) {
049: takeLock(floid, WRITE);
050: }
051:
052: public void finishCreateObject(LogicalOid floid,
053: DataWrapper objectToCreate) {
054: lockTable.getLock(floid).addTransactionnalValue(sessionId,
055: objectToCreate,
056: ObjectTransactionnalState.CREATE_ACTION, (short) 0);
057: }
058:
059: public void finishReadObjectByOid(LogicalOid floid,
060: DataWrapper objectToRead, short update) {
061: lockTable.getLock(floid).addTransactionnalValue(sessionId,
062: objectToRead, ObjectTransactionnalState.READ_ACTION,
063: update);
064: }
065:
066: public void finishUpdateObjectByOid(LogicalOid floid,
067: DataWrapper objectToUpdate, short update) {
068: takeLock(floid, WRITE).addTransactionnalValue(sessionId,
069: objectToUpdate,
070: ObjectTransactionnalState.UPDATE_ACTION, update);
071: }
072:
073: public synchronized void commit() {
074: try {
075: accessController
076: .getAccessControl(AccessController.COMMIT_ROLLBACK_LOCKMANAGER);
077: Iterator floids = lockTable.getKeys();
078: while (floids.hasNext()) {
079: LogicalOid floid = (LogicalOid) floids.next();
080: Lock lock = lockTable.getLockOrCreate(floid);
081: if (lock.hasModifiedValueWith(sessionId)) {
082: if (lock.hasWriteLockWith(sessionId)) {
083: if (lock.getCurrentAction(sessionId) == ObjectTransactionnalState.CREATE_ACTION) {
084: session.createObjectCommit(floid);
085: } else if (lock.getCurrentAction(sessionId) == ObjectTransactionnalState.UPDATE_ACTION) {
086: lock.update(sessionId);
087: session.updateObjectCommit(floid);
088: } else if (lock.getCurrentAction(sessionId) == ObjectTransactionnalState.DELETE_ACTION) {
089: session.deleteObjectCommit(floid);
090: } else if (lock.getCurrentAction(sessionId) == ObjectTransactionnalState.NEW_OID_ACTION) {
091: session.makeNewFileOidCommit(floid);
092: }
093: }
094: lock.releaseAllLock(sessionId);
095: lockTable.removeLockByFloid(floid);
096: }
097: }
098: session.getFileAccess().getPhysicalAccess().commit();
099: } finally {
100: accessController
101: .releaseAccessControl(AccessController.COMMIT_ROLLBACK_LOCKMANAGER);
102: }
103: }
104:
105: public synchronized void rollback() {
106: try {
107: accessController
108: .getAccessControl(AccessController.COMMIT_ROLLBACK_LOCKMANAGER);
109: Iterator floids = lockTable.getKeys();
110: while (floids.hasNext()) {
111: LogicalOid floid = (LogicalOid) floids.next();
112: Lock lock = lockTable.getLockOrCreate(floid);
113: if (lock.hasModifiedValueWith(sessionId)) {
114: if (lock.hasWriteLockWith(sessionId)) {
115: if (lock.getCurrentAction(sessionId) == ObjectTransactionnalState.CREATE_ACTION) {
116: session.createObjectRollback(floid);
117: } else if (lock.getCurrentAction(sessionId) == ObjectTransactionnalState.UPDATE_ACTION) {
118: session.updateObjectRollback(floid);
119: } else if (lock.getCurrentAction(sessionId) == ObjectTransactionnalState.DELETE_ACTION) {
120: session.deleteObjectRollback(floid);
121: } else if (lock.getCurrentAction(sessionId) == ObjectTransactionnalState.NEW_OID_ACTION) {
122: session.makeNewFileOidRollback(floid);
123: }
124: }
125: lock.releaseAllLock(sessionId);
126: lockTable.removeLockByFloid(floid);
127: }
128: }
129: session.getFileAccess().rollback(sessionId);
130: } finally {
131: accessController
132: .releaseAccessControl(AccessController.COMMIT_ROLLBACK_LOCKMANAGER);
133: }
134: }
135:
136: }
|