/*********************************************************************************** * @author John Miller * @version 1.0 * @date Thu Feb 11 12:38:17 EST 2010 */ package trans /*********************************************************************************** * Abstract representation of a database as a collection of data objects and a * lock table. */ object Database { /** The lock table to control concurrent access to the database */ val ltab = new LockTable () /** The abstract database representation (an array of double objects) */ val data = new Array [Double] (100) } // Database object /*********************************************************************************** * This class models database transactions. * @param tid the transaction identifier * @param s the schedule of read/write operations making up the transaction */ class Transaction (tid: Int, s: Schedule) extends Thread with ReadWrite { import Database._ /******************************************************************************* * Run the transaction: begin, reads/writes, commit. */ override def run () { var value = 0.0 begin () for (op <- s) { if (op._1 == r) { ltab.rl (tid, op._3); value = read (op._3) } else { ltab.wl (tid, op._3); write (op._3, value + 1.0) } // if } // for for (op <- s) ltab.ul (op._2, op._3) commit () } // run /******************************************************************************* * Begin this transaction. */ def begin () { Thread.sleep (5) println ("begin transaction " + tid) } // begin /******************************************************************************* * Read data object oid. * @param oid the database object */ def read (oid: Int): Double = { Thread.sleep (10) val value = data(oid) println ("read " + tid + " ( " + oid + " ) value = " + value) value } // read /******************************************************************************* * Write data object oid. * @param oid the database object */ def write (oid: Int, value: Double) { Thread.sleep (15) println ("write " + tid + " ( " + oid + " ) value = " + value) data(oid) = value } // write /******************************************************************************* * Commit this transaction. */ def commit () { Thread.sleep (20) println ("commit transaction " + tid) } // commit } // Transaction class /*********************************************************************************** * Test the Transaction class by running several concurrent transactions/threads. */ object Transaction extends App with ReadWrite { println ("Test Transactions") val t1 = new Transaction (1, new Schedule (List ( (w, 1, 0), (w, 1, 1) ))) val t2 = new Transaction (2, new Schedule (List ( (w, 2, 0), (w, 2, 1) ))) t1.start () t2.start () } // Transaction object