Search in sources :

Example 1 with MysqlXid

use of com.mysql.cj.jdbc.MysqlXid in project project by Ahaochan.

the class MysqlXANativeTest method prepare.

private Xid prepare(XAResource xaResource, String bqualStr, PreparedStatement statement) throws Exception {
    // gtrid: 全局事务ID
    // bqual: 分支修饰词标识符(branch qualifier)
    // formatID: 是一个无符号整数,用于标记gtrid和bqual值的格式,默认为1,长度不超过64.
    byte[] gtrid = "g12345".getBytes();
    byte[] bqual = bqualStr.getBytes();
    int formatId = 1;
    // 这个xid代表了一个子事务
    Xid xid = new MysqlXid(gtrid, bqual, formatId);
    // 通过START和END两个操作, 定义子事务要执行的SQL语句
    // 但是这里的SQL绝对不会执行的, 只是说先定义好, 在子事务中要执行哪些SQL
    xaResource.start(xid, XAResource.TMNOFLAGS);
    statement.execute();
    xaResource.end(xid, XAResource.TMSUCCESS);
    return xid;
}
Also used : MysqlXid(com.mysql.cj.jdbc.MysqlXid) Xid(javax.transaction.xa.Xid) MysqlXid(com.mysql.cj.jdbc.MysqlXid)

Example 2 with MysqlXid

use of com.mysql.cj.jdbc.MysqlXid in project aws-mysql-jdbc by awslabs.

the class ConnectionRegressionTest method testBug69506.

/**
 * Tests fix for BUG#69506 - XAER_DUPID error code is not returned when a duplicate XID is offered in Java.
 *
 * @throws Exception
 */
@Test
public void testBug69506() throws Exception {
    MysqlXADataSource dataSource = new MysqlXADataSource();
    dataSource.setUrl(dbUrl);
    dataSource.getStringProperty(PropertyKey.sslMode.getKeyName()).setValue("DISABLED");
    dataSource.getBooleanProperty(PropertyKey.allowPublicKeyRetrieval.getKeyName()).setValue(true);
    XAConnection testXAConn1 = dataSource.getXAConnection();
    XAConnection testXAConn2 = dataSource.getXAConnection();
    Xid duplicateXID = new MysqlXid("1".getBytes(), "1".getBytes(), 1);
    testXAConn1.getXAResource().start(duplicateXID, 0);
    try {
        testXAConn2.getXAResource().start(duplicateXID, 0);
        fail("XAException was expected.");
    } catch (XAException e) {
        assertEquals(XAException.XAER_DUPID, e.errorCode, "Wrong error code retured for duplicated XID.");
    }
}
Also used : MysqlXid(com.mysql.cj.jdbc.MysqlXid) Xid(javax.transaction.xa.Xid) MysqlXADataSource(com.mysql.cj.jdbc.MysqlXADataSource) XAException(javax.transaction.xa.XAException) MysqlXid(com.mysql.cj.jdbc.MysqlXid) SuspendableXAConnection(com.mysql.cj.jdbc.SuspendableXAConnection) XAConnection(javax.sql.XAConnection) MysqlXAConnection(com.mysql.cj.jdbc.MysqlXAConnection) Test(org.junit.jupiter.api.Test)

Example 3 with MysqlXid

use of com.mysql.cj.jdbc.MysqlXid in project aws-mysql-jdbc by awslabs.

the class XATest method createXid.

private Xid createXid() throws IOException {
    ByteArrayOutputStream gtridOut = new ByteArrayOutputStream();
    DataOutputStream dataOut = new DataOutputStream(gtridOut);
    new UID().write(dataOut);
    final byte[] gtrid = gtridOut.toByteArray();
    ByteArrayOutputStream bqualOut = new ByteArrayOutputStream();
    dataOut = new DataOutputStream(bqualOut);
    new UID().write(dataOut);
    final byte[] bqual = bqualOut.toByteArray();
    Xid xid = new MysqlXid(gtrid, bqual, 3306);
    return xid;
}
Also used : UID(java.rmi.server.UID) MysqlXid(com.mysql.cj.jdbc.MysqlXid) Xid(javax.transaction.xa.Xid) MysqlXid(com.mysql.cj.jdbc.MysqlXid) DataOutputStream(java.io.DataOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 4 with MysqlXid

use of com.mysql.cj.jdbc.MysqlXid in project aws-mysql-jdbc by awslabs.

the class ConnectionRegressionTest method testBug46925.

@Test
public void testBug46925() throws Exception {
    MysqlXADataSource xads1 = new MysqlXADataSource();
    MysqlXADataSource xads2 = new MysqlXADataSource();
    Xid txid = new MysqlXid(new byte[] { 0x1 }, new byte[] { 0xf }, 3306);
    xads1.getStringProperty(PropertyKey.sslMode.getKeyName()).setValue("DISABLED");
    xads1.getBooleanProperty(PropertyKey.allowPublicKeyRetrieval.getKeyName()).setValue(true);
    xads1.getProperty(PropertyKey.pinGlobalTxToPhysicalConnection).setValue(true);
    xads1.setUrl(dbUrl);
    xads2.getStringProperty(PropertyKey.sslMode.getKeyName()).setValue("DISABLED");
    xads2.getBooleanProperty(PropertyKey.allowPublicKeyRetrieval.getKeyName()).setValue(true);
    xads2.getProperty(PropertyKey.pinGlobalTxToPhysicalConnection).setValue(true);
    xads2.setUrl(dbUrl);
    XAConnection c1 = xads1.getXAConnection();
    assertTrue(c1 instanceof SuspendableXAConnection);
    // start a transaction on one connection
    c1.getXAResource().start(txid, XAResource.TMNOFLAGS);
    c1.getXAResource().end(txid, XAResource.TMSUCCESS);
    XAConnection c2 = xads2.getXAConnection();
    assertTrue(c2 instanceof SuspendableXAConnection);
    // prepare on another one. Since we are using a "pinned" connection we should have the same "currentXAConnection" for both SuspendableXAConnection
    // this will fail without the fix.
    c2.getXAResource().prepare(txid);
    c2.getXAResource().commit(txid, false);
}
Also used : MysqlXid(com.mysql.cj.jdbc.MysqlXid) Xid(javax.transaction.xa.Xid) MysqlXADataSource(com.mysql.cj.jdbc.MysqlXADataSource) MysqlXid(com.mysql.cj.jdbc.MysqlXid) SuspendableXAConnection(com.mysql.cj.jdbc.SuspendableXAConnection) SuspendableXAConnection(com.mysql.cj.jdbc.SuspendableXAConnection) XAConnection(javax.sql.XAConnection) MysqlXAConnection(com.mysql.cj.jdbc.MysqlXAConnection) Test(org.junit.jupiter.api.Test)

Example 5 with MysqlXid

use of com.mysql.cj.jdbc.MysqlXid in project aws-mysql-jdbc by awslabs.

the class ConnectionRegressionTest method testBug67803.

/**
 * Tests fix for BUG#67803 - XA commands sent twice to MySQL server
 *
 * @throws Exception
 */
@Test
public void testBug67803() throws Exception {
    MysqlXADataSource dataSource = new MysqlXADataSource();
    dataSource.setUrl(dbUrl);
    dataSource.getStringProperty(PropertyKey.sslMode.getKeyName()).setValue("DISABLED");
    dataSource.getBooleanProperty(PropertyKey.allowPublicKeyRetrieval.getKeyName()).setValue(true);
    dataSource.getProperty(PropertyKey.useCursorFetch).setValue(true);
    dataSource.getProperty(PropertyKey.defaultFetchSize).setValue(50);
    dataSource.getProperty(PropertyKey.useServerPrepStmts).setValue(true);
    dataSource.getProperty(PropertyKey.exceptionInterceptors).setValue("testsuite.regression.ConnectionRegressionTest$TestBug67803ExceptionInterceptor");
    XAConnection testXAConn1 = dataSource.getXAConnection();
    testXAConn1.getXAResource().start(new MysqlXid("2".getBytes(), "2".getBytes(), 1), 0);
}
Also used : MysqlXADataSource(com.mysql.cj.jdbc.MysqlXADataSource) MysqlXid(com.mysql.cj.jdbc.MysqlXid) SuspendableXAConnection(com.mysql.cj.jdbc.SuspendableXAConnection) XAConnection(javax.sql.XAConnection) MysqlXAConnection(com.mysql.cj.jdbc.MysqlXAConnection) Test(org.junit.jupiter.api.Test)

Aggregations

MysqlXid (com.mysql.cj.jdbc.MysqlXid)8 Xid (javax.transaction.xa.Xid)7 MysqlXAConnection (com.mysql.cj.jdbc.MysqlXAConnection)4 MysqlXADataSource (com.mysql.cj.jdbc.MysqlXADataSource)4 XAConnection (javax.sql.XAConnection)4 Test (org.junit.jupiter.api.Test)4 SuspendableXAConnection (com.mysql.cj.jdbc.SuspendableXAConnection)3 XAException (javax.transaction.xa.XAException)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 DataOutputStream (java.io.DataOutputStream)2 UID (java.rmi.server.UID)2 Connection (java.sql.Connection)2 SQLException (java.sql.SQLException)2 XAResource (javax.transaction.xa.XAResource)2 MysqlConnection (com.mysql.cj.MysqlConnection)1 JdbcConnection (com.mysql.cj.jdbc.JdbcConnection)1 PooledConnection (javax.sql.PooledConnection)1