Coverage Report - org.melati.poem.transaction.Transaction
 
Classes in this File Line Coverage Branch Coverage Complexity
Transaction
64%
54/84
59%
13/22
2.286
 
 1  
 /*
 2  
  * $Source$
 3  
  * $Revision$
 4  
  *
 5  
  * Copyright (C) 2000 William Chesters
 6  
  *
 7  
  * Part of Melati (http://melati.org), a framework for the rapid
 8  
  * development of clean, maintainable web applications.
 9  
  *
 10  
  * Melati is free software; Permission is granted to copy, distribute
 11  
  * and/or modify this software under the terms either:
 12  
  *
 13  
  * a) the GNU General Public License as published by the Free Software
 14  
  *    Foundation; either version 2 of the License, or (at your option)
 15  
  *    any later version,
 16  
  *
 17  
  *    or
 18  
  *
 19  
  * b) any version of the Melati Software License, as published
 20  
  *    at http://melati.org
 21  
  *
 22  
  * You should have received a copy of the GNU General Public License and
 23  
  * the Melati Software License along with this program;
 24  
  * if not, write to the Free Software Foundation, Inc.,
 25  
  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA to obtain the
 26  
  * GNU General Public License and visit http://melati.org to obtain the
 27  
  * Melati Software License.
 28  
  *
 29  
  * Feel free to contact the Developers of Melati (http://melati.org),
 30  
  * if you would like to work out a different arrangement than the options
 31  
  * outlined here.  It is our intention to allow Melati to be used by as
 32  
  * wide an audience as possible.
 33  
  *
 34  
  * This program is distributed in the hope that it will be useful,
 35  
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
 36  
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 37  
  * GNU General Public License for more details.
 38  
  *
 39  
  * Contact details for copyright holder:
 40  
  *
 41  
  *     William Chesters <williamc At paneris.org>
 42  
  *     http://paneris.org/~williamc
 43  
  *     Obrechtstraat 114, 2517VX Den Haag, The Netherlands
 44  
  */
 45  
 
 46  
 package org.melati.poem.transaction;
 47  
 
 48  
 import java.util.Vector;
 49  
 
 50  
 import org.melati.poem.UnexpectedExceptionPoemException;
 51  
 
 52  
 /**
 53  
  * A Transaction.
 54  
  */
 55  
 public abstract class Transaction {
 56  
 
 57  
   /** Index of the Transaction. */
 58  
   public final int index;
 59  
   /** Mask. */
 60  
   public final int mask;
 61  
   /** Negative mask. */
 62  
   public final int negMask;
 63  
 
 64  
   /** The transaction we are waiting on. */
 65  357
   private Transaction blockedOn = null;
 66  
 
 67  
   /** The transactions that are directly waiting on us. */
 68  357
   private Vector<Transaction> blockees = new Vector<Transaction>();
 69  
 
 70  
   /** The transitive closure of the transactions we are waiting on. */
 71  
   private int blockedOnMask;
 72  
 
 73  357
   private int seenCapacityMin = 50;
 74  357
   private int seenCapacityMax = 1000;
 75  357
   private Vector<Transactioned> seen = new Vector<Transactioned>(seenCapacityMin);
 76  
 
 77  357
   private int touchedCapacityMin = 50;
 78  357
   private int touchedCapacityMax = 1000;
 79  357
   private Vector<Transactioned> touched = new Vector<Transactioned>();
 80  
 
 81  
   private TransactionPool transactionPool;
 82  
 
 83  
   /**
 84  
    * Constructor.
 85  
    * 
 86  
    * @param transactionPoolP the pool this transaction belongs to 
 87  
    * @param indexP the key for this Transaction 
 88  
    */
 89  357
   public Transaction(TransactionPool transactionPoolP, int indexP) {
 90  357
     this.transactionPool = transactionPoolP;
 91  357
     if (indexP > transactionPool.transactionsMax())
 92  0
       throw new TransactionIndexTooLargeException();
 93  
 
 94  357
     this.index = indexP;
 95  357
     mask = 1 << index;
 96  357
     negMask = ~mask;
 97  357
   }
 98  
 
 99  
   protected abstract void backingCommit();
 100  
   protected abstract void backingRollback();
 101  
 
 102  
   /**
 103  
    * The thread calling block will have to wait
 104  
    * until (another thread) calls finish and calls notifyAll.
 105  
    */
 106  
   synchronized void block(Transaction blockee) {
 107  0
     blockees.addElement(blockee);
 108  0
     blockee.blockedOn = this;
 109  0
     blockee.propagateBlockage();
 110  
     try {
 111  0
       wait();
 112  
     }
 113  0
     catch (InterruptedException e) {
 114  0
       throw new UnexpectedExceptionPoemException(e);
 115  
     }
 116  
     finally {
 117  0
       blockees.removeElement(blockee);
 118  0
       blockee.blockedOn = null;
 119  0
       blockee.propagateBlockage();
 120  0
     }
 121  0
   }
 122  
 
 123  
   private synchronized void propagateBlockage() {
 124  0
     if (blockedOn == null)
 125  0
       blockedOnMask = mask;  // we are only waiting on ourself
 126  
     else {
 127  0
       if ((blockedOn.blockedOnMask & mask) != 0)
 128  0
         throw new WouldDeadlockException();
 129  0
       blockedOnMask = blockedOn.blockedOnMask | mask;
 130  
     }
 131  
 
 132  0
     for (int i = blockees.size() - 1; i >= 0; --i)
 133  0
       ((Transaction)blockees.elementAt(i)).propagateBlockage();
 134  0
   }
 135  
 
 136  
   final void notifyTouched(Transactioned persistent) {
 137  1215
     touched.addElement(persistent);
 138  1215
   }
 139  
 
 140  
   final void notifySeen(Transactioned persistent) {
 141  12909
     seen.addElement(persistent);
 142  12909
   }
 143  
 
 144  
   /**
 145  
    * Make persistent ie no longer able to be rolled back.
 146  
    */
 147  
   public void writeDown() {
 148  20382
     synchronized (touched) {
 149  20382
       for (Transactioned persistent : touched) 
 150  9769
         persistent.writeDown(this);
 151  20382
     }
 152  20382
   }
 153  
 
 154  
   private void unSee() {
 155  4811
     synchronized (seen) {
 156  4811
       for (Transactioned persistent : seen)
 157  12909
         persistent.unSee(this);
 158  
 
 159  4811
       if (seen.size() > seenCapacityMax)
 160  0
         seen = new Vector<Transactioned>(seenCapacityMin);
 161  
       else
 162  4811
         seen.setSize(0);
 163  4811
     }
 164  4811
   }
 165  
 
 166  
   // This doesn't have to be synchronized.
 167  
   private void finish(boolean commit) {
 168  
     try {
 169  4811
       if (commit) {
 170  4798
         writeDown();
 171  4798
         backingCommit();
 172  
       }
 173  
       else
 174  13
         backingRollback();
 175  
 
 176  4811
       for (Transactioned persistent : touched) { 
 177  1215
           if (commit)
 178  1201
               persistent.commit(this);
 179  
             else
 180  14
               persistent.rollback(this);
 181  1215
       }
 182  
     }
 183  
     finally {
 184  4811
       if (touched.size() > touchedCapacityMax)
 185  0
         touched = new Vector<Transactioned>(touchedCapacityMin);
 186  
       else
 187  4811
         touched.setSize(0);
 188  
 
 189  4811
       unSee();
 190  
 
 191  
       // notifyAll will wake too many threads if some of them are writers, but
 192  
       // this is really the best we can do without using heavy Lock-ish objects
 193  
 
 194  4811
       synchronized (this) {
 195  4811
         notifyAll();
 196  4811
       }
 197  4811
     }
 198  4811
   }
 199  
 
 200  
   /**
 201  
    * Finish up, for example write to database.
 202  
    */
 203  
   public void commit() {
 204  
     try {
 205  4798
       finish(true);
 206  
     }
 207  0
     catch (RuntimeException e) {
 208  
       try {
 209  0
         System.err.println("Rolling back due to " + e);
 210  0
         finish(false);
 211  
       }
 212  0
       catch (Exception ignore) {
 213  
         // Ignore
 214  0
         ignore = null; // shut PMD up
 215  0
       }
 216  0
       throw e;
 217  4798
     }
 218  4798
   }
 219  
 
 220  
   /**
 221  
    * Finish without commit.
 222  
    */
 223  
   public void rollback() {
 224  13
     finish(false);
 225  13
   }
 226  
 
 227  
   /**
 228  
    * @return the Transaction we are waiting for
 229  
    */
 230  
   public Transaction getBlockedOn() {
 231  0
     return blockedOn;
 232  
   }
 233  
 
 234  
   /**
 235  
    * The transaction index.
 236  
    * {@inheritDoc}
 237  
    * @see java.lang.Object#toString()
 238  
    */
 239  
   public String toString() {
 240  2
     return "transaction" + index;
 241  
   }
 242  
 }
 243  
 
 244  
 
 245