From e51bf9ccc4c08a9f93f4e0295bd334bbd3b747ca Mon Sep 17 00:00:00 2001
From: Jean-Noel Rouvignac <jean-noel.rouvignac@forgerock.com>
Date: Tue, 17 Sep 2013 13:02:22 +0000
Subject: [PATCH] OPENDJ-1116 Introduce abstraction for the changelog DB

---
 opends/src/server/org/opends/server/replication/server/changelog/je/DraftCNDB.java |   32 ++++++++++++++++++++++----------
 1 files changed, 22 insertions(+), 10 deletions(-)

diff --git a/opends/src/server/org/opends/server/replication/server/changelog/je/DraftCNDB.java b/opends/src/server/org/opends/server/replication/server/changelog/je/DraftCNDB.java
index aa4c1a7..50ace70 100644
--- a/opends/src/server/org/opends/server/replication/server/changelog/je/DraftCNDB.java
+++ b/opends/src/server/org/opends/server/replication/server/changelog/je/DraftCNDB.java
@@ -377,6 +377,7 @@
       // unlock it when throwing an exception.
       dbCloseLock.readLock().lock();
 
+      boolean cursorHeld = false;
       Cursor localCursor = null;
       try
       {
@@ -423,18 +424,21 @@
 
         this.txn = null;
         this.cursor = localCursor;
+        cursorHeld = true;
       }
       catch (DatabaseException e)
       {
-        // Unlocking is required before throwing any exception
-        closeLockedCursor(localCursor);
         throw new ChangelogException(e);
       }
-      catch (ChangelogException e)
+      finally
       {
-        // Unlocking is required before throwing any exception
-        closeLockedCursor(localCursor);
-        throw e;
+        if (!cursorHeld)
+        {
+          // Do not keep a readLock on the DB when this class does not hold a DB
+          // cursor. Either an exception was thrown or no cursor could be opened
+          // for some reason.
+          closeLockedCursor(localCursor);
+        }
       }
     }
 
@@ -446,6 +450,7 @@
       this.key = new ReplicationDraftCNKey();
 
       // We'll go on only if no close or no clear is running
+      boolean cursorHeld = false;
       dbCloseLock.readLock().lock();
       try
       {
@@ -465,23 +470,30 @@
 
         this.txn = localTxn;
         this.cursor = localCursor;
+        cursorHeld = true;
       }
       catch (DatabaseException e)
       {
         TRACER.debugCaught(DebugLogLevel.ERROR, e);
-
-        closeLockedCursor(localCursor);
         DraftCNDB.abort(localTxn);
         throw new ChangelogException(e);
       }
       catch (ChangelogException e)
       {
         TRACER.debugCaught(DebugLogLevel.ERROR, e);
-
-        closeLockedCursor(localCursor);
         DraftCNDB.abort(localTxn);
         throw e;
       }
+      finally
+      {
+        if (!cursorHeld)
+        {
+          // Do not keep a readLock on the DB when this class does not hold a DB
+          // cursor. Either an exception was thrown or no cursor could be opened
+          // for some reason.
+          closeLockedCursor(localCursor);
+        }
+      }
     }
 
     /**

--
Gitblit v1.10.0