001: ///////////////////////////////////////////////////////////////////////////////
002: //
003: // Copyright (C) 2003-@year@ by Thomas M. Hazel, MyOODB (www.myoodb.org)
004: //
005: // All Rights Reserved
006: //
007: // This program is free software; you can redistribute it and/or modify
008: // it under the terms of the GNU General Public License and GNU Library
009: // General Public License as published by the Free Software Foundation;
010: // either version 2, or (at your option) any later version.
011: //
012: // This program is distributed in the hope that it will be useful,
013: // but WITHOUT ANY WARRANTY; without even the implied warranty of
014: // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
015: // GNU General Public License and GNU Library General Public License
016: // for more details.
017: //
018: // You should have received a copy of the GNU General Public License
019: // and GNU Library General Public License along with this program; if
020: // not, write to the Free Software Foundation, 675 Mass Ave, Cambridge,
021: // MA 02139, USA.
022: //
023: ///////////////////////////////////////////////////////////////////////////////
024: package org.myoodb;
025:
026: public final class MyOodbTransaction {
027: private MyOodbDatabase m_database;
028: private java.util.LinkedList m_transactions;
029:
030: private long m_transactionIdentifier;
031: private java.lang.Thread m_transactionThread;
032: private org.myoodb.core.AbstractConnection m_databaseConnection;
033:
034: public MyOodbTransaction(MyOodbDatabase database) {
035: m_database = database;
036: m_transactions = new java.util.LinkedList();
037:
038: m_transactionIdentifier = -1;
039: m_transactionThread = null;
040: m_databaseConnection = null;
041: }
042:
043: protected void setTransactionThread(java.lang.Thread thread) {
044: m_transactionThread = thread;
045: }
046:
047: protected void setTransactionIdentifier(long transactionId) {
048: m_transactionIdentifier = transactionId;
049: }
050:
051: protected void setDatabaseConnection(
052: org.myoodb.core.AbstractConnection databaseConnection) {
053: m_databaseConnection = databaseConnection;
054: }
055:
056: protected org.myoodb.core.AbstractConnection getDatabaseConnection() {
057: return m_databaseConnection;
058: }
059:
060: public MyOodbDatabase getDatabase() {
061: return m_database;
062: }
063:
064: public long getTransactionIdentifier() {
065: long transactionId = -1;
066:
067: if (m_transactions.size() == 0) {
068: throw new org.myoodb.exception.TransactionException(
069: "Transaction has no id");
070: } else if (m_transactions.size() == 1) {
071: transactionId = m_transactionIdentifier;
072: } else {
073: MyOodbTransaction subTx = (MyOodbTransaction) m_transactions
074: .getLast();
075: transactionId = subTx.getTransactionIdentifier();
076: }
077:
078: return transactionId;
079: }
080:
081: public java.lang.Thread getTransactionThread() {
082: return m_transactionThread;
083: }
084:
085: public java.util.ArrayList getTransactions() {
086: return new java.util.ArrayList(m_transactions);
087: }
088:
089: public synchronized void begin() throws Exception {
090: if (m_transactions.size() == 0) {
091: m_transactions.add(this );
092: m_database.beginTransaction(this );
093: } else {
094: MyOodbTransaction subTx = m_database.createTransaction();
095: m_transactions.add(subTx);
096: subTx.begin();
097: }
098: }
099:
100: public synchronized void join() throws Exception {
101: if (m_transactions.size() == 0) {
102: throw new org.myoodb.exception.TransactionException(
103: "Transaction has nothing to join");
104: } else if (m_transactions.size() == 1) {
105: m_database.leaveTransaction(this );
106: m_database.joinTransaction(this );
107: } else {
108: MyOodbTransaction subTx = (MyOodbTransaction) m_transactions
109: .getLast();
110: subTx.join();
111: }
112: }
113:
114: public synchronized void transfer(Thread thread) throws Exception {
115: if (m_transactions.size() == 0) {
116: throw new org.myoodb.exception.TransactionException(
117: "Transaction has nothing to transfer");
118: } else if (m_transactions.size() == 1) {
119: m_database.transferTransaction(this , thread);
120: } else {
121: MyOodbTransaction subTx = (MyOodbTransaction) m_transactions
122: .getLast();
123: subTx.transfer(thread);
124: }
125: }
126:
127: public synchronized void leave() throws Exception {
128: if (m_transactions.size() == 0) {
129: throw new org.myoodb.exception.TransactionException(
130: "Transaction has nothing to leave");
131: } else if (m_transactions.size() == 1) {
132: m_transactions.removeLast();
133: m_database.leaveTransaction(this );
134: } else {
135: MyOodbTransaction subTx = (MyOodbTransaction) m_transactions
136: .removeLast();
137: subTx.leave();
138: }
139: }
140:
141: public synchronized void commit() throws Exception {
142: if (m_transactions.size() == 0) {
143: throw new org.myoodb.exception.TransactionException(
144: "Transaction has nothing to commit");
145: } else if (m_transactions.size() == 1) {
146: m_transactions.removeLast();
147: m_database.commitTransaction(this );
148: } else {
149: MyOodbTransaction subTx = (MyOodbTransaction) m_transactions
150: .removeLast();
151: subTx.commit();
152: }
153: }
154:
155: public synchronized void rollback() throws Exception {
156: if (m_transactions.size() == 0) {
157: throw new org.myoodb.exception.TransactionException(
158: "Transaction has nothing to rollback");
159: } else if (m_transactions.size() == 1) {
160: m_transactions.removeLast();
161: m_database.rollbackTransaction(this );
162: } else {
163: MyOodbTransaction subTx = (MyOodbTransaction) m_transactions
164: .removeLast();
165: subTx.rollback();
166: }
167: }
168:
169: public synchronized int getStatus() throws Exception {
170: if (m_transactions.size() == 0) {
171: throw new org.myoodb.exception.TransactionException(
172: "Transaction has no status");
173: }
174:
175: return m_database
176: .getStatusTransaction((MyOodbTransaction) m_transactions
177: .getLast());
178: }
179: }
|