我在使用Bitronix将分布式事务执行到两个数据源时收到此警告“使用0登记资源执行事务”。有人可以帮助理解如何将资源登记到全局事务。我的代码是:
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import javax.sql.DataSource;
import javax.transaction.HeuristicMixedException;
import javax.transaction.HeuristicRollbackException;
import javax.transaction.NotSupportedException;
import javax.transaction.RollbackException;
import javax.transaction.SystemException;
import bitronix.tm.BitronixTransactionManager;
import bitronix.tm.TransactionManagerServices;
public class TestMain {
public static void main(String[] args) {
new TestMain().TestDT();
}
private void TestDT(){
DataSource dataSourceRemote = ConnectionManager.getDatasourceRemote();
DataSource dataSourceLocal = ConnectionManager.getDatasourceLocal();
Connection con, conn = null;
try {
/*String remoteDS = "remoteDS";
String localDS = "localDS";
InitialContext ctxRemote = new InitialContext();
ctxRemote.bind(remoteDS, dataSourceRemote);*/
String INSERT_QUERY = "insert emp values (?,?,?,?)";
/*InitialContext ctxLocal = new InitialContext();
ctxLocal.bind(localDS, dataSourceLocal);*/
BitronixTransactionManager btx = TransactionManagerServices.getTransactionManager();
//DataSource dsRemote = null;
//btx.ge
//UserTransaction us =
try {
btx.begin();
//DataSource dsRemote = (DataSource) ctxRemote.lookup(remoteDS);
con = dataSourceRemote.getConnection();
//con.setAutoCommit(false);
PreparedStatement pstmt = con.prepareStatement(INSERT_QUERY);
for(int i=1; i<=5; i++){
pstmt.setInt(1, i);
pstmt.setString(2, "Sanjay_"+i);
pstmt.setString(3, "123"+i);
pstmt.setString(4, "1000"+i);
pstmt.execute();
}
//DataSource dsLocal = (DataSource) ctxLocal.lookup(localDS);
conn = dataSourceLocal.getConnection();
//conn.setAutoCommit(false);
PreparedStatement ps = conn.prepareStatement(INSERT_QUERY);
for(int i=1; i<=5; i++){
ps.setInt(1, i);
ps.setString(2, "Nikhil_"+i);
ps.setString(3, "123"+i);
ps.setString(4, "1000"+i);
ps.execute();
}
btx.commit();
con.close();
conn.close();
} catch (NotSupportedException e) {
e.printStackTrace();
} catch (SystemException e) {
e.printStackTrace();
}catch (SQLException e) {
try{
btx.rollback();
}catch (Exception ex) {
ex.printStackTrace();
}
e.printStackTrace();
}catch (HeuristicRollbackException e) {
try{
btx.rollback();
}catch (Exception ex) {
ex.printStackTrace();
}
e.printStackTrace();
}catch (HeuristicMixedException e) {
try{
btx.rollback();
}catch (Exception ex) {
ex.printStackTrace();
}
e.printStackTrace();
}catch (RollbackException e) {
try{
btx.rollback();
}catch (Exception ex) {
ex.printStackTrace();
}
e.printStackTrace();
}finally{
//try{
btx.shutdown();
//con.close();
//conn.close();
/*}catch (Exception e) {
e.printStackTrace();
}*/
}
//btx.getTransaction()
}catch (Exception e) {
e.printStackTrace();
}
}
}
答案 0 :(得分:-2)
“使用0登记资源执行事务”只是意味着您的数据源未与Bitronix事务管理器映射。