package org.enhydra.jdbc.oracle;
import oracle.jdbc.xa.client.OracleXAResource;
import oracle.jdbc.xa.OracleXid;
import org.enhydra.jdbc.standard.StandardXAConnection;
import org.enhydra.jdbc.standard.StandardXADataSource;
import javax.transaction.xa.XAResource;
import javax.transaction.xa.XAException;
import javax.transaction.xa.Xid;
import java.util.Hashtable;
import java.sql.SQLException;
public final class OracleXAConnection extends StandardXAConnection {
private XAResource xarsrc = null;
private static Hashtable txctxs = new Hashtable();
public OracleXAConnection (StandardXADataSource dataSource, String user, String password) throws SQLException {
super (dataSource, user, password);
}
private OracleXid getOracleXid(Xid xid) throws XAException {
if (!(xid instanceof OracleXid)) {
byte[] txctx = (byte[])txctxs.get(xid);
dataSource.log.debug("txctx is " + txctx);
OracleXid newXid = new OracleXid(xid.getFormatId(), xid.getGlobalTransactionId(), xid.getBranchQualifier(), txctx);
return newXid;
} else {
return (OracleXid)xid;
}
}
public void commit(Xid xid, boolean flag) throws XAException {
dataSource.log.debug("commit:" + xid.getGlobalTransactionId());
xarsrc.commit(getOracleXid(xid), flag);
xaDataSource.freeConnection(xid, false);
txctxs.remove(xid);
}
public void end(Xid xid, int flags) throws XAException {
dataSource.log.debug("end" + ":" + xid.getFormatId() + ":" + xid.getGlobalTransactionId() + ":" + xid.getBranchQualifier() + ":" + flags);
xarsrc.end(getOracleXid(xid), flags);
}
public void forget(Xid xid) throws XAException {
dataSource.log.debug("forget" + ":" + xid.getGlobalTransactionId());
xarsrc.forget(getOracleXid(xid));
xaDataSource.freeConnection(xid, false);
txctxs.remove(xid);
}
public int prepare(Xid xid) throws XAException {
dataSource.log.debug("prepare" + ":" + xid.getGlobalTransactionId());
int res = xarsrc.prepare(getOracleXid(xid));
if (res == XA_RDONLY) {
xaDataSource.freeConnection(xid, false);
txctxs.remove(xid);
}
return res;
}
public void rollback(Xid xid) throws XAException {
dataSource.log.debug("rollback" + ":" + xid.getGlobalTransactionId());
xarsrc.rollback(getOracleXid(xid));
xaDataSource.freeConnection(xid, false);
txctxs.remove(xid);
}
public void start(Xid xid, int flags) throws XAException {
dataSource.log.debug("start" + ":" + xid.getFormatId() + ":" + xid.getGlobalTransactionId() + ":" + xid.getBranchQualifier() + ":" + flags);
doStart(xid, flags);
xarsrc = new OracleXAResource(curCon.con);
OracleXid oXid = getOracleXid(xid);
xarsrc.start(oXid, flags);
txctxs.put(xid, oXid.getTxContext());
curCon = null;
con = null;
}
public boolean isSameRM(XAResource res) throws XAException {
if (!(res instanceof OracleXAConnection)) {
dataSource.log.debug("isSameRM returning false");
return false;
}
OracleXAConnection ores = (OracleXAConnection)res;
if (ores.xarsrc.isSameRM(xarsrc)) {
dataSource.log.debug("isSameRM returning true");
return true;
}
dataSource.log.debug("isSameRM returning false");
return false;
}
}