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

TransactionManager.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.util.HashMap;
00032 
00033 import org.openmobileis.database.fastobjectdb.db.exception.FODBException;
00034 
00040 public final class TransactionManager {
00041         private HashMap transactionListbythread;
00042         private HashMap transactionFileListbyCollection;
00046   public TransactionManager() {
00047     super();
00048                 transactionListbythread = new HashMap(10);
00049                 transactionFileListbyCollection = new HashMap(20);
00050   }
00051   
00052   private Transaction getTransaction()  {
00053         Thread th = Thread.currentThread();
00054         Transaction ret = (Transaction)transactionListbythread.get(th);
00055         if (ret==null)  {
00056                 ret = new Transaction();
00057                         transactionListbythread.put(th, ret);
00058         }
00059         return ret;
00060   }
00061   
00062   public synchronized void registerTransactionFile(TransactionFile file, String collectionName) {
00063                 transactionFileListbyCollection.put(collectionName, file);
00064   }
00065   
00066         public synchronized void enterTransaction(String collectionName)        throws FODBException {
00067                 TransactionFile file = (TransactionFile)transactionFileListbyCollection.get(collectionName);
00068                 if (file != null)       {
00069                         this.getTransaction().addTransactionFile(file);
00070                 } else  {
00071                         throw new FODBException("ERROR TransactionManager Unregistered Collection :"+collectionName+" try to enter transaction.");
00072                 }
00073         }
00074   
00075   public void begin()   throws FODBException {
00076                 this.getTransaction().begin();
00077   }
00078   
00079   public void commit() throws FODBException     {
00080                 boolean effective = this.getTransaction().commit();
00081                 if (effective)  {
00082                         transactionListbythread.remove(Thread.currentThread());
00083                 }
00084   }
00085   
00086   public void rollback() throws FODBException   {
00087                 boolean effective = this.getTransaction().rollback();
00088                 if (effective)  {
00089                         transactionListbythread.remove(Thread.currentThread());
00090                 }
00091   }
00092 
00093 }

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