package org.jboss.resource.adapter.jdbc.xa;
import java.sql.SQLException;
import java.util.Properties;
import javax.resource.ResourceException;
import javax.resource.spi.LocalTransaction;
import javax.sql.XAConnection;
import javax.transaction.xa.XAException;
import javax.transaction.xa.XAResource;
import javax.transaction.xa.Xid;
import org.jboss.resource.JBossResourceException;
import org.jboss.resource.adapter.jdbc.BaseWrapperManagedConnection;
public class XAManagedConnection extends BaseWrapperManagedConnection implements XAResource
{
protected final XAConnection xaConnection;
protected final XAResource xaResource;
protected Xid currentXid;
public XAManagedConnection(XAManagedConnectionFactory mcf, XAConnection xaConnection, Properties props,
int transactionIsolation, int psCacheSize) throws SQLException
{
super(mcf, xaConnection.getConnection(), props, transactionIsolation, psCacheSize);
this.xaConnection = xaConnection;
xaConnection.addConnectionEventListener(new javax.sql.ConnectionEventListener()
{
public void connectionClosed(javax.sql.ConnectionEvent ce)
{
}
public void connectionErrorOccurred(javax.sql.ConnectionEvent ce)
{
SQLException ex = ce.getSQLException();
broadcastConnectionError(ex);
}
});
this.xaResource = xaConnection.getXAResource();
}
protected void broadcastConnectionError(SQLException e)
{
super.broadcastConnectionError(e);
}
public LocalTransaction getLocalTransaction() throws ResourceException
{
throw new JBossResourceException("xa tx only!");
}
public XAResource getXAResource() throws ResourceException
{
return this;
}
public void destroy() throws ResourceException
{
try
{
super.destroy();
}
finally
{
try
{
xaConnection.close();
}
catch (SQLException e)
{
checkException(e);
} } }
public void start(Xid xid, int flags) throws XAException
{
try
{
checkState();
}
catch (SQLException e)
{
getLog().warn("Error setting state ", e);
}
xaResource.start(xid, flags);
synchronized (stateLock)
{
currentXid = xid;
inManagedTransaction = true;
}
}
public void end(Xid xid, int flags) throws XAException
{
xaResource.end(xid, flags);
synchronized (stateLock)
{
if (currentXid != null && currentXid.equals(xid))
{
inManagedTransaction = false;
currentXid = null;
}
}
}
public int prepare(Xid xid) throws XAException
{
return xaResource.prepare(xid);
}
public void commit(Xid xid, boolean onePhase) throws XAException
{
xaResource.commit(xid, onePhase);
}
public void rollback(Xid xid) throws XAException
{
xaResource.rollback(xid);
}
public void forget(Xid xid) throws XAException
{
xaResource.forget(xid);
}
public Xid[] recover(int flag) throws XAException
{
return xaResource.recover(flag);
}
public boolean isSameRM(XAResource other) throws XAException
{
Boolean overrideValue = ((XAManagedConnectionFactory) mcf).getIsSameRMOverrideValue();
if (overrideValue != null)
{
return overrideValue.booleanValue();
}
return (other instanceof XAManagedConnection)
? xaResource.isSameRM(((XAManagedConnection) other).xaResource)
: xaResource.isSameRM(other);
}
public int getTransactionTimeout() throws XAException
{
return xaResource.getTransactionTimeout();
}
public boolean setTransactionTimeout(int seconds) throws XAException
{
return xaResource.setTransactionTimeout(seconds);
}
Properties getProps()
{
return props;
}
}