1 cananian 1.1.2.1 // CommitRecord.java, created Fri Oct 27 16:59:59 2000 by cananian 2 cananian 1.1.2.1 // Copyright (C) 2000 C. Scott Ananian <cananian@alumni.princeton.edu> 3 cananian 1.1.2.1 // Licensed under the terms of the GNU GPL; see COPYING for details. 4 cananian 1.1.2.1 package harpoon.Runtime.Transactions; 5 cananian 1.1.2.1 6 cananian 1.1.2.1 /** 7 cananian 1.1.2.2 * A <code>CommitRecord</code> keeps track of the status of a transaction. 8 cananian 1.1.2.2 * It is initialized in the <code>WAITING</code> state and can be updated 9 cananian 1.1.2.2 * exactly once to either COMMITTED or ABORTED. Transactions can be nested; 10 cananian 1.1.2.2 * therefore <code>CommitRecord</code>s can be dependent on other commit 11 cananian 1.1.2.2 * records. 12 cananian 1.1.2.1 * 13 cananian 1.1.2.1 * @author C. Scott Ananian <cananian@alumni.princeton.edu> 14 cananian 1.3 * @version $Id: CommitRecord.java,v 1.3 2003/07/21 21:21:58 cananian Exp $ 15 cananian 1.1.2.1 */ 16 cananian 1.1.2.1 public class CommitRecord { 17 cananian 1.1.2.2 // enumerated constants 18 cananian 1.1.2.4 public final static int WAITING=0; 19 cananian 1.1.2.4 public final static int COMMITTED=1; 20 cananian 1.1.2.4 public final static int ABORTED=2; 21 cananian 1.1.2.2 // fields. 22 cananian 1.1.2.2 /** The commit record that this is dependent on, if any. */ 23 cananian 1.1.2.2 public final CommitRecord parent; 24 cananian 1.1.2.2 /** The 'state' of this record: initialized to WAITING, and 25 cananian 1.1.2.2 * write-once to either COMMITTED or ABORTED. */ 26 cananian 1.1.2.4 private int state = WAITING; 27 cananian 1.1.2.3 28 cananian 1.1.2.3 /** Constructor method. */ 29 cananian 1.1.2.3 public static CommitRecord newTransaction(CommitRecord parent) { 30 cananian 1.1.2.3 return new CommitRecord(parent, 0); 31 cananian 1.1.2.3 } 32 cananian 1.1.2.3 /** Returns a new commit record suitable for retrying this transaction. 33 cananian 1.1.2.3 * @exception TransactionAbortException if this transaction has been 34 cananian 1.1.2.3 * retried too many times. 35 cananian 1.1.2.3 */ 36 cananian 1.1.2.3 public final CommitRecord retryTransaction() 37 cananian 1.1.2.3 throws TransactionAbortException { 38 cananian 1.3 if (retry_count > 2 && parent!=null) 39 cananian 1.3 throw new TransactionAbortException(parent); 40 cananian 1.1.2.3 try { 41 cananian 1.1.2.3 Thread.sleep(retry_count); // sleep for a few milliseconds. 42 cananian 1.1.2.3 } catch (InterruptedException ex) { /* wake up */ } 43 cananian 1.1.2.3 return new CommitRecord(this.parent, retry_count+1); 44 cananian 1.1.2.3 } 45 cananian 1.1.2.3 public final void commitTransaction() throws TransactionAbortException { 46 cananian 1.1.2.3 if (this.commit()!=COMMITTED) 47 cananian 1.1.2.3 throw new TransactionAbortException(this); 48 cananian 1.1.2.3 } 49 cananian 1.1.2.3 50 cananian 1.1.2.3 /** Private constructor. */ 51 cananian 1.1.2.3 private CommitRecord(CommitRecord parent, int retry_count) { 52 cananian 1.1.2.2 this.parent = parent; 53 cananian 1.1.2.3 this.retry_count = retry_count; 54 cananian 1.1.2.2 } 55 cananian 1.1.2.3 /** Keep track of how many times this transaction's been retried. */ 56 cananian 1.1.2.3 private final int retry_count; 57 cananian 1.1.2.2 58 cananian 1.1.2.2 /** Returns the 'state' of this record (including dependencies) */ 59 cananian 1.1.2.4 public int state() { return state(this); } 60 cananian 1.1.2.4 61 cananian 1.1.2.2 /** 'StateP' procedure from write-up. */ 62 cananian 1.1.2.4 public static native int state(CommitRecord c); 63 cananian 1.1.2.4 64 cananian 1.1.2.2 /** Abort this (sub)transaction, if possible. 65 cananian 1.1.2.2 * @return the final state of the (sub)transaction. 66 cananian 1.1.2.2 */ 67 cananian 1.1.2.4 public int abort() { return abort(this); } 68 cananian 1.1.2.2 /** Commit this (sub)transaction, if possible. 69 cananian 1.1.2.2 * @return the final state of the (sub)transaction. 70 cananian 1.1.2.2 */ 71 cananian 1.1.2.4 public int commit() { return commit(this); } 72 cananian 1.1.2.4 73 cananian 1.1.2.2 /** Abort a transaction, if possible. 74 cananian 1.1.2.2 * @return the final state of the (sub)transaction. 75 cananian 1.1.2.2 */ 76 cananian 1.1.2.4 public static native int abort(CommitRecord c); 77 cananian 1.1.2.4 78 cananian 1.1.2.2 /** Commit a transaction, if possible. 79 cananian 1.1.2.2 * @return the final state of the (sub)transaction. 80 cananian 1.1.2.2 */ 81 cananian 1.1.2.4 public static native int commit(CommitRecord c); 82 cananian 1.2 }