KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > gjc > spi > XAResourceImpl


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24 package com.sun.gjc.spi;
25
26 import javax.transaction.xa.Xid JavaDoc;
27 import javax.transaction.xa.XAException JavaDoc;
28 import javax.transaction.xa.XAResource JavaDoc;
29 import com.sun.gjc.spi.ManagedConnection;
30
31 /**
32  * <code>XAResource</code> wrapper for Generic JDBC Connector.
33  *
34  * @version 1.0, 02/08/23
35  * @author Evani Sai Surya Kiran
36  */

37 public class XAResourceImpl implements XAResource JavaDoc {
38
39     XAResource JavaDoc xar;
40     ManagedConnection mc;
41     
42     /**
43      * Constructor for XAResourceImpl
44      *
45      * @param xar <code>XAResource</code>
46      * @param mc <code>ManagedConnection</code>
47      */

48     public XAResourceImpl(XAResource JavaDoc xar, ManagedConnection mc) {
49         this.xar = xar;
50         this.mc = mc;
51     }
52     
53     /**
54      * Commit the global transaction specified by xid.
55      *
56      * @param xid A global transaction identifier
57      * @param onePhase If true, the resource manager should use a one-phase commit
58      * protocol to commit the work done on behalf of xid.
59      */

60     public void commit(Xid JavaDoc xid, boolean onePhase) throws XAException JavaDoc {
61         //the mc.transactionCompleted call has come here becasue
62
//the transaction *actually* completes after the flow
63
//reaches here. the end() method might not really signal
64
//completion of transaction in case the transaction is
65
//suspended. In case of transaction suspension, the end
66
//method is still called by the transaction manager
67
try{
68              xar.commit(xid, onePhase);
69          }catch(XAException JavaDoc xae){
70              throw xae;
71          }catch(Exception JavaDoc e){
72              throw new XAException JavaDoc(e.getMessage());
73          }finally{
74              mc.transactionCompleted();
75          }
76     }
77     
78     /**
79      * Ends the work performed on behalf of a transaction branch.
80      *
81      * @param xid A global transaction identifier that is the same as what
82      * was used previously in the start method.
83      * @param flags One of TMSUCCESS, TMFAIL, or TMSUSPEND
84      */

85     public void end(Xid JavaDoc xid, int flags) throws XAException JavaDoc {
86         xar.end(xid, flags);
87         //GJCINT
88
//mc.transactionCompleted();
89
}
90     
91     /**
92      * Tell the resource manager to forget about a heuristically completed transaction branch.
93      *
94      * @param xid A global transaction identifier
95      */

96     public void forget(Xid JavaDoc xid) throws XAException JavaDoc {
97         xar.forget(xid);
98     }
99     
100     /**
101      * Obtain the current transaction timeout value set for this
102      * <code>XAResource</code> instance.
103      *
104      * @return the transaction timeout value in seconds
105      */

106     public int getTransactionTimeout() throws XAException JavaDoc {
107         return xar.getTransactionTimeout();
108     }
109     
110     /**
111      * This method is called to determine if the resource manager instance
112      * represented by the target object is the same as the resouce manager
113      * instance represented by the parameter xares.
114      *
115      * @param xares An <code>XAResource</code> object whose resource manager
116      * instance is to be compared with the resource
117      * @return true if it's the same RM instance; otherwise false.
118      */

119     public boolean isSameRM(XAResource JavaDoc xares) throws XAException JavaDoc {
120         return xar.isSameRM(xares);
121     }
122     
123     /**
124      * Ask the resource manager to prepare for a transaction commit
125      * of the transaction specified in xid.
126      *
127      * @param xid A global transaction identifier
128      * @return A value indicating the resource manager's vote on the
129      * outcome of the transaction. The possible values
130      * are: XA_RDONLY or XA_OK. If the resource manager wants
131      * to roll back the transaction, it should do so
132      * by raising an appropriate <code>XAException</code> in the prepare method.
133      */

134     public int prepare(Xid JavaDoc xid) throws XAException JavaDoc {
135     try{
136             return xar.prepare(xid);
137     }catch(XAException JavaDoc xae){
138         mc.transactionCompleted();
139         throw xae;
140     }catch(Exception JavaDoc e){
141         mc.transactionCompleted();
142         throw new XAException JavaDoc(e.getMessage());
143     }
144     }
145     
146     /**
147      * Obtain a list of prepared transaction branches from a resource manager.
148      *
149      * @param flag One of TMSTARTRSCAN, TMENDRSCAN, TMNOFLAGS. TMNOFLAGS
150      * must be used when no other flags are set in flags.
151      * @return The resource manager returns zero or more XIDs for the transaction
152      * branches that are currently in a prepared or heuristically
153      * completed state. If an error occurs during the operation, the resource
154      * manager should throw the appropriate <code>XAException</code>.
155      */

156     public Xid JavaDoc[] recover(int flag) throws XAException JavaDoc {
157         return xar.recover(flag);
158     }
159     
160     /**
161      * Inform the resource manager to roll back work done on behalf of a transaction branch
162      *
163      * @param xid A global transaction identifier
164      */

165     public void rollback(Xid JavaDoc xid) throws XAException JavaDoc {
166         //the mc.transactionCompleted call has come here becasue
167
//the transaction *actually* completes after the flow
168
//reaches here. the end() method might not really signal
169
//completion of transaction in case the transaction is
170
//suspended. In case of transaction suspension, the end
171
//method is still called by the transaction manager
172
try{
173              xar.rollback(xid);
174          }catch(XAException JavaDoc xae){
175              throw xae;
176          }catch(Exception JavaDoc e){
177              throw new XAException JavaDoc(e.getMessage());
178          }finally{
179              mc.transactionCompleted();
180          }
181     }
182     
183     /**
184      * Set the current transaction timeout value for this <code>XAResource</code> instance.
185      *
186      * @param seconds the transaction timeout value in seconds.
187      * @return true if transaction timeout value is set successfully; otherwise false.
188      */

189     public boolean setTransactionTimeout(int seconds) throws XAException JavaDoc {
190         return xar.setTransactionTimeout(seconds);
191     }
192     
193     /**
194      * Start work on behalf of a transaction branch specified in xid.
195      *
196      * @param xid A global transaction identifier to be associated with the resource
197      * @return flags One of TMNOFLAGS, TMJOIN, or TMRESUME
198      */

199     public void start(Xid JavaDoc xid, int flags) throws XAException JavaDoc {
200         //GJCINT
201
mc.transactionStarted();
202         xar.start(xid, flags);
203     }
204 }
205
Popular Tags