Coverage Report - org.apache.ojb.otm.core.Transaction
 
Classes in this File Line Coverage Branch Coverage Complexity
Transaction
N/A
N/A
3.1
 
 1  
 package org.apache.ojb.otm.core;
 2  
 
 3  
 /* Copyright 2003-2005 The Apache Software Foundation
 4  
  *
 5  
  * Licensed under the Apache License, Version 2.0 (the "License");
 6  
  * you may not use this file except in compliance with the License.
 7  
  * You may obtain a copy of the License at
 8  
  *
 9  
  *     http://www.apache.org/licenses/LICENSE-2.0
 10  
  *
 11  
  * Unless required by applicable law or agreed to in writing, software
 12  
  * distributed under the License is distributed on an "AS IS" BASIS,
 13  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 14  
  * See the License for the specific language governing permissions and
 15  
  * limitations under the License.
 16  
  */
 17  
 
 18  
 import java.util.ArrayList;
 19  
 import java.util.Iterator;
 20  
 import java.util.List;
 21  
 
 22  
 import org.apache.ojb.otm.OTMKit;
 23  
 import org.apache.ojb.otm.OTMConnection;
 24  
 
 25  
 /**
 26  
  *  Transaction delivers the core function of OTMKit - to manage objects within the
 27  
  *  context of a transaction.
 28  
  *
 29  
  *  @author <a href="mailto:rraghuram@hotmail.com">Raghu Rajah</a>
 30  
  */
 31  
 public class Transaction
 32  
 {
 33  
     private boolean _isInProgress;
 34  
     private List _listeners;
 35  
     private List _connections;
 36  
     private OTMKit _kit;
 37  
 
 38  
     public Transaction()
 39  
     {
 40  
         _listeners = new ArrayList();
 41  
         _connections = new ArrayList();
 42  
     }
 43  
 
 44  
     public OTMKit getKit()
 45  
     {
 46  
         return _kit;
 47  
     }
 48  
 
 49  
     public void setKit(OTMKit kit)
 50  
     {
 51  
         _kit = kit;
 52  
     }
 53  
 
 54  
     public void begin()
 55  
             throws TransactionException
 56  
     {
 57  
         if (_isInProgress)
 58  
         {
 59  
             throw new TransactionInProgressException(
 60  
                 "Transaction already in progress, cannot restart");
 61  
         }
 62  
 
 63  
         _isInProgress = true;
 64  
 
 65  
         for (Iterator iterator = _connections.iterator(); iterator.hasNext();)
 66  
         {
 67  
             BaseConnection connection = (BaseConnection) iterator.next();
 68  
             connection.transactionBegin();
 69  
         }
 70  
 
 71  
         for (Iterator iterator = _listeners.iterator(); iterator.hasNext();)
 72  
         {
 73  
             TransactionListener listener = (TransactionListener) iterator.next();
 74  
             listener.transactionBegan(this);
 75  
         }
 76  
     }
 77  
 
 78  
     /**
 79  
      *
 80  
      *  Commit this transaction. A commit notifies all listeners of this transaction. It then
 81  
      *  initiates a two phase commit on connections. Since, connections cannot be associated to
 82  
      *  more than one transaction at any given point in time, there is no neccessity to identify
 83  
      *  transaction.
 84  
      *
 85  
      */
 86  
     public void commit()
 87  
             throws TransactionException
 88  
     {
 89  
         if (!_isInProgress)
 90  
         {
 91  
             throw new TransactionNotInProgressException(
 92  
                 "Transaction not in progress, nothing to commit");
 93  
         }
 94  
 
 95  
         for (Iterator iterator = _listeners.iterator(); iterator.hasNext();)
 96  
         {
 97  
             TransactionListener listener = (TransactionListener) iterator.next();
 98  
 
 99  
             listener.transactionCommitting(this);
 100  
         }
 101  
 
 102  
         for (Iterator iterator = _connections.iterator(); iterator.hasNext();)
 103  
         {
 104  
             BaseConnection connection = (BaseConnection) iterator.next();
 105  
             ConcreteEditingContext context =
 106  
                     (ConcreteEditingContext) connection.getEditingContext();
 107  
 
 108  
             context.commit();
 109  
             connection.transactionPrepare();
 110  
         }
 111  
 
 112  
         for (Iterator iterator = _connections.iterator(); iterator.hasNext();)
 113  
         {
 114  
             BaseConnection connection = (BaseConnection) iterator.next();
 115  
 
 116  
             connection.transactionCommit();
 117  
             connection.setTransaction(null);
 118  
         }
 119  
 
 120  
         _connections.clear();
 121  
         _isInProgress = false;
 122  
     }
 123  
 
 124  
     /**
 125  
      *
 126  
      *  Checkpoint this transaction.
 127  
      *
 128  
      */
 129  
     public void checkpoint()
 130  
             throws TransactionException
 131  
     {
 132  
         if (!_isInProgress)
 133  
         {
 134  
             throw new TransactionNotInProgressException(
 135  
                 "Transaction not in progress, cannot checkpoint");
 136  
         }
 137  
 
 138  
         for (Iterator iterator = _connections.iterator(); iterator.hasNext();)
 139  
         {
 140  
             BaseConnection connection = (BaseConnection) iterator.next();
 141  
             ConcreteEditingContext context =
 142  
                     (ConcreteEditingContext) connection.getEditingContext();
 143  
 
 144  
             context.checkpoint();
 145  
         }
 146  
     }
 147  
 
 148  
     /**
 149  
      *
 150  
      * Rollback this transaction. A rollback on the transaction, notifies all its listeners. It,
 151  
      * then initiates a rollback on all associated connections.
 152  
      *
 153  
      */
 154  
     public void rollback()
 155  
             throws TransactionException
 156  
     {
 157  
         if (!_isInProgress)
 158  
         {
 159  
             throw new TransactionNotInProgressException(
 160  
                 "Transaction not in progress, nothing to commit");
 161  
         }
 162  
 
 163  
         for (Iterator iterator = _listeners.iterator(); iterator.hasNext();)
 164  
         {
 165  
             TransactionListener listener = (TransactionListener) iterator.next();
 166  
             listener.transactionRollingBack(this);
 167  
         }
 168  
 
 169  
         for (Iterator iterator = _connections.iterator(); iterator.hasNext();)
 170  
         {
 171  
             BaseConnection connection = (BaseConnection) iterator.next();
 172  
             ConcreteEditingContext context =
 173  
                     (ConcreteEditingContext) connection.getEditingContext();
 174  
 
 175  
             context.rollback();
 176  
             connection.transactionRollback();
 177  
             connection.setTransaction(null);
 178  
         }
 179  
 
 180  
         _connections.clear();
 181  
         _isInProgress = false;
 182  
     }
 183  
 
 184  
     public boolean isInProgress()
 185  
     {
 186  
         return _isInProgress;
 187  
     }
 188  
     
 189  
     /**
 190  
      *
 191  
      *  Associate a connection to this transaction. A OTMConnection can be registered to atmost one
 192  
      *  transaction, while a transaction can manage multiple connections.
 193  
      *
 194  
      *  @param connection       the connection to register
 195  
      *
 196  
      */
 197  
     public void registerConnection(OTMConnection connection)
 198  
     {
 199  
         Transaction connectionTx = connection.getTransaction();
 200  
         if ((connectionTx != null) && (connectionTx != this))
 201  
         {
 202  
             throw new TransactionException(
 203  
                 "Attempt to re-assign a different transaction to a open connection");
 204  
         }
 205  
 
 206  
         if (!_connections.contains(connection))
 207  
         {
 208  
             _connections.add(connection);
 209  
 
 210  
             connection.setTransaction(this);
 211  
 
 212  
         }
 213  
     }
 214  
 
 215  
     /**
 216  
      *
 217  
      *  Adds a listener to this transaction. Listeners get boundary notifications of this
 218  
      *  transaction.
 219  
      *
 220  
      *  @param listener         the listener of this transaction
 221  
      *
 222  
      */
 223  
     public void registerListener(TransactionListener listener)
 224  
     {
 225  
         if (_listeners.indexOf(listener) < 0)
 226  
         {
 227  
             _listeners.add(listener);
 228  
         }
 229  
     }
 230  
 }