是的。您可以使用純JDBC的JTA。總體思路是,不使用JDBC Connection對象來聲明事務邊界,而是使用由JTA實現提供的事務管理器對象來聲明事務邊界。
例如,在Bitronix Transaction Manager的情況下,宣告事務邊界跨越許多數據庫Connection可以通過下面的代碼來完成:
PoolingDataSource derbyDataSource1 = new PoolingDataSource();
derbyDataSource1.setClassName("org.apache.derby.jdbc.EmbeddedXADataSource");
derbyDataSource1.setUniqueName("derby1");
derbyDataSource1.getDriverProperties().setProperty("databaseName", "database1");
derbyDataSource1.init();
PoolingDataSource derbyDataSource2= new PoolingDataSource();
derbyDataSource2.setClassName("org.apache.derby.jdbc.EmbeddedXADataSource");
derbyDataSource2.setUniqueName("derby2");
derbyDataSource2.getDriverProperties().setProperty("databaseName", "database2");
derbyDataSource2.init();
BitronixTransactionManager btm = TransactionManagerServices.getTransactionManager();
btm.begin();
try {
Connection c1= derbyDataSource1.getConnection();
Connection c2= derbyDataSource2.getConnection();
/***Use c1 and c2 to execute statements again their corresponding DBs as usual**/
btm.commit();
} catch (SQLException ex) {
ex.printStackTrace();
btm.rollback();
}
OK,看到這個我最近的文章中http://stackoverflow.com/questions/13269718/how-to-maintain-acid-property-of-3-sequential-transaction-of-three-diffrenet-dat,你的例子在分佈式數據庫中運行良好。 –
也可以用JTA代替BitronixTransactionManager或者BitronixTransactionManager是更好的選擇 –
嗨arvin,BitronixTransactionManager是JTA的一個實現。 –