TX その19 ちょっとした進展
エミュレーションコード
public void testTxManager() throws Exception { String url = "jdbc:mysql://localhost:3306/test"; Class.forName("com.mysql.jdbc.Driver").newInstance(); Connection conn = (com.mysql.jdbc.Connection) DriverManager.getConnection(url); XAResource xaResource = new MysqlXAConnection(conn); Connection conn2 = (com.mysql.jdbc.Connection) DriverManager.getConnection(url); XAResource xaResource2 = new MysqlXAConnection(conn2); TxManager tm = TxManager.getInstance(); tm.begin(); conn.setAutoCommit(false); conn2.setAutoCommit(false); Transaction tx = tm.getTransaction(); tx.enlistResource(xaResource); tx.enlistResource(xaResource2); tm.commit(); conn.close(); conn2.close(); }
このコードを実行した際のMySQLのGeneral log
11 Connect @localhost as anonymous on test 11 Query SET NAMES latin1 11 Query SET character_set_results = NULL 11 Query SHOW VARIABLES 11 Query SHOW COLLATION 11 Query SET autocommit=1 12 Connect @localhost as anonymous on test 12 Query SET NAMES latin1 12 Query SET character_set_results = NULL 12 Query SHOW VARIABLES 12 Query SHOW COLLATION 12 Query SET autocommit=1 11 Query SET autocommit=0 12 Query SET autocommit=0 11 Query XA START 0x6d697234322f31,0x31,0x101 12 Query XA START 0x6d697234322f31,0x32,0x101 11 Query XA END 0x6d697234322f31,0x31,0x101 12 Query XA END 0x6d697234322f31,0x32,0x101 11 Query XA PREPARE 0x6d697234322f31,0x31,0x101 12 Query XA PREPARE 0x6d697234322f31,0x32,0x101 11 Query XA COMMIT 0x6d697234322f31,0x31,0x101 12 Query XA COMMIT 0x6d697234322f31,0x32,0x101 11 Query rollback 11 Quit 12 Query rollback 12 Quit
conn2とかxaResource2とかをコメントアウトした場合,つまりXAResourceが1つである場合には,PREPAREを行わずにXA COMMIT...ONE PHASEとなる.
5 Connect @localhost as anonymous on test 5 Query SET NAMES latin1 5 Query SET character_set_results = NULL 5 Query SHOW VARIABLES 5 Query SHOW COLLATION 5 Query SET autocommit=1 5 Query XA START 0x6d697234322f31,0x31,0x101 5 Query XA END 0x6d697234322f31,0x31,0x101 5 Query XA COMMIT 0x6d697234322f31,0x31,0x101 ONE PHASE 5 Quit
以前の謎が解けたね〜.