From 05612f5fc3edd6f5d490283a4141964b65366037 Mon Sep 17 00:00:00 2001
From: Matthew Swift <matthew.swift@forgerock.com>
Date: Tue, 28 Jun 2011 11:55:34 +0000
Subject: [PATCH] Fix OPENDJ-219 - Replication server and draft changelog DB code may attempt to reference closed DB

---
 opendj-sdk/opends/src/server/org/opends/server/replication/server/DraftCNDB.java |  107 ++++++++++++++++++++++++++++++++++++++++++++++++++++-
 1 files changed, 104 insertions(+), 3 deletions(-)

diff --git a/opendj-sdk/opends/src/server/org/opends/server/replication/server/DraftCNDB.java b/opendj-sdk/opends/src/server/org/opends/server/replication/server/DraftCNDB.java
index c9f23e5..aaae7b8 100644
--- a/opendj-sdk/opends/src/server/org/opends/server/replication/server/DraftCNDB.java
+++ b/opendj-sdk/opends/src/server/org/opends/server/replication/server/DraftCNDB.java
@@ -103,6 +103,12 @@
       dbCloseLock.readLock().lock();
       try
       {
+        // If the DB has been closed then return immediately.
+        if (isDBClosed())
+        {
+          return;
+        }
+
         txn = dbenv.beginTransaction();
         db.put(txn, key, data);
         txn.commit(Durability.COMMIT_WRITE_NO_SYNC);
@@ -141,6 +147,7 @@
       try
       {
         db.close();
+        db = null;
       }
       finally
       {
@@ -221,6 +228,12 @@
       Cursor cursor = null;
       try
       {
+        // If the DB has been closed then return immediately.
+        if (isDBClosed())
+        {
+          return 0;
+        }
+
         cursor = db.openCursor(null, null);
         DatabaseEntry key = new DatabaseEntry();
         DatabaseEntry entry = new DatabaseEntry();
@@ -252,14 +265,25 @@
    */
   public long count()
   {
+    dbCloseLock.readLock().lock();
     try
     {
+      // If the DB has been closed then return immediately.
+      if (isDBClosed())
+      {
+        return 0L;
+      }
+
       return db.count();
     }
-    catch(Exception e)
+    catch (Exception e)
     {
       TRACER.debugCaught(DebugLogLevel.ERROR, e);
     }
+    finally
+    {
+      dbCloseLock.readLock().unlock();
+    }
     return 0L;
   }
 
@@ -275,6 +299,12 @@
       Cursor cursor = null;
       try
       {
+        // If the DB has been closed then return immediately.
+        if (isDBClosed())
+        {
+          return 0;
+        }
+
         cursor = db.openCursor(null, null);
         DatabaseEntry key = new DatabaseEntry();
         DatabaseEntry entry = new DatabaseEntry();
@@ -351,6 +381,15 @@
 
       try
       {
+        // If the DB has been closed then create empty cursor.
+        if (isDBClosed())
+        {
+          isClosed = true;
+          txn = null;
+          cursor = null;
+          return;
+        }
+
         localCursor = db.openCursor(localTxn, null);
         if (startingDraftCN >= 0)
         {
@@ -410,6 +449,15 @@
       dbCloseLock.readLock().lock();
       try
       {
+        // If the DB has been closed then create empty cursor.
+        if (isDBClosed())
+        {
+          isClosed = true;
+          txn = null;
+          cursor = null;
+          return;
+        }
+
         // Create the transaction that will protect whatever done with this
         // write cursor.
         localTxn = dbenv.beginTransaction();
@@ -516,6 +564,11 @@
      */
     public String currentValue()
     {
+      if (isClosed)
+      {
+        return null;
+      }
+
       try
       {
         if (seqnumData != null)
@@ -536,6 +589,11 @@
      */
     public String currentServiceID()
     {
+      if (isClosed)
+      {
+        return null;
+      }
+
       try
       {
         if (seqnumData != null)
@@ -547,6 +605,7 @@
       {
         TRACER.debugCaught(DebugLogLevel.ERROR, e);
       }
+
       return null;
     }
 
@@ -558,16 +617,22 @@
      */
     public int currentKey()
     {
-       try
+      if (isClosed)
+      {
+        return -1;
+      }
+
+      try
       {
         String str = decodeUTF8(key.getData());
         int draftCN = new Integer(str);
         return draftCN;
       }
-      catch(Exception e)
+      catch (Exception e)
       {
         TRACER.debugCaught(DebugLogLevel.ERROR, e);
       }
+
       return -1;
     }
 
@@ -577,6 +642,11 @@
      */
     public ChangeNumber currentChangeNumber()
     {
+      if (isClosed)
+      {
+        return null;
+      }
+
       try
       {
         if (seqnumData != null)
@@ -598,6 +668,11 @@
      */
     public boolean next() throws DatabaseException
     {
+      if (isClosed)
+      {
+        return false;
+      }
+
       OperationStatus status = cursor.getNext(key, entry, LockMode.DEFAULT);
       if (status != OperationStatus.SUCCESS)
       {
@@ -621,6 +696,11 @@
      */
     public void delete() throws DatabaseException
     {
+      if (isClosed)
+      {
+        throw new IllegalStateException("DraftCNDB already closed");
+      }
+
       cursor.delete();
     }
 
@@ -631,6 +711,11 @@
      */
     public DatabaseEntry getKey()
     {
+      if (isClosed)
+      {
+        return null;
+      }
+
       return key;
     }
   }
@@ -647,10 +732,17 @@
     dbCloseLock.writeLock().lock();
     try
     {
+      // If the DB has been closed then return immediately.
+      if (isDBClosed())
+      {
+        return;
+      }
+
       String dbName = db.getDatabaseName();
 
       // Closing is requested by the Berkeley DB before truncate
       db.close();
+      db = null; // In case there's a failure between here and recreation.
 
       // Clears the changes
       dbenv.clearDb(dbName);
@@ -672,4 +764,13 @@
       dbCloseLock.writeLock().unlock();
     }
   }
+
+
+
+  //Returns {@code true} if the DB is closed. This method assumes that either
+  // the db read/write lock has been taken.
+  private boolean isDBClosed()
+  {
+    return db == null;
+  }
 }

--
Gitblit v1.10.0