标签:
import com.mysql.jdbc.jdbc2.optional.MysqlXADataSource; import oracle.jdbc.xa.client.OracleXADataSource; public class Test { public static void main(String[] args) throws SQLException { OracleXADataSource oDS=new OracleXADataSource(); oDS.setURL("jdbc:oracle:thin:@localhost:1521:orcl"); oDS.setUser("scott"); oDS.setPassword("tiger"); MysqlXADataSource mDS=new MysqlXADataSource(); mDS.setURL("jdbc:mysql://localhost:3306/test"); mDS.setUser("root"); mDS.setPassword("tiger"); XAConnection oXACon=oDS.getXAConnection(); XAConnection mXACon=mDS.getXAConnection(); XAResource oXARes=oXACon.getXAResource(); XAResource mXARes=mXACon.getXAResource(); Connection oCon=oXACon.getConnection(); Connection mCon=mXACon.getConnection(); Statement oSta=oCon.createStatement(); Statement mSta=mCon.createStatement(); Xid xid1=new MyXid(0, new byte[]{3}, new byte[]{2}); Xid xid2=new MyXid(0,new byte[]{3},new byte[]{5}); try { oXARes.start(xid1, XAResource.TMNOFLAGS); oSta.execute("insert into test1 values(133,122)"); oXARes.end(xid1, XAResource.TMSUCCESS); mXARes.start(xid2, XAResource.TMNOFLAGS); mSta.execute("insert into student values(ddd,17)"); mXARes.end(xid2, XAResource.TMSUCCESS); int oPre=oXARes.prepare(xid1); int mPre=mXARes.prepare(xid2); if(oPre==XAResource.XA_OK&&mPre==XAResource.XA_OK){ oXARes.commit(xid1, false);//true为单阶段提交协议 mXARes.commit(xid2, false); } } catch (XAException e) { try { oXARes.rollback(xid1); mXARes.rollback(xid2); } catch (XAException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } e.printStackTrace(); } } }
标签:
原文地址:http://www.cnblogs.com/coolraw/p/4722154.html