mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
add xa support
This commit is contained in:
@@ -0,0 +1,73 @@
|
||||
package com.alibaba.druid.pool.xa;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.SQLException;
|
||||
|
||||
import javax.sql.ConnectionEventListener;
|
||||
import javax.sql.StatementEventListener;
|
||||
import javax.sql.XAConnection;
|
||||
import javax.transaction.xa.XAResource;
|
||||
|
||||
import com.alibaba.druid.util.JdbcUtils;
|
||||
|
||||
import net.sourceforge.jtds.jdbc.XASupport;
|
||||
|
||||
public class JtdsXAConnection implements XAConnection {
|
||||
|
||||
private Connection connection;
|
||||
|
||||
private final XAResource resource;
|
||||
private final int xaConnectionId;
|
||||
|
||||
public JtdsXAConnection(Connection connection) throws SQLException{
|
||||
this.resource = new JtdsXAResource(this, connection);
|
||||
this.connection = connection;
|
||||
this.xaConnectionId = XASupport.xa_open(connection);
|
||||
}
|
||||
|
||||
int getXAConnectionID() {
|
||||
return this.xaConnectionId;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Connection getConnection() throws SQLException {
|
||||
return connection;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() throws SQLException {
|
||||
try {
|
||||
XASupport.xa_close(connection, xaConnectionId);
|
||||
} catch (SQLException e) {
|
||||
// Ignore close errors
|
||||
}
|
||||
|
||||
JdbcUtils.close(connection);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addConnectionEventListener(ConnectionEventListener listener) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void removeConnectionEventListener(ConnectionEventListener listener) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addStatementEventListener(StatementEventListener listener) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void removeStatementEventListener(StatementEventListener listener) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public XAResource getXAResource() throws SQLException {
|
||||
return resource;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,87 @@
|
||||
package com.alibaba.druid.pool.xa;
|
||||
|
||||
import java.sql.Connection;
|
||||
|
||||
import javax.transaction.xa.XAException;
|
||||
import javax.transaction.xa.XAResource;
|
||||
import javax.transaction.xa.Xid;
|
||||
|
||||
import net.sourceforge.jtds.jdbc.ConnectionJDBC2;
|
||||
import net.sourceforge.jtds.jdbc.XASupport;
|
||||
|
||||
public class JtdsXAResource implements XAResource {
|
||||
|
||||
private final Connection connection;
|
||||
private final JtdsXAConnection xaConnection;
|
||||
private final String rmHost;
|
||||
|
||||
public JtdsXAResource(JtdsXAConnection xaConnection, Connection connection){
|
||||
this.xaConnection = xaConnection;
|
||||
this.connection = connection;
|
||||
rmHost = ((ConnectionJDBC2) connection).getRmHost();
|
||||
}
|
||||
|
||||
protected JtdsXAConnection getResourceManager() {
|
||||
return xaConnection;
|
||||
}
|
||||
|
||||
protected String getRmHost() {
|
||||
return this.rmHost;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void commit(Xid xid, boolean commit) throws XAException {
|
||||
XASupport.xa_commit(connection, xaConnection.getXAConnectionID(), xid, commit);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void end(Xid xid, int flags) throws XAException {
|
||||
XASupport.xa_end(connection, xaConnection.getXAConnectionID(), xid, flags);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void forget(Xid xid) throws XAException {
|
||||
XASupport.xa_forget(connection, xaConnection.getXAConnectionID(), xid);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getTransactionTimeout() throws XAException {
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isSameRM(XAResource xares) throws XAException {
|
||||
if (xares instanceof JtdsXAResource) {
|
||||
if (((JtdsXAResource) xares).getRmHost().equals(this.rmHost)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int prepare(Xid xid) throws XAException {
|
||||
return XASupport.xa_prepare(connection, xaConnection.getXAConnectionID(), xid);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Xid[] recover(int flags) throws XAException {
|
||||
return XASupport.xa_recover(connection, xaConnection.getXAConnectionID(), flags);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void rollback(Xid xid) throws XAException {
|
||||
XASupport.xa_rollback(connection, xaConnection.getXAConnectionID(), xid);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean setTransactionTimeout(int seconds) throws XAException {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void start(Xid xid, int flags) throws XAException {
|
||||
XASupport.xa_start(connection, xaConnection.getXAConnectionID(), xid, flags);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user