00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
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)) {
00061 if (transacCounter > 0) {
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 }