KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sleepycat > je > jca > ra > JELocalTransaction


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

8
9 package com.sleepycat.je.jca.ra;
10
11 import javax.resource.ResourceException JavaDoc;
12 import javax.resource.spi.ConnectionEvent JavaDoc;
13
14 import com.sleepycat.je.DatabaseException;
15 import com.sleepycat.je.Transaction;
16 import com.sleepycat.je.TransactionConfig;
17 import com.sleepycat.je.XAEnvironment;
18
19 public class JELocalTransaction
20     implements javax.resource.cci.LocalTransaction JavaDoc,
21            javax.resource.spi.LocalTransaction JavaDoc {
22
23     private static boolean DEBUG = false;
24
25     private transient XAEnvironment env;
26     private transient TransactionConfig transConfig;
27     private transient JEManagedConnection mgdConn;
28
29     JELocalTransaction(XAEnvironment env,
30                TransactionConfig transConfig,
31                JEManagedConnection mgdConn) {
32     this.env = env;
33     this.transConfig = transConfig;
34     this.mgdConn = mgdConn;
35     }
36
37     public Transaction getTransaction()
38     throws DatabaseException {
39
40     return env.getThreadTransaction();
41     }
42
43     protected XAEnvironment getEnv() {
44     return env;
45     }
46
47     private void checkEnv(String JavaDoc methodName)
48     throws ResourceException JavaDoc {
49
50     if (env == null) {
51         throw new ResourceException JavaDoc("env is null in " + methodName);
52     }
53     }
54
55     /*
56      * Methods for LocalTransaction.
57      */

58
59     public void begin()
60     throws ResourceException JavaDoc {
61
62     checkEnv("begin");
63     long id = -1;
64     try {
65         Transaction txn = env.beginTransaction(null, transConfig);
66         env.setThreadTransaction(txn);
67         id = txn.getId();
68     } catch (DatabaseException DE) {
69         throw new ResourceException JavaDoc("During begin: " + DE.toString());
70     }
71
72     ConnectionEvent JavaDoc connEvent = new ConnectionEvent JavaDoc
73         (mgdConn, ConnectionEvent.LOCAL_TRANSACTION_STARTED);
74     connEvent.setConnectionHandle(mgdConn);
75     mgdConn.sendConnectionEvent(connEvent);
76
77     if (DEBUG) {
78         System.out.println("JELocalTransaction.begin " + id);
79     }
80     }
81
82     public void commit()
83     throws ResourceException JavaDoc {
84
85     checkEnv("commit");
86     try {
87         env.getThreadTransaction().commit();
88     } catch (DatabaseException DE) {
89         ResourceException JavaDoc ret = new ResourceException JavaDoc(DE.toString());
90         ret.initCause(DE);
91         throw ret;
92     } finally {
93         env.setThreadTransaction(null);
94     }
95
96     ConnectionEvent JavaDoc connEvent = new ConnectionEvent JavaDoc
97         (mgdConn, ConnectionEvent.LOCAL_TRANSACTION_COMMITTED);
98     connEvent.setConnectionHandle(mgdConn);
99     mgdConn.sendConnectionEvent(connEvent);
100
101     if (DEBUG) {
102         System.out.println("JELocalTransaction.commit");
103     }
104     }
105
106     public void rollback()
107     throws ResourceException JavaDoc {
108
109     checkEnv("rollback");
110     try {
111         env.getThreadTransaction().abort();
112     } catch (DatabaseException DE) {
113         ResourceException JavaDoc ret = new ResourceException JavaDoc(DE.toString());
114         ret.initCause(DE);
115         throw ret;
116     } finally {
117         env.setThreadTransaction(null);
118     }
119
120     ConnectionEvent JavaDoc connEvent = new ConnectionEvent JavaDoc
121         (mgdConn, ConnectionEvent.LOCAL_TRANSACTION_ROLLEDBACK);
122     connEvent.setConnectionHandle(mgdConn);
123     mgdConn.sendConnectionEvent(connEvent);
124
125     if (DEBUG) {
126         System.out.println("JELocalTransaction.rollback");
127     }
128     }
129 }
130
Popular Tags