KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > myoodb > MyOodbTransaction


1 ///////////////////////////////////////////////////////////////////////////////
2
//
3
// Copyright (C) 2003-@year@ by Thomas M. Hazel, MyOODB (www.myoodb.org)
4
//
5
// All Rights Reserved
6
//
7
// This program is free software; you can redistribute it and/or modify
8
// it under the terms of the GNU General Public License and GNU Library
9
// General Public License as published by the Free Software Foundation;
10
// either version 2, or (at your option) any later version.
11
//
12
// This program is distributed in the hope that it will be useful,
13
// but WITHOUT ANY WARRANTY; without even the implied warranty of
14
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
// GNU General Public License and GNU Library General Public License
16
// for more details.
17
//
18
// You should have received a copy of the GNU General Public License
19
// and GNU Library General Public License along with this program; if
20
// not, write to the Free Software Foundation, 675 Mass Ave, Cambridge,
21
// MA 02139, USA.
22
//
23
///////////////////////////////////////////////////////////////////////////////
24
package org.myoodb;
25
26 public final class MyOodbTransaction
27 {
28     private MyOodbDatabase m_database;
29     private java.util.LinkedList JavaDoc m_transactions;
30
31     private long m_transactionIdentifier;
32     private java.lang.Thread JavaDoc m_transactionThread;
33     private org.myoodb.core.AbstractConnection m_databaseConnection;
34     
35     public MyOodbTransaction(MyOodbDatabase database)
36     {
37         m_database = database;
38         m_transactions = new java.util.LinkedList JavaDoc();
39
40         m_transactionIdentifier = -1;
41         m_transactionThread = null;
42         m_databaseConnection = null;
43     }
44
45     protected void setTransactionThread(java.lang.Thread JavaDoc thread)
46     {
47         m_transactionThread = thread;
48     }
49
50     protected void setTransactionIdentifier(long transactionId)
51     {
52         m_transactionIdentifier = transactionId;
53     }
54
55     protected void setDatabaseConnection(org.myoodb.core.AbstractConnection databaseConnection)
56     {
57         m_databaseConnection = databaseConnection;
58     }
59
60     protected org.myoodb.core.AbstractConnection getDatabaseConnection()
61     {
62         return m_databaseConnection;
63     }
64
65     public MyOodbDatabase getDatabase()
66     {
67         return m_database;
68     }
69
70     public long getTransactionIdentifier()
71     {
72         long transactionId = -1;
73
74         if (m_transactions.size() == 0)
75         {
76             throw new org.myoodb.exception.TransactionException("Transaction has no id");
77         }
78         else if (m_transactions.size() == 1)
79         {
80             transactionId = m_transactionIdentifier;
81         }
82         else
83         {
84             MyOodbTransaction subTx = (MyOodbTransaction) m_transactions.getLast();
85             transactionId = subTx.getTransactionIdentifier();
86         }
87
88         return transactionId;
89     }
90
91     public java.lang.Thread JavaDoc getTransactionThread()
92     {
93         return m_transactionThread;
94     }
95
96     public java.util.ArrayList JavaDoc getTransactions()
97     {
98         return new java.util.ArrayList JavaDoc(m_transactions);
99     }
100
101     public synchronized void begin() throws Exception JavaDoc
102     {
103         if (m_transactions.size() == 0)
104         {
105             m_transactions.add(this);
106             m_database.beginTransaction(this);
107         }
108         else
109         {
110             MyOodbTransaction subTx = m_database.createTransaction();
111             m_transactions.add(subTx);
112             subTx.begin();
113         }
114     }
115
116     public synchronized void join() throws Exception JavaDoc
117     {
118         if (m_transactions.size() == 0)
119         {
120             throw new org.myoodb.exception.TransactionException("Transaction has nothing to join");
121         }
122         else if (m_transactions.size() == 1)
123         {
124             m_database.leaveTransaction(this);
125             m_database.joinTransaction(this);
126         }
127         else
128         {
129             MyOodbTransaction subTx = (MyOodbTransaction) m_transactions.getLast();
130             subTx.join();
131         }
132     }
133
134     public synchronized void transfer(Thread JavaDoc thread) throws Exception JavaDoc
135     {
136         if (m_transactions.size() == 0)
137         {
138             throw new org.myoodb.exception.TransactionException("Transaction has nothing to transfer");
139         }
140         else if (m_transactions.size() == 1)
141         {
142             m_database.transferTransaction(this, thread);
143         }
144         else
145         {
146             MyOodbTransaction subTx = (MyOodbTransaction) m_transactions.getLast();
147             subTx.transfer(thread);
148         }
149     }
150
151     public synchronized void leave() throws Exception JavaDoc
152     {
153         if (m_transactions.size() == 0)
154         {
155             throw new org.myoodb.exception.TransactionException("Transaction has nothing to leave");
156         }
157         else if (m_transactions.size() == 1)
158         {
159             m_database.leaveTransaction(this);
160         }
161         else
162         {
163             MyOodbTransaction subTx = (MyOodbTransaction) m_transactions.getLast();
164             subTx.leave();
165         }
166     }
167
168     public synchronized void commit() throws Exception JavaDoc
169     {
170         if (m_transactions.size() == 0)
171         {
172             throw new org.myoodb.exception.TransactionException("Transaction has nothing to commit");
173         }
174         else if (m_transactions.size() == 1)
175         {
176             m_transactions.removeLast();
177             m_database.commitTransaction(this);
178         }
179         else
180         {
181             MyOodbTransaction subTx = (MyOodbTransaction) m_transactions.removeLast();
182             subTx.commit();
183         }
184     }
185
186     public synchronized void rollback() throws Exception JavaDoc
187     {
188         if (m_transactions.size() == 0)
189         {
190             throw new org.myoodb.exception.TransactionException("Transaction has nothing to rollback");
191         }
192         else if (m_transactions.size() == 1)
193         {
194             m_transactions.removeLast();
195             m_database.rollbackTransaction(this);
196         }
197         else
198         {
199             MyOodbTransaction subTx = (MyOodbTransaction) m_transactions.removeLast();
200             subTx.rollback();
201         }
202     }
203
204     public synchronized int getStatus() throws Exception JavaDoc
205     {
206         if (m_transactions.size() == 0)
207         {
208             throw new org.myoodb.exception.TransactionException("Transaction has no status");
209         }
210
211         return m_database.getStatusTransaction((MyOodbTransaction) m_transactions.getLast());
212     }
213 }
214
Popular Tags