001: /*
002: * XAPool: Open Source XA JDBC Pool
003: * Copyright (C) 2003 Objectweb.org
004: * Initial Developer: Lutris Technologies Inc.
005: * Contact: xapool-public@lists.debian-sf.objectweb.org
006: *
007: * This library is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU Lesser General Public
009: * License as published by the Free Software Foundation; either
010: * version 2.1 of the License, or any later version.
011: *
012: * This library is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this library; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
020: * USA
021: */
022: package org.enhydra.jdbc.oracle;
023:
024: import oracle.jdbc.xa.client.OracleXAResource;
025: import oracle.jdbc.xa.OracleXid;
026: import org.enhydra.jdbc.standard.StandardXAConnection;
027: import org.enhydra.jdbc.standard.StandardXADataSource;
028:
029: import javax.transaction.xa.XAResource;
030: import javax.transaction.xa.XAException;
031: import javax.transaction.xa.Xid;
032: import java.util.Hashtable;
033: import java.sql.SQLException;
034:
035: /**
036: * Provides an Oracle specific instance of StandardXAConnection. Almost all of
037: * the required functionality is provided curtesy of the generic super class
038: * which looks after most of the transaction state management. Oracle's
039: * own resource manager is informed that it is part of a global transaction
040: * and looks after the detail thereafter.
041: */
042: public final class OracleXAConnection extends StandardXAConnection {
043:
044: private XAResource xarsrc = null;
045: private static Hashtable txctxs = new Hashtable();
046:
047: /**
048: * Creates the first free connection.
049: */
050: public OracleXAConnection(StandardXADataSource dataSource,
051: String user, String password) throws SQLException {
052: super (dataSource, user, password); // creates the first Connection object
053: }
054:
055: private OracleXid getOracleXid(Xid xid) throws XAException {
056: if (!(xid instanceof OracleXid)) {
057: byte[] txctx = (byte[]) txctxs.get(xid);
058: dataSource.log.debug("txctx is " + txctx);
059: OracleXid newXid = new OracleXid(xid.getFormatId(), xid
060: .getGlobalTransactionId(),
061: xid.getBranchQualifier(), txctx);
062: return newXid;
063: } else {
064: return (OracleXid) xid;
065: }
066: }
067:
068: public void commit(Xid xid, boolean flag) throws XAException {
069: dataSource.log.debug("commit:" + xid.getGlobalTransactionId());
070: xarsrc.commit(getOracleXid(xid), flag);
071: xaDataSource.freeConnection(xid, false);
072: txctxs.remove(xid);
073: }
074:
075: public void end(Xid xid, int flags) throws XAException {
076: dataSource.log.debug("end" + ":" + xid.getFormatId() + ":"
077: + xid.getGlobalTransactionId() + ":"
078: + xid.getBranchQualifier() + ":" + flags);
079: xarsrc.end(getOracleXid(xid), flags);
080: }
081:
082: public void forget(Xid xid) throws XAException {
083: dataSource.log.debug("forget" + ":"
084: + xid.getGlobalTransactionId());
085: xarsrc.forget(getOracleXid(xid));
086: xaDataSource.freeConnection(xid, false);
087: txctxs.remove(xid);
088: }
089:
090: public int prepare(Xid xid) throws XAException {
091: dataSource.log.debug("prepare" + ":"
092: + xid.getGlobalTransactionId());
093: int res = xarsrc.prepare(getOracleXid(xid));
094: if (res == XA_RDONLY) {
095: xaDataSource.freeConnection(xid, false);
096: txctxs.remove(xid);
097: }
098: return res;
099: }
100:
101: public void rollback(Xid xid) throws XAException {
102: dataSource.log.debug("rollback" + ":"
103: + xid.getGlobalTransactionId());
104: xarsrc.rollback(getOracleXid(xid));
105: xaDataSource.freeConnection(xid, false);
106: txctxs.remove(xid);
107: }
108:
109: public void start(Xid xid, int flags) throws XAException {
110: dataSource.log.debug("start" + ":" + xid.getFormatId() + ":"
111: + xid.getGlobalTransactionId() + ":"
112: + xid.getBranchQualifier() + ":" + flags);
113: doStart(xid, flags);
114: xarsrc = new OracleXAResource(curCon.con);
115: OracleXid oXid = getOracleXid(xid);
116: xarsrc.start(oXid, flags);
117: txctxs.put(xid, oXid.getTxContext());
118: curCon = null;
119: con = null;
120: }
121:
122: public boolean isSameRM(XAResource res) throws XAException {
123: if (!(res instanceof OracleXAConnection)) {
124: dataSource.log.debug("isSameRM returning false");
125: return false;
126: }
127: OracleXAConnection ores = (OracleXAConnection) res;
128: if (ores.xarsrc.isSameRM(xarsrc)) {
129: dataSource.log.debug("isSameRM returning true");
130: return true;
131: }
132: dataSource.log.debug("isSameRM returning false");
133: return false;
134: }
135:
136: }
|