DatabaseConfig dbConfig = new DatabaseConfig();
dbConfig.setTransactional(true);
dbConfig.setAllowCreate(true);
db = env.openDatabase(null, "foo", dbConfig);
StringDbt key = new StringDbt("key1");
StringDbt data = new StringDbt("data1");
txnA = env.beginTransaction(null, TransactionConfig.DEFAULT);
OperationStatus status = db.put(txnA, key, data);
assertEquals(OperationStatus.SUCCESS, status);
/*
* txnA should have a write lock on this record. Now try
* to read-uncommitted it.
*/
DatabaseEntry foundKey = new DatabaseEntry();
DatabaseEntry foundData = new DatabaseEntry();
/*
* Make sure we get a deadlock exception without read-uncommitted.
*/
try {
db.get(null, key, foundData, LockMode.DEFAULT);
fail("Should deadlock");
} catch (DeadlockException e) {
}
/*
* Specify read-uncommitted as a lock mode.
*/
status = db.get(null, key, foundData, LockMode.READ_UNCOMMITTED);
assertEquals(OperationStatus.SUCCESS, status);
assertTrue(Arrays.equals(data.getData(), foundData.getData()));
status = db.getSearchBoth
(null, key, data, LockMode.READ_UNCOMMITTED);
assertEquals(OperationStatus.SUCCESS, status);
cursor = db.openCursor(null, CursorConfig.DEFAULT);
status = cursor.getFirst(foundKey, foundData,
LockMode.READ_UNCOMMITTED);
assertEquals(OperationStatus.SUCCESS, status);
assertTrue(Arrays.equals(key.getData(), foundKey.getData()));
assertTrue(Arrays.equals(data.getData(), foundData.getData()));
cursor.close();
/*
* Specify read-uncommitted through a read-uncommitted txn.
*/
TransactionConfig txnConfig = new TransactionConfig();
txnConfig.setReadUncommitted(true);
Transaction readUncommittedTxn =
env.beginTransaction(null, txnConfig);
status = db.get
(readUncommittedTxn, key, foundData, LockMode.DEFAULT);
assertEquals(OperationStatus.SUCCESS, status);
assertTrue(Arrays.equals(data.getData(), foundData.getData()));
status = db.getSearchBoth
(readUncommittedTxn, key, data,LockMode.DEFAULT);
assertEquals(OperationStatus.SUCCESS, status);
cursor = db.openCursor(readUncommittedTxn, CursorConfig.DEFAULT);
status = cursor.getFirst(foundKey, foundData, LockMode.DEFAULT);
assertEquals(OperationStatus.SUCCESS, status);
assertTrue(Arrays.equals(key.getData(), foundKey.getData()));
assertTrue(Arrays.equals(data.getData(), foundData.getData()));
cursor.close();
readUncommittedTxn.abort();
/*
* Specify read-uncommitted through a read-uncommitted cursor
*/
CursorConfig cursorConfig = new CursorConfig();
cursorConfig.setReadUncommitted(true);
cursor = db.openCursor(null, cursorConfig);
status = cursor.getFirst(foundKey, foundData, LockMode.DEFAULT);
assertEquals(OperationStatus.SUCCESS, status);
assertTrue(Arrays.equals(key.getData(), foundKey.getData()));
assertTrue(Arrays.equals(data.getData(), foundData.getData()));
/*
* Open through the compatiblity method, should accept dirty
* read (but ignores it)
*/