KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sleepycat > je > Transaction


1 /*-
2  * See the file LICENSE for redistribution information.
3  *
4  * Copyright (c) 2002,2006 Oracle. All rights reserved.
5  *
6  * $Id: Transaction.java,v 1.48 2006/10/30 21:14:12 bostic Exp $
7  */

8
9 package com.sleepycat.je;
10
11 import com.sleepycat.je.txn.Locker;
12 import com.sleepycat.je.txn.Txn;
13 import com.sleepycat.je.utilint.PropUtil;
14
15 /**
16  * Javadoc for this public class is generated
17  * via the doc templates in the doc_src directory.
18  */

19 public class Transaction {
20
21     private Txn txn;
22     private Environment env;
23     private long id;
24     private String JavaDoc name;
25
26     /**
27      * Creates a transaction.
28      */

29     Transaction(Environment env, Txn txn) {
30         this.env = env;
31         this.txn = txn;
32
33         /*
34          * Copy the id to this wrapper object so the id will be available
35          * after the transaction is closed and the txn field is nulled.
36          */

37         this.id = txn.getId();
38     }
39
40     /**
41      * Javadoc for this public method is generated via
42      * the doc templates in the doc_src directory.
43      */

44     public void abort()
45     throws DatabaseException {
46
47     try {
48         checkEnv();
49         env.removeReferringHandle(this);
50         txn.abort(false); // no sync required
51

52         /* Remove reference to internal txn, so we can reclaim memory. */
53         txn = null;
54     } catch (Error JavaDoc E) {
55         DbInternal.envGetEnvironmentImpl(env).invalidate(E);
56         throw E;
57     }
58     }
59
60     /**
61      * Javadoc for this public method is generated via
62      * the doc templates in the doc_src directory.
63      */

64     public long getId()
65         throws DatabaseException {
66
67         return id;
68     }
69
70     /**
71      * Javadoc for this public method is generated via
72      * the doc templates in the doc_src directory.
73      */

74     public void commit()
75     throws DatabaseException {
76
77     try {
78         checkEnv();
79         env.removeReferringHandle(this);
80         txn.commit();
81         /* Remove reference to internal txn, so we can reclaim memory. */
82         txn = null;
83     } catch (Error JavaDoc E) {
84         DbInternal.envGetEnvironmentImpl(env).invalidate(E);
85         throw E;
86     }
87     }
88
89     /**
90      * Javadoc for this public method is generated via
91      * the doc templates in the doc_src directory.
92      */

93     public void commitSync()
94     throws DatabaseException {
95
96         doCommit(Txn.TXN_SYNC);
97     }
98
99     /**
100      * Javadoc for this public method is generated via
101      * the doc templates in the doc_src directory.
102      */

103     public void commitNoSync()
104     throws DatabaseException {
105
106         doCommit(Txn.TXN_NOSYNC);
107     }
108
109     /**
110      * Javadoc for this public method is generated via
111      * the doc templates in the doc_src directory.
112      */

113     public void commitWriteNoSync()
114     throws DatabaseException {
115
116         doCommit(Txn.TXN_WRITE_NOSYNC);
117     }
118
119     private void doCommit(byte commitType)
120     throws DatabaseException {
121
122     try {
123         checkEnv();
124         env.removeReferringHandle(this);
125         txn.commit(commitType);
126
127         /* Remove reference to internal txn, so we can reclaim memory. */
128         txn = null;
129     } catch (Error JavaDoc E) {
130         DbInternal.envGetEnvironmentImpl(env).invalidate(E);
131         throw E;
132     }
133     }
134
135     /**
136      * Javadoc for this public method is generated via
137      * the doc templates in the doc_src directory.
138      */

139     public void setTxnTimeout(long timeOut)
140         throws DatabaseException {
141
142         checkEnv();
143         txn.setTxnTimeout(PropUtil.microsToMillis(timeOut));
144     }
145
146     /**
147      * Javadoc for this public method is generated via
148      * the doc templates in the doc_src directory.
149      */

150     public void setLockTimeout(long timeOut)
151         throws DatabaseException {
152
153         checkEnv();
154         txn.setLockTimeout(PropUtil.microsToMillis(timeOut));
155     }
156
157     /**
158      * Javadoc for this public method is generated via
159      * the doc templates in the doc_src directory.
160      */

161     public void setName(String JavaDoc name) {
162     this.name = name;
163     }
164
165     /**
166      * Javadoc for this public method is generated via
167      * the doc templates in the doc_src directory.
168      */

169     public String JavaDoc getName() {
170     return name;
171     }
172
173     public int hashCode() {
174     return (int) id;
175     }
176
177     public boolean equals(Object JavaDoc o) {
178     if (o == null) {
179         return false;
180     }
181
182     if (!(o instanceof Transaction)) {
183         return false;
184     }
185
186     if (((Transaction) o).id == id) {
187         return true;
188     }
189
190     return false;
191     }
192
193     public String JavaDoc toString() {
194     StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
195     sb.append("<Transaction id=\"");
196     sb.append(id).append("\"");
197     if (name != null) {
198         sb.append(" name=\"");
199         sb.append(name).append("\"");
200         }
201     sb.append(">");
202     return sb.toString();
203     }
204
205     /**
206      * This method should only be called by the LockerFactory.getReadableLocker
207      * and getWritableLocker methods. The locker returned does not enforce the
208      * readCommitted isolation setting.
209      */

210     Locker getLocker()
211         throws DatabaseException {
212
213         if (txn == null) {
214             throw new DatabaseException("Transaction " + id +
215                                         " has been closed and is no longer"+
216                                         " usable.");
217         } else {
218             return txn;
219         }
220     }
221
222     /*
223      * Helpers
224      */

225
226     Txn getTxn() {
227     return txn;
228     }
229
230     /**
231      * @throws RunRecoveryException if the underlying environment is invalid.
232      */

233     private void checkEnv()
234         throws RunRecoveryException {
235         
236     env.getEnvironmentImpl().checkIfInvalid();
237     }
238 }
239
Popular Tags