Transaction.java

00001 /*
00002  * OpenMobileIS - a free Java(TM) Framework for mobile applications Java(TM)
00003  * Copyright (C) 2004-2006 Philippe Delrieu
00004  * All rights reserved.
00005  * Contact: pdelrieu@openmobileis.org
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 import java.util.Stack;
00033 
00034 import org.openmobileis.common.util.collection.Array;
00035 import org.openmobileis.database.fastobjectdb.db.exception.FODBException;
00036 
00043 public final class Transaction {
00044         private static final int NONE=0; 
00045         private static final int BEGIN=1;
00046         private static final int COMMIT=2;
00047         private static final int ROLLBACK=3;
00048         private Array fileList;
00049         private int transacCounter;
00050         private int state = Transaction.NONE;
00051         private Stack threadStack;
00052         private Thread transactionThread;
00056   public Transaction(Thread th) {
00057     super();
00058                 this.fileList = new Array(10);
00059                 this.transacCounter = 0;
00060                 this.threadStack = new Stack();
00061                 this.transactionThread = th;
00062   }
00063   
00064   public Thread getTransactionThread()  {
00065     return this.transactionThread;
00066   }
00067   
00068   public void pushThread(Thread th)     {
00069     this.threadStack.push(th);
00070   }
00071   
00072   public void popallthread()    {
00073     Thread th = null;
00074     while (!threadStack.empty())        {
00075       th=(Thread)threadStack.pop();
00076       synchronized(th)  {
00077         th.notifyAll();
00078       }
00079     }
00080   }
00081   
00082   public void addTransactionFile(TransactionFile file)  throws FODBException {
00083         if (!fileList.contains(file))   { //add if not already added
00084                 if (transacCounter > 0) { //already begin.
00085                         try     {
00086                                 file.open();
00087                         } catch (IOException ex)        {
00088                                 this.rollback();
00089                                 throw new FODBException(ex);
00090                         }
00091                 }
00092                 fileList.add(file);             
00093         }
00094   }
00095   
00096         public void begin() throws FODBException {
00097                 if (transacCounter == 0) {
00098                         int i=0; 
00099                         try     {
00100                                 for (i=0; i<fileList.size(); i++)       {
00101                                         TransactionFile file = (TransactionFile) fileList.get(i);
00102                                         file.open();
00103                                 }
00104                         } catch (Throwable ex)  {
00105                                 transacCounter = 0;
00106                                 this.rollback();
00107                                 throw new FODBException("ERROR during transaction begin", ex);
00108                         }
00109                 }
00110                 transacCounter++;
00111                 state = Transaction.BEGIN;
00112         }
00113   
00114         public boolean commit() throws FODBException    {
00115                 boolean ret = false;
00116                 transacCounter--;
00117                 if (transacCounter <= 0) {
00118                         state = Transaction.COMMIT;
00119                         try     {
00120                                 while (!fileList.isEmpty())     {
00121                                         TransactionFile file = (TransactionFile) fileList.get(0);
00122                                         file.close();
00123                                         fileList.remove(0);
00124                                 }
00125                                 ret = true;
00126                         } catch (Throwable ex)  {
00127                                 transacCounter = 0;
00128                                 ret = this.rollback();
00129                                 throw new FODBException("ERROR during transaction commit", ex);
00130                         }
00131                         transacCounter = 0;
00132                 }
00133                 return ret;
00134         }
00135   
00136         public boolean rollback() throws FODBException  {
00137                 boolean ret = false;
00138                 state = Transaction.ROLLBACK;
00139                 transacCounter--;
00140                 if (transacCounter <= 0) {
00141                         try     {
00142                                 while (!fileList.isEmpty())     {
00143                                         TransactionFile file = (TransactionFile) fileList.get(0);
00144                                         file.close();
00145                                         fileList.remove(0);
00146                                 }
00147                                 ret = true;
00148                         } catch (Throwable ex)  {
00149                                 transacCounter = 0;
00150                                 throw new FODBException("ERROR during transaction rollback", ex);
00151                         }
00152                         transacCounter = 0;
00153                 }
00154                 return ret;
00155         }
00156 
00157 }

Generated on Mon Dec 4 11:03:31 2006 for OpenMobileIS by  doxygen 1.5.1-p1