001: /*-
002: * See the file LICENSE for redistribution information.
003: *
004: * Copyright (c) 2002,2008 Oracle. All rights reserved.
005: *
006: * $Id: Transaction.java,v 1.48.2.5 2008/02/21 08:09:02 chao Exp $
007: */
008:
009: package com.sleepycat.je;
010:
011: import com.sleepycat.je.txn.Locker;
012: import com.sleepycat.je.txn.Txn;
013: import com.sleepycat.je.utilint.PropUtil;
014:
015: /**
016: * Javadoc for this public class is generated
017: * via the doc templates in the doc_src directory.
018: */
019: public class Transaction {
020:
021: private Txn txn;
022: private Environment env;
023: private long id;
024: private String name;
025:
026: /**
027: * Creates a transaction.
028: */
029: Transaction(Environment env, Txn txn) {
030: this .env = env;
031: this .txn = txn;
032:
033: /*
034: * Copy the id to this wrapper object so the id will be available
035: * after the transaction is closed and the txn field is nulled.
036: */
037: this .id = txn.getId();
038: }
039:
040: /**
041: * Javadoc for this public method is generated via
042: * the doc templates in the doc_src directory.
043: */
044: public void abort() throws DatabaseException {
045:
046: try {
047: checkEnv();
048: env.removeReferringHandle(this );
049: txn.abort(false); // no sync required
050:
051: /* Remove reference to internal txn, so we can reclaim memory. */
052: txn = null;
053: } catch (Error E) {
054: DbInternal.envGetEnvironmentImpl(env).invalidate(E);
055: throw E;
056: }
057: }
058:
059: /**
060: * Javadoc for this public method is generated via
061: * the doc templates in the doc_src directory.
062: */
063: public long getId() throws DatabaseException {
064:
065: return id;
066: }
067:
068: /**
069: * Javadoc for this public method is generated via
070: * the doc templates in the doc_src directory.
071: */
072: public void commit() throws DatabaseException {
073:
074: try {
075: checkEnv();
076: env.removeReferringHandle(this );
077: txn.commit();
078: /* Remove reference to internal txn, so we can reclaim memory. */
079: txn = null;
080: } catch (Error E) {
081: DbInternal.envGetEnvironmentImpl(env).invalidate(E);
082: throw E;
083: }
084: }
085:
086: /**
087: * Javadoc for this public method is generated via
088: * the doc templates in the doc_src directory.
089: */
090: public void commitSync() throws DatabaseException {
091:
092: doCommit(Txn.TXN_SYNC);
093: }
094:
095: /**
096: * Javadoc for this public method is generated via
097: * the doc templates in the doc_src directory.
098: */
099: public void commitNoSync() throws DatabaseException {
100:
101: doCommit(Txn.TXN_NOSYNC);
102: }
103:
104: /**
105: * Javadoc for this public method is generated via
106: * the doc templates in the doc_src directory.
107: */
108: public void commitWriteNoSync() throws DatabaseException {
109:
110: doCommit(Txn.TXN_WRITE_NOSYNC);
111: }
112:
113: public boolean getPrepared() {
114: return txn.getPrepared();
115: }
116:
117: private void doCommit(byte commitType) throws DatabaseException {
118:
119: try {
120: checkEnv();
121: env.removeReferringHandle(this );
122: txn.commit(commitType);
123:
124: /* Remove reference to internal txn, so we can reclaim memory. */
125: txn = null;
126: } catch (Error E) {
127: DbInternal.envGetEnvironmentImpl(env).invalidate(E);
128: throw E;
129: }
130: }
131:
132: /**
133: * Javadoc for this public method is generated via
134: * the doc templates in the doc_src directory.
135: */
136: public void setTxnTimeout(long timeout)
137: throws IllegalArgumentException, DatabaseException {
138:
139: checkEnv();
140: txn.setTxnTimeout(PropUtil.microsToMillis(timeout));
141: }
142:
143: /**
144: * Javadoc for this public method is generated via
145: * the doc templates in the doc_src directory.
146: */
147: public void setLockTimeout(long timeout)
148: throws IllegalArgumentException, DatabaseException {
149:
150: checkEnv();
151: txn.setLockTimeout(PropUtil.microsToMillis(timeout));
152: }
153:
154: /**
155: * Javadoc for this public method is generated via
156: * the doc templates in the doc_src directory.
157: */
158: public void setName(String name) {
159: this .name = name;
160: }
161:
162: /**
163: * Javadoc for this public method is generated via
164: * the doc templates in the doc_src directory.
165: */
166: public String getName() {
167: return name;
168: }
169:
170: public int hashCode() {
171: return (int) id;
172: }
173:
174: public boolean equals(Object o) {
175: if (o == null) {
176: return false;
177: }
178:
179: if (!(o instanceof Transaction)) {
180: return false;
181: }
182:
183: if (((Transaction) o).id == id) {
184: return true;
185: }
186:
187: return false;
188: }
189:
190: public String toString() {
191: StringBuffer sb = new StringBuffer();
192: sb.append("<Transaction id=\"");
193: sb.append(id).append("\"");
194: if (name != null) {
195: sb.append(" name=\"");
196: sb.append(name).append("\"");
197: }
198: sb.append(">");
199: return sb.toString();
200: }
201:
202: /**
203: * This method should only be called by the LockerFactory.getReadableLocker
204: * and getWritableLocker methods. The locker returned does not enforce the
205: * readCommitted isolation setting.
206: */
207: Locker getLocker() throws DatabaseException {
208:
209: if (txn == null) {
210: throw new DatabaseException("Transaction " + id
211: + " has been closed and is no longer" + " usable.");
212: } else {
213: return txn;
214: }
215: }
216:
217: /*
218: * Helpers
219: */
220:
221: Txn getTxn() {
222: return txn;
223: }
224:
225: /**
226: * @throws RunRecoveryException if the underlying environment is invalid.
227: */
228: private void checkEnv() throws RunRecoveryException {
229:
230: env.getEnvironmentImpl().checkIfInvalid();
231: }
232: }
|