1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
| boolean logXaCommands = true; Connection conn1 = DriverManager.getConnection("jdbc:mysql://localhost:33060/d1", "root", "pass"); XAConnection xaConn1 = new MysqlXAConnection((JdbcConnection)conn1, logXaCommands); XAResource rm1 = xaConn1.getXAResource();
Connection conn2 = DriverManager.getConnection("jdbc:mysql://localhost:33060/d2", "root", "pass"); XAConnection xaConn2 = new MysqlXAConnection((JdbcConnection)conn2, logXaCommands); XAResource rm2 = xaConn2.getXAResource(); byte[] gtrid = "g12345".getBytes(); int formatId = 1; try { byte[] bqual1 = "b00001".getBytes(); Xid xid1 = new MysqlXid(gtrid, bqual1, formatId); rm1.start(xid1, XAResource.TMNOFLAGS); PreparedStatement ps1 = conn1.prepareStatement("insert into t1 values (10,'ab')"); ps1.execute(); rm1.end(xid1, XAResource.TMSUCCESS);
byte[] bqual2 = "b00002".getBytes(); Xid xid2 = new MysqlXid(gtrid, bqual2, formatId); rm2.start(xid2, XAResource.TMNOFLAGS); PreparedStatement ps2 = conn2.prepareStatement("insert into t2 values (20,'cd')"); ps2.execute(); rm2.end(xid2, XAResource.TMSUCCESS);
int rm1Prepare = rm1.prepare(xid1); int rm2Prepare = rm2.prepare(xid2); boolean onePhase = false; if (rm1Prepare == XAResource.XA_OK && rm2Prepare == XAResource.XA_OK) { rm1.commit(xid1, onePhase); rm2.commit(xid2, onePhase); } else { rm1.rollback(xid1); rm1.rollback(xid2); } } catch (XAException e) { e.printStackTrace(); }
|