Main Page | Packages | Class Hierarchy | Class List | Directories | File List | Class Members

Transaction.java

00001 /*
00002  * OpenMobileIS - a free Java(TM) Framework for mobile applications Java(TM)
00003  * Copyright (C) 2004-2005 Philippe Delrieu
00004  * All rights reserved.
00005  * Contact: openmobileis@e-care.fr
00006  *
00007  * This library is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU Lesser General Public
00009  * License as published by the Free Software Foundation; either
00010  * version 2.1 of the License, or any later version.
00011  *
00012  * This library is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015  * Lesser General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU Lesser General Public
00018  * License along with this library; if not, write to the Free Software
00019  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
00020  * USA
00021  *
00022  *  Author : Philippe Delrieu
00023  *  
00024  *  Modifications :
00025  *  2004 Creation P.Delrieu
00026  *  2004 Modified by Romain Beaugrand
00027  * 
00028  */
00029 package org.openmobileis.database.fastobjectdb.db.transaction;
00030 
00031 import java.io.IOException;
00032 
00033 import org.openmobileis.common.util.collection.Array;
00034 import org.openmobileis.database.fastobjectdb.db.exception.FODBException;
00035 
00042 public final class Transaction {
00043         private static final int NONE=0; 
00044         private static final int BEGIN=1;
00045         private static final int COMMIT=2;
00046         private static final int ROLLBACK=3;
00047         private Array fileList;
00048         private int transacCounter;
00049         private int state = Transaction.NONE;
00053   public Transaction() {
00054     super();
00055                 fileList = new Array(10);
00056                 transacCounter = 0;
00057   }
00058   
00059   public void addTransactionFile(TransactionFile file)  throws FODBException {
00060         if (!fileList.contains(file))   { //add if not already added
00061                 if (transacCounter > 0) { //already begin.
00062                         try     {
00063                                 file.open();
00064                         } catch (IOException ex)        {
00065                                 this.rollback();
00066                                 throw new FODBException(ex);
00067                         }
00068                 }
00069                 fileList.add(file);             
00070         }
00071   }
00072   
00073         public void begin() throws FODBException {
00074                 if (transacCounter == 0) {
00075                         int i=0; 
00076                         try     {
00077                                 for (i=0; i<fileList.size(); i++)       {
00078                                         TransactionFile file = (TransactionFile) fileList.get(i);
00079                                         file.open();
00080                                 }
00081                         } catch (Throwable ex)  {
00082                                 transacCounter = 0;
00083                                 this.rollback();
00084                                 throw new FODBException("ERROR during transaction begin", ex);
00085                         }
00086                 }
00087                 transacCounter++;
00088                 state = Transaction.BEGIN;
00089         }
00090   
00091         public boolean commit() throws FODBException    {
00092                 boolean ret = false;
00093                 transacCounter--;
00094                 if (transacCounter <= 0) {
00095                         state = Transaction.COMMIT;
00096                         try     {
00097                                 while (!fileList.isEmpty())     {
00098                                         TransactionFile file = (TransactionFile) fileList.get(0);
00099                                         file.close();
00100                                         fileList.remove(0);
00101                                 }
00102                                 ret = true;
00103                         } catch (Throwable ex)  {
00104                                 transacCounter = 0;
00105                                 ret = this.rollback();
00106                                 throw new FODBException("ERROR during transaction commit", ex);
00107                         }
00108                         transacCounter = 0;
00109                 }
00110                 return ret;
00111         }
00112   
00113         public boolean rollback() throws FODBException  {
00114                 boolean ret = false;
00115                 state = Transaction.ROLLBACK;
00116                 transacCounter--;
00117                 if (transacCounter <= 0) {
00118                         try     {
00119                                 while (!fileList.isEmpty())     {
00120                                         TransactionFile file = (TransactionFile) fileList.get(0);
00121                                         file.close();
00122                                         fileList.remove(0);
00123                                 }
00124                                 ret = true;
00125                         } catch (Throwable ex)  {
00126                                 transacCounter = 0;
00127                                 throw new FODBException("ERROR during transaction rollback", ex);
00128                         }
00129                         transacCounter = 0;
00130                 }
00131                 return ret;
00132         }
00133 
00134 }

Generated on Thu Oct 6 10:06:34 2005 for OpenMobileIS by  doxygen 1.4.3