KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jalisto > se > jca > JalistoXAResource


1 /*
2  * Jalisto - JAva LIght STOrage
3  * Copyright (C) 2000-2005 Xcalia http://www.xcalia.com
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
18  *
19  * Xcalia
20  * 71, rue Desnouettes
21  * 75014 Paris - France
22  * http://www.xcalia.com
23  */

24 package org.objectweb.jalisto.se.jca;
25
26 import org.objectweb.jalisto.se.impl.trace.Trace;
27 import org.objectweb.jalisto.se.jca.session.ManagedSession;
28 import org.objectweb.jalisto.se.jca.session.ManagedTransactionImpl;
29
30 import javax.transaction.xa.XAException JavaDoc;
31 import javax.transaction.xa.Xid JavaDoc;
32
33 public class JalistoXAResource {
34
35     public JalistoXAResource(ManagedSession mpm, Trace trace) {
36         this.managedJalistoSession = mpm;
37         this.trace = trace;
38         this.transaction = (ManagedTransactionImpl) mpm.currentTransaction();
39     }
40
41     public void setCurrentXid(Xid JavaDoc currentXid) {
42         this.currentXid = currentXid;
43     }
44
45     public Xid JavaDoc getCurrentXid() {
46         return currentXid;
47     }
48
49     public void setManagedJalistoSession(ManagedSession managedJalistoSession) {
50         this.managedJalistoSession = managedJalistoSession;
51     }
52
53     public ManagedSession getManagedJalistoSession() {
54         return managedJalistoSession;
55     }
56
57     public boolean isSameJDOXAR(JalistoXAResource jdoxar) {
58         if ((managedJalistoSession == null) || (jdoxar == null)) {
59             return false;
60         }
61
62         return managedJalistoSession.equals(jdoxar.managedJalistoSession);
63     }
64
65     public void commit(Xid JavaDoc xid, boolean b) throws XAException JavaDoc {
66         getTrace().println(Trace.JCA,
67                            TRACE_ID + " commit XAResource : {0} current xid : {1} call with xid : {2}",
68                            this, currentXid, xid);
69         getTrace().println(Trace.JCA, TRACE_ID + " hashcodes : {0}, {1}",
70                            String.valueOf(currentXid.hashCode()),
71                            String.valueOf(xid.hashCode()));
72
73         if (transaction.isActive()) {
74             if ((currentXid == null) || (currentXid.hashCode() != xid.hashCode()) || !xaUsed) {
75                 if (currentXid == null) {
76                     throw new XAException JavaDoc("Cannot mix transactions (currentXid == null)");
77                 }
78                 if (currentXid.hashCode() != xid.hashCode()) {
79                     throw new XAException JavaDoc("Cannot mix transactions (currentXid.hashCode() != xid.hashCode())");
80                 }
81                 if (!xaUsed) {
82                     throw new XAException JavaDoc("Cannot mix transactions (!xaUsed)");
83                 }
84             }
85
86             transaction.commit();
87             managedJalistoSession.finishTransaction();
88             JalistoXARPool.releaseXAResource(this);
89             currentXid = null;
90             xaUsed = false;
91         }
92     }
93
94     public void rollback(Xid JavaDoc xid) throws XAException JavaDoc {
95         getTrace().println(Trace.JCA,
96                            TRACE_ID + " commit XAResource : {0} current xid : {1} call with xid : {2}",
97                            this, currentXid, xid);
98         getTrace().println(Trace.JCA, TRACE_ID + " hashcodes : {0}, {1}",
99                            String.valueOf(currentXid.hashCode()),
100                            String.valueOf(xid.hashCode()));
101
102         if (transaction.isActive()) {
103             if ((currentXid == null) || (currentXid.hashCode() != xid.hashCode()) || !xaUsed) {
104                 if (currentXid == null) {
105                     throw new XAException JavaDoc("Cannot mix transactions (currentXid == null)");
106                 }
107                 if (currentXid.hashCode() != xid.hashCode()) {
108                     throw new XAException JavaDoc("Cannot mix transactions (currentXid.hashCode() != xid.hashCode())");
109                 }
110                 if (!xaUsed) {
111                     throw new XAException JavaDoc("Cannot mix transactions (!xaUsed)");
112                 }
113             }
114
115             transaction.rollback();
116             managedJalistoSession.finishTransaction();
117             JalistoXARPool.releaseXAResource(this);
118             currentXid = null;
119             xaUsed = false;
120         }
121     }
122
123     public void start(Xid JavaDoc xid, JalistoManagedConnection mc) throws XAException JavaDoc {
124         if (xaUsed) {
125             if (currentXid == null) {
126                 throw new XAException JavaDoc("XA START: xaUsed && currentXid == null");
127             }
128         }
129         xaUsed = true;
130         currentXid = xid;
131         try {
132             mc.setManagedJalistoSession(managedJalistoSession);
133             if (!transaction.isActive()) {
134                 transaction.begin();
135                 managedJalistoSession.startTransaction(xid);
136             }
137         } catch (Throwable JavaDoc t) {
138             t.printStackTrace();
139             throw new XAException JavaDoc("ERROR during IES association : " + t.getMessage());
140         }
141         getTrace().println(Trace.JCA, TRACE_ID + " start");
142     }
143
144     private Trace getTrace() {
145         return trace;
146     }
147
148
149     private ManagedSession managedJalistoSession;
150     private ManagedTransactionImpl transaction;
151     private Xid JavaDoc currentXid = null;
152     private boolean xaUsed = false;
153     int timeout;
154     private Trace trace;
155
156     private static final String JavaDoc TRACE_ID = "[JalistoXAResource]";
157
158 }
159
Popular Tags