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 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)) {
00084 if (transacCounter > 0) {
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 }