// Thread method that writes a series of records
// to the database using transaction protection.
// Deadlock handling is demonstrated here.
public void run () {
Transaction txn = null;
// Perform 50 transactions
for (int i=0; i<50; i++) {
boolean retry = true;
int retry_count = 0;
// while loop is used for deadlock retries
while (retry) {
// try block used for deadlock detection and
// general db exception handling
try {
// Get a transaction
txn = myEnv.beginTransaction(null, null);
// Write 10 records to the db
// for each transaction
for (int j = 0; j < 10; j++) {
// Get the key
DatabaseEntry key = new DatabaseEntry();
StringBinding.stringToEntry(keys[j], key);
// Get the data
PayloadData pd = new PayloadData(i+j, getName(),
generator.nextDouble());
DatabaseEntry data = new DatabaseEntry();
dataBinding.objectToEntry(pd, data);
// Do the put
myDb.put(txn, key, data);
}
// commit
System.out.println(getName() + " : committing txn : " + i);
// This code block allows us to decide if txn handle is
// passed to countRecords()
//
// TxnGuideInMemory requires a txn handle be handed to
// countRecords(). The code self deadlocks if you don't.
// TxnGuide has no such requirement because it supports
// uncommitted reads.
Transaction txnHandle = null;
if (passTxn) { txnHandle = txn; }
System.out.println(getName() + " : Found " +
countRecords(txnHandle) + " records in the database.");
try {