KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > jdbc > oracle > OracleXAConnection


1 /*
2  * XAPool: Open Source XA JDBC Pool
3  * Copyright (C) 2003 Objectweb.org
4  * Initial Developer: Lutris Technologies Inc.
5  * Contact: xapool-public@lists.debian-sf.objectweb.org
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
20  * USA
21  */

22 package org.enhydra.jdbc.oracle;
23
24
25 import oracle.jdbc.xa.client.OracleXAResource;
26 import oracle.jdbc.xa.OracleXid;
27 import org.enhydra.jdbc.standard.StandardXAConnection;
28 import org.enhydra.jdbc.standard.StandardXADataSource;
29
30 import javax.transaction.xa.XAResource JavaDoc;
31 import javax.transaction.xa.XAException JavaDoc;
32 import javax.transaction.xa.Xid JavaDoc;
33 import java.util.Hashtable JavaDoc;
34 import java.sql.SQLException JavaDoc;
35
36
37 /**
38  * Provides an Oracle specific instance of StandardXAConnection. Almost all of
39  * the required functionality is provided curtesy of the generic super class
40  * which looks after most of the transaction state management. Oracle's
41  * own resource manager is informed that it is part of a global transaction
42  * and looks after the detail thereafter.
43  */

44 public final class OracleXAConnection extends StandardXAConnection {
45
46     private XAResource xarsrc = null;
47     private static Hashtable JavaDoc txctxs = new Hashtable JavaDoc();
48
49     /**
50      * Creates the first free connection.
51      */

52     public OracleXAConnection (StandardXADataSource dataSource, String JavaDoc user, String JavaDoc password) throws SQLException JavaDoc {
53         super (dataSource, user, password); // creates the first Connection object
54
}
55
56     private OracleXid getOracleXid(Xid xid) throws XAException JavaDoc {
57         if (!(xid instanceof OracleXid)) {
58             byte[] txctx = (byte[])txctxs.get(xid);
59             dataSource.log.debug("txctx is " + txctx);
60             OracleXid newXid = new OracleXid(xid.getFormatId(), xid.getGlobalTransactionId(), xid.getBranchQualifier(), txctx);
61             return newXid;
62         } else {
63             return (OracleXid)xid;
64         }
65     }
66
67     public void commit(Xid xid, boolean flag) throws XAException JavaDoc {
68         dataSource.log.debug("commit:" + xid.getGlobalTransactionId());
69         xarsrc.commit(getOracleXid(xid), flag);
70         xaDataSource.freeConnection(xid, false);
71         txctxs.remove(xid);
72     }
73
74     public void end(Xid xid, int flags) throws XAException JavaDoc {
75         dataSource.log.debug("end" + ":" + xid.getFormatId() + ":" + xid.getGlobalTransactionId() + ":" + xid.getBranchQualifier() + ":" + flags);
76         xarsrc.end(getOracleXid(xid), flags);
77     }
78
79     public void forget(Xid xid) throws XAException JavaDoc {
80         dataSource.log.debug("forget" + ":" + xid.getGlobalTransactionId());
81         xarsrc.forget(getOracleXid(xid));
82         xaDataSource.freeConnection(xid, false);
83         txctxs.remove(xid);
84     }
85
86     public int prepare(Xid xid) throws XAException JavaDoc {
87         dataSource.log.debug("prepare" + ":" + xid.getGlobalTransactionId());
88         int res = xarsrc.prepare(getOracleXid(xid));
89         if (res == XA_RDONLY) {
90             xaDataSource.freeConnection(xid, false);
91             txctxs.remove(xid);
92         }
93         return res;
94     }
95
96     public void rollback(Xid xid) throws XAException JavaDoc {
97         dataSource.log.debug("rollback" + ":" + xid.getGlobalTransactionId());
98         xarsrc.rollback(getOracleXid(xid));
99         xaDataSource.freeConnection(xid, false);
100         txctxs.remove(xid);
101     }
102
103     public void start(Xid xid, int flags) throws XAException JavaDoc {
104         dataSource.log.debug("start" + ":" + xid.getFormatId() + ":" + xid.getGlobalTransactionId() + ":" + xid.getBranchQualifier() + ":" + flags);
105         doStart(xid, flags);
106         xarsrc = new OracleXAResource(curCon.con);
107         OracleXid oXid = getOracleXid(xid);
108         xarsrc.start(oXid, flags);
109         txctxs.put(xid, oXid.getTxContext());
110         curCon = null;
111         con = null;
112     }
113
114     public boolean isSameRM(XAResource res) throws XAException JavaDoc {
115         if (!(res instanceof OracleXAConnection)) {
116             dataSource.log.debug("isSameRM returning false");
117             return false;
118         }
119         OracleXAConnection ores = (OracleXAConnection)res;
120         if (ores.xarsrc.isSameRM(xarsrc)) {
121             dataSource.log.debug("isSameRM returning true");
122             return true;
123         }
124         dataSource.log.debug("isSameRM returning false");
125         return false;
126     }
127
128 }
129
Popular Tags