Skip to content

Instantly share code, notes, and snippets.

@afeinberg
Created December 7, 2009 17:51
Show Gist options
  • Save afeinberg/250968 to your computer and use it in GitHub Desktop.
Save afeinberg/250968 to your computer and use it in GitHub Desktop.
diff --git a/src/java/voldemort/store/bdb/BdbStorageEngine.java b/src/java/voldemort/store/bdb/BdbStorageEngine.java
index caa38ca..986a543 100644
--- a/src/java/voldemort/store/bdb/BdbStorageEngine.java
+++ b/src/java/voldemort/store/bdb/BdbStorageEngine.java
@@ -144,12 +144,17 @@ public class BdbStorageEngine implements StorageEngine<ByteArray, byte[]> {
Cursor cursor = null;
try {
cursor = bdbDatabase.openCursor(null, null);
- return get(cursor, key, lockMode, serializer);
+ List<T> result = get(cursor, key, lockMode, serializer);
+
+ attemptClose(cursor);
+ cursor = null;
+
+ return result;
} catch(DatabaseException e) {
logger.error(e);
throw new PersistenceFailureException(e);
} finally {
- attemptClose(cursor);
+ closeAndAbortSilently(cursor, null);
}
}
@@ -168,11 +173,14 @@ public class BdbStorageEngine implements StorageEngine<ByteArray, byte[]> {
if(!values.isEmpty())
result.put(key, values);
}
+
+ attemptClose(cursor);
+ cursor = null;
} catch(DatabaseException e) {
logger.error(e);
throw new PersistenceFailureException(e);
} finally {
- attemptClose(cursor);
+ closeAndAbortSilently(cursor, null);
}
return result;
}
@@ -199,7 +207,6 @@ public class BdbStorageEngine implements StorageEngine<ByteArray, byte[]> {
StoreUtils.assertValidKey(key);
DatabaseEntry keyEntry = new DatabaseEntry(key.get());
- boolean succeeded = false;
Transaction transaction = null;
Cursor cursor = null;
try {
@@ -233,17 +240,17 @@ public class BdbStorageEngine implements StorageEngine<ByteArray, byte[]> {
OperationStatus status = cursor.put(keyEntry, valueEntry);
if(status != OperationStatus.SUCCESS)
throw new PersistenceFailureException("Put operation failed with status: " + status);
- succeeded = true;
-
+
+ attemptClose(cursor);
+ cursor = null;
+ attemptCommit(transaction);
+ // null out the transaction to avoid an abort if no exception is thrown
+ transaction = null;
} catch(DatabaseException e) {
logger.error(e);
throw new PersistenceFailureException(e);
} finally {
- attemptClose(cursor);
- if(succeeded)
- attemptCommit(transaction);
- else
- attemptAbort(transaction);
+ closeAndAbortSilently(cursor, transaction);
}
}
@@ -268,16 +275,18 @@ public class BdbStorageEngine implements StorageEngine<ByteArray, byte[]> {
}
status = cursor.getNextDup(keyEntry, valueEntry, LockMode.READ_UNCOMMITTED);
}
+
+ attemptClose(cursor);
+ cursor = null;
+ attemptCommit(transaction);
+ // null out the transaction to avoid an abort if no exception is thrown
+ transaction = null;
return deletedSomething;
} catch(DatabaseException e) {
logger.error(e);
throw new PersistenceFailureException(e);
} finally {
- try {
- attemptClose(cursor);
- } finally {
- attemptCommit(transaction);
- }
+ closeAndAbortSilently(cursor, transaction);
}
}
@@ -308,21 +317,11 @@ public class BdbStorageEngine implements StorageEngine<ByteArray, byte[]> {
}
}
- private void attemptAbort(Transaction transaction) {
- try {
- if(transaction != null)
- transaction.abort();
- } catch(Exception e) {
- logger.error("Abort failed!", e);
- }
- }
-
private void attemptCommit(Transaction transaction) {
try {
transaction.commit();
} catch(DatabaseException e) {
logger.error("Transaction commit failed!", e);
- attemptAbort(transaction);
throw new PersistenceFailureException(e);
}
}
@@ -337,6 +336,20 @@ public class BdbStorageEngine implements StorageEngine<ByteArray, byte[]> {
}
}
+ private static void closeAndAbortSilently(Cursor cursor, Transaction transaction) {
+ try {
+ try {
+ if(cursor != null)
+ cursor.close();
+ } finally {
+ if(transaction != null)
+ transaction.abort();
+ }
+ } catch(Exception e) {
+ logger.error("Error closing cursor or aborting transaction.", e);
+ }
+ }
+
public DatabaseStats getStats(boolean setFast) {
try {
StatsConfig config = new StatsConfig();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment