[Mulgara-svn] r564 - branches/mgr-73/src/jar/resolver/java/org/mulgara/resolver

andrae at mulgara.org andrae at mulgara.org
Fri Nov 16 06:19:09 UTC 2007


Author: andrae
Date: 2007-11-16 00:19:08 -0600 (Fri, 16 Nov 2007)
New Revision: 564

Modified:
   branches/mgr-73/src/jar/resolver/java/org/mulgara/resolver/MulgaraTransactionManager.java
Log:
Rewrote the writelock reservation logic - we now track the reserving session
rather than the reserving thread.  This is both possible and necessary now the
reservation requests come from the factories rather than other methods on this
class.



Modified: branches/mgr-73/src/jar/resolver/java/org/mulgara/resolver/MulgaraTransactionManager.java
===================================================================
--- branches/mgr-73/src/jar/resolver/java/org/mulgara/resolver/MulgaraTransactionManager.java	2007-11-16 06:16:39 UTC (rev 563)
+++ branches/mgr-73/src/jar/resolver/java/org/mulgara/resolver/MulgaraTransactionManager.java	2007-11-16 06:19:08 UTC (rev 564)
@@ -71,28 +71,23 @@
 
   // Write lock is associated with a session.
   private DatabaseSession sessionHoldingWriteLock;
-  private MulgaraTransaction currentWritingTransaction;
 
+  // Used to support write-lock reservation.
+  private DatabaseSession sessionReservingWriteLock;
+
   // Used to synchronize access to other fields.
   private ReentrantLock mutex;
   private Condition writeLockCondition;
 
-  // Used to support write-lock reservation.
-  private Condition reserveCondition;
-  private Thread reservingThread;
-
   private MulgaraInternalTransactionFactory internalFactory;
   private MulgaraExternalTransactionFactory externalFactory;
 
   public MulgaraTransactionManager(TransactionManagerFactory transactionManagerFactory) {
     this.sessionHoldingWriteLock = null;
-    this.currentWritingTransaction = null;
+    this.sessionReservingWriteLock = null;
     this.mutex = new ReentrantLock();
     this.writeLockCondition = this.mutex.newCondition();
 
-    this.reserveCondition = this.mutex.newCondition();
-    this.reservingThread = null;
-
     this.internalFactory = new MulgaraInternalTransactionFactory(this, transactionManagerFactory)
     this.externalFactory = new MulgaraExternalTransactionFactory(this)
 
@@ -119,7 +114,7 @@
         return;
       }
 
-      while (writeLockHeld() || writeLockReserved()) {
+      while (writeLockHeld() || (writeLockReserved() && !writeLockReserved(session))) {
         try {
           writeLockCondition.await();
         } catch (InterruptedException ei) {
@@ -174,28 +169,39 @@
    * Used to reserve the write lock during a commit or rollback.
    * Should only be used by a transaction manager.
    */
-  void reserveWriteLock() throws MulgaraTransactionException {
-    if (!mutex.isHeldByCurrentThread()) {
-      throw new IllegalStateException("Attempt to set modify without holding mutex");
+  void reserveWriteLock(DatabaseSession session) throws MulgaraTransactionException {
+    acquireMutex();
+    try {
+      if (session != sessionReservingWriteLock && session != sessionHoldingWriteLock) {
+        throw new IllegalStateException("Attempt to reserve writelock without holding writelock");
+      }
+      if (session != sessionReservingWriteLock && sessionReservingWriteLock != null) {
+        throw new IllegalStateException("Attempt to reserve writelock when writelock already reserved");
+      }
+
+      sessionReservingWriteLock = session;
+    } finally {
+      releaseMutex();
     }
+  }
 
-    if (Thread.currentThread().equals(reservingThread)) {
+  boolean writeLockReserved() {
+    return sessionReservingWriteLock != null;
+  }
+
+  boolean writeLockReserved(DatabaseSession session) {
+    return session == sessionReservingWriteLock;
+  }
+
+  void releaseReserve(DatabaseSession session) {
+    if (!writeLockReserved()) {
       return;
     }
-
-    while (reservingThread != null) {
-      try {
-        reserveCondition.await();
-      } catch (InterruptedException ei) {
-        throw new MulgaraTransactionException("Thread interrupted while reserving write lock", ei);
-      }
+    if (!writeLockReserved(session)) {
+      throw new IllegalStateException("Attempt to release reserve without holding reserve");
     }
-    reservingThread = Thread.currentThread();
-  }
 
-  boolean writeLockReserved() {
-    // TRUE iff there is a reserving thread AND it is different thread to the current thread.
-    return reservingThread != null && !Thread.currentThread().equals(reservingThread);
+    sessionReservingWriteLock = null;
   }
 
   private boolean writeLockHeld() {




More information about the Mulgara-svn mailing list