/*********************************************************************************** * @author John Miller * @version 1.0 * @date Sat Aug 23 13:21:52 EDT 2014 */ package transj; import java.util.List; import static java.lang.System.out; import static java.util.Arrays.asList; import static transj.Database.data; import static transj.Database.ltab; /*********************************************************************************** * Abstract representation of a database as a collection of data objects and a * lock table. */ class Database { /** the lock table to control concurrent access to the database */ final static LockTable ltab = new LockTable (); /** the abstract database representation (an array of double objects) */ final static double [] data = new double [100]; } // Database class /*********************************************************************************** * This class models database transactions. */ class Transaction extends Thread implements ReadWrite { private final int tid; // the transaction identifier private final Schedule s; // the schedule of read/write operations making up the transaction /******************************************************************************* * Construct a transaction object. * @param tid the transaction identifier * @param s the schedule of read/write operations making up the transaction */ public Transaction (int tid_, Schedule s_) { tid = tid_; s = s_; } // constructor /******************************************************************************* * Run the transaction: begin, reads/writes, commit. */ public void run () { double value = 0.0; begin (); for (Op op : s.get ()) { if (op.typ == r) { ltab.rl (tid, op.oid); value = read (op.oid); } else { ltab.wl (tid, op.oid); write (op.oid, value + 1.0); } // if } // for for (Op op : s.get ()) ltab.ul (op.tid, op.oid); commit (); } // run /******************************************************************************* * Begin this transaction. */ public void begin () { try { Thread.sleep (5); } catch (InterruptedException ex) {} out.println ("begin transaction " + tid); } // begin /******************************************************************************* * Read data object oid. * @param oid the database object */ public double read (int oid) { try { Thread.sleep (10); } catch (InterruptedException ex) {} double value = data[oid]; out.println ("read " + tid + " ( " + oid + " ) value = " + value); return value; } // read /******************************************************************************* * Write data object oid. * @param oid the database object */ public void write (int oid, double value) { try { Thread.sleep (15); } catch (InterruptedException ex) {} out.println ("write " + tid + " ( " + oid + " ) value = " + value); data[oid] = value; } // write /******************************************************************************* * Commit this transaction. */ public void commit () { try { Thread.sleep (20); } catch (InterruptedException ex) {} out.println ("commit transaction " + tid); } // commit /******************************************************************************* * Test the Transaction class by running several concurrent transactions/threads. */ public static void main (String [] args) { out.println ("Test Transactions"); List list1 = asList (new Op (w, 1, 0), new Op (w, 1, 1)); Transaction t1 = new Transaction (1, new Schedule (list1)); List list2 = asList (new Op (w, 2, 0), new Op (w, 2, 1)); Transaction t2 = new Transaction (2, new Schedule (list2)); t1.start (); t2.start (); } // main } // Transaction class