Search in sources :

Example 1 with MysqlXAConnection

use of com.mysql.cj.jdbc.MysqlXAConnection in project JavaSegundasQuintas by ecteruel.

the class ConnectionRegressionTest method testBug62452.

/**
 * Tests fix for BUG#62452 - NPE thrown in JDBC4MySQLPooledException when statement is closed.
 *
 * @throws Exception
 */
@Test
public void testBug62452() throws Exception {
    PooledConnection con = null;
    MysqlConnectionPoolDataSource pds = new MysqlConnectionPoolDataSource();
    pds.setUrl(dbUrl);
    pds.getStringProperty(PropertyKey.sslMode.getKeyName()).setValue("DISABLED");
    pds.getBooleanProperty(PropertyKey.allowPublicKeyRetrieval.getKeyName()).setValue(true);
    con = pds.getPooledConnection();
    assertTrue(con instanceof MysqlPooledConnection);
    testBug62452WithConnection(con);
    MysqlXADataSource xads = new MysqlXADataSource();
    xads.setUrl(dbUrl);
    xads.getStringProperty(PropertyKey.sslMode.getKeyName()).setValue("DISABLED");
    xads.getBooleanProperty(PropertyKey.allowPublicKeyRetrieval.getKeyName()).setValue(true);
    xads.getProperty(PropertyKey.pinGlobalTxToPhysicalConnection).setValue(false);
    con = xads.getXAConnection();
    assertTrue(con instanceof MysqlXAConnection);
    testBug62452WithConnection(con);
    xads.getProperty(PropertyKey.pinGlobalTxToPhysicalConnection).setValue(true);
    con = xads.getXAConnection();
    assertTrue(con instanceof SuspendableXAConnection);
    testBug62452WithConnection(con);
}
Also used : MysqlPooledConnection(com.mysql.cj.jdbc.MysqlPooledConnection) PooledConnection(javax.sql.PooledConnection) MysqlXADataSource(com.mysql.cj.jdbc.MysqlXADataSource) SuspendableXAConnection(com.mysql.cj.jdbc.SuspendableXAConnection) MysqlXAConnection(com.mysql.cj.jdbc.MysqlXAConnection) MysqlConnectionPoolDataSource(com.mysql.cj.jdbc.MysqlConnectionPoolDataSource) MysqlPooledConnection(com.mysql.cj.jdbc.MysqlPooledConnection) Test(org.junit.jupiter.api.Test)

Example 2 with MysqlXAConnection

use of com.mysql.cj.jdbc.MysqlXAConnection in project ABC by RuiPinto96274.

the class ConnectionRegressionTest method testBug62452.

/**
 * Tests fix for BUG#62452 - NPE thrown in JDBC4MySQLPooledException when statement is closed.
 *
 * @throws Exception
 */
@Test
public void testBug62452() throws Exception {
    PooledConnection con = null;
    MysqlConnectionPoolDataSource pds = new MysqlConnectionPoolDataSource();
    pds.setUrl(dbUrl);
    pds.getStringProperty(PropertyKey.sslMode.getKeyName()).setValue("DISABLED");
    pds.getBooleanProperty(PropertyKey.allowPublicKeyRetrieval.getKeyName()).setValue(true);
    con = pds.getPooledConnection();
    assertTrue(con instanceof MysqlPooledConnection);
    testBug62452WithConnection(con);
    MysqlXADataSource xads = new MysqlXADataSource();
    xads.setUrl(dbUrl);
    xads.getStringProperty(PropertyKey.sslMode.getKeyName()).setValue("DISABLED");
    xads.getBooleanProperty(PropertyKey.allowPublicKeyRetrieval.getKeyName()).setValue(true);
    xads.getProperty(PropertyKey.pinGlobalTxToPhysicalConnection).setValue(false);
    con = xads.getXAConnection();
    assertTrue(con instanceof MysqlXAConnection);
    testBug62452WithConnection(con);
    xads.getProperty(PropertyKey.pinGlobalTxToPhysicalConnection).setValue(true);
    con = xads.getXAConnection();
    assertTrue(con instanceof SuspendableXAConnection);
    testBug62452WithConnection(con);
}
Also used : MysqlPooledConnection(com.mysql.cj.jdbc.MysqlPooledConnection) PooledConnection(javax.sql.PooledConnection) MysqlXADataSource(com.mysql.cj.jdbc.MysqlXADataSource) SuspendableXAConnection(com.mysql.cj.jdbc.SuspendableXAConnection) MysqlXAConnection(com.mysql.cj.jdbc.MysqlXAConnection) MysqlConnectionPoolDataSource(com.mysql.cj.jdbc.MysqlConnectionPoolDataSource) MysqlPooledConnection(com.mysql.cj.jdbc.MysqlPooledConnection) Test(org.junit.jupiter.api.Test)

Example 3 with MysqlXAConnection

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

the class MysqlXANativeTest method test.

public void test(boolean rollback) throws Exception {
    // 这个XAResource其实你可以认为是RM(Resource Manager)的一个代码中的对象实例
    XAConnection xaConnection1 = null;
    XAConnection xaConnection2 = null;
    try (Connection connection1 = DriverManager.getConnection(url("ahaodb"), "root", "root");
        Connection connection2 = DriverManager.getConnection(url("ahaodb"), "root", "root")) {
        // 这个XAResource其实你可以认为是RM(Resource Manager)的一个代码中的对象实例
        xaConnection1 = new MysqlXAConnection((JdbcConnection) connection1, true);
        XAResource xaResource1 = xaConnection1.getXAResource();
        // 这个XAResource其实你可以认为是RM(Resource Manager)的一个代码中的对象实例
        xaConnection2 = new MysqlXAConnection((JdbcConnection) connection2, true);
        XAResource xaResource2 = xaConnection2.getXAResource();
        // 生成xid, xid是子事务的唯一标识, 通过START和END两个操作, 定义子事务要执行的SQL语句
        Xid xid1 = this.prepare(xaResource1, "bqual1", connection1.prepareStatement(SQL1));
        Xid xid2 = this.prepare(xaResource2, "bqual2", connection2.prepareStatement(SQL2));
        // 2PC的阶段一: 向两个库都发送prepare消息,执行事务中的SQL语句,但是不提交
        int prepareResult1 = xaResource1.prepare(xid1);
        int prepareResult2 = xaResource2.prepare(xid2);
        if (rollback) {
            xaResource1.rollback(xid1);
            xaResource2.rollback(xid2);
            return;
        }
        // 如果两个库对prepare都返回ok, 那么就全部commit, 对每个库都发送commit消息, 完成自己本地事务的提交
        if (prepareResult1 == XAResource.XA_OK && prepareResult2 == XAResource.XA_OK) {
            xaResource1.commit(xid1, false);
            xaResource2.commit(xid2, false);
        } else // 如果不是所有库都对prepare返回ok,那么就全部rollback
        {
            xaResource1.rollback(xid1);
            xaResource2.rollback(xid2);
        }
    } finally {
        if (xaConnection1 != null)
            xaConnection1.close();
        if (xaConnection2 != null)
            xaConnection2.close();
    }
}
Also used : XAResource(javax.transaction.xa.XAResource) MysqlXid(com.mysql.cj.jdbc.MysqlXid) Xid(javax.transaction.xa.Xid) MysqlXAConnection(com.mysql.cj.jdbc.MysqlXAConnection) Connection(java.sql.Connection) XAConnection(javax.sql.XAConnection) JdbcConnection(com.mysql.cj.jdbc.JdbcConnection) MysqlXAConnection(com.mysql.cj.jdbc.MysqlXAConnection) JdbcConnection(com.mysql.cj.jdbc.JdbcConnection) XAConnection(javax.sql.XAConnection) MysqlXAConnection(com.mysql.cj.jdbc.MysqlXAConnection)

Example 4 with MysqlXAConnection

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

the class ConnectionRegressionTest method testBug62452.

/**
 * Tests fix for BUG#62452 - NPE thrown in JDBC4MySQLPooledException when statement is closed.
 *
 * @throws Exception
 */
@Test
public void testBug62452() throws Exception {
    PooledConnection con = null;
    MysqlConnectionPoolDataSource pds = new MysqlConnectionPoolDataSource();
    pds.setUrl(dbUrl);
    pds.getStringProperty(PropertyKey.sslMode.getKeyName()).setValue("DISABLED");
    pds.getBooleanProperty(PropertyKey.allowPublicKeyRetrieval.getKeyName()).setValue(true);
    con = pds.getPooledConnection();
    assertTrue(con instanceof MysqlPooledConnection);
    testBug62452WithConnection(con);
    MysqlXADataSource xads = new MysqlXADataSource();
    xads.setUrl(dbUrl);
    xads.getStringProperty(PropertyKey.sslMode.getKeyName()).setValue("DISABLED");
    xads.getBooleanProperty(PropertyKey.allowPublicKeyRetrieval.getKeyName()).setValue(true);
    xads.getProperty(PropertyKey.pinGlobalTxToPhysicalConnection).setValue(false);
    con = xads.getXAConnection();
    assertTrue(con instanceof MysqlXAConnection);
    testBug62452WithConnection(con);
    xads.getProperty(PropertyKey.pinGlobalTxToPhysicalConnection).setValue(true);
    con = xads.getXAConnection();
    assertTrue(con instanceof SuspendableXAConnection);
    testBug62452WithConnection(con);
}
Also used : MysqlPooledConnection(com.mysql.cj.jdbc.MysqlPooledConnection) PooledConnection(javax.sql.PooledConnection) MysqlXADataSource(com.mysql.cj.jdbc.MysqlXADataSource) SuspendableXAConnection(com.mysql.cj.jdbc.SuspendableXAConnection) MysqlXAConnection(com.mysql.cj.jdbc.MysqlXAConnection) MysqlConnectionPoolDataSource(com.mysql.cj.jdbc.MysqlConnectionPoolDataSource) MysqlPooledConnection(com.mysql.cj.jdbc.MysqlPooledConnection) Test(org.junit.jupiter.api.Test)

Example 5 with MysqlXAConnection

use of com.mysql.cj.jdbc.MysqlXAConnection in project note by kebukeYi.

the class XATransaction method main.

public static void main(String[] args) {
    try {
        // ========================== START ==========================================//
        // 是否开启日志
        boolean isLogging = true;
        // 获取rm
        Connection conn1 = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/bank1?serverTimezone=UTC", "root", "123456");
        XAResource accountResource = new MysqlXAConnection((com.mysql.cj.jdbc.JdbcConnection) conn1, isLogging).getXAResource();
        Connection conn2 = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/bank2?serverTimezone=UTC", "root", "123456");
        XAResource redBagResource = new MysqlXAConnection((com.mysql.cj.jdbc.JdbcConnection) conn2, isLogging).getXAResource();
        // 开始XA事物
        // 全局事物
        byte[] globalId = UUID.randomUUID().toString().getBytes();
        // 标识
        int formatId = 1;
        // 分支事物一
        Xid xid1 = new MysqlXid(globalId, UUID.randomUUID().toString().getBytes(), formatId);
        // 分支事物二
        Xid xid2 = new MysqlXid(globalId, UUID.randomUUID().toString().getBytes(), formatId);
        try {
            // 分支事物1开始,此时状态:ACTIVE
            accountResource.start(xid1, XAResource.TMNOFLAGS);
            // 模拟业务
            conn1.prepareStatement("update account_info set account_balance=account_balance-10 where account_no=1").execute();
            accountResource.end(xid1, XAResource.TMSUCCESS);
            // 分支事物1 此时状态 IDLE
            // 分支事物2开始
            redBagResource.start(xid2, XAResource.TMNOFLAGS);
            // 模拟业务
            conn2.prepareStatement("update account_info set account_balance=account_balance+10 where account_no=2").execute();
            // 存在异常
            int i = 10 / 0;
            redBagResource.end(xid2, XAResource.TMSUCCESS);
            // 分支事物2 此时状态 IDLE
            // 第一阶段:准备提交
            int rm1_pre = accountResource.prepare(xid1);
            int rm2_pre = redBagResource.prepare(xid2);
            // XA事物 此时状态 PREPARED
            // 第二阶段: TM根据根据第一阶段的情况决定提交还是回滚
            // TM判断有两个事物分支,所以不能优化为一阶段提交
            boolean flag = false;
            if (rm1_pre == XAResource.XA_OK && rm2_pre == XAResource.XA_OK) {
                accountResource.commit(xid1, flag);
                redBagResource.commit(xid2, flag);
            } else {
                accountResource.rollback(xid1);
                redBagResource.rollback(xid2);
            }
        // ================= END =========================//
        } catch (Exception e) {
            // 出现异常,也要回滚
            try {
                accountResource.rollback(xid1);
                redBagResource.rollback(xid2);
                e.printStackTrace();
            } catch (XAException xaException) {
                xaException.printStackTrace();
            }
        }
    } catch (SQLException throwables) {
        throwables.printStackTrace();
    }
}
Also used : XAException(javax.transaction.xa.XAException) MysqlXid(com.mysql.cj.jdbc.MysqlXid) SQLException(java.sql.SQLException) Connection(java.sql.Connection) MysqlXAConnection(com.mysql.cj.jdbc.MysqlXAConnection) SQLException(java.sql.SQLException) XAException(javax.transaction.xa.XAException) XAResource(javax.transaction.xa.XAResource) MysqlXid(com.mysql.cj.jdbc.MysqlXid) Xid(javax.transaction.xa.Xid) MysqlXAConnection(com.mysql.cj.jdbc.MysqlXAConnection)

Aggregations

MysqlXAConnection (com.mysql.cj.jdbc.MysqlXAConnection)5 MysqlConnectionPoolDataSource (com.mysql.cj.jdbc.MysqlConnectionPoolDataSource)3 MysqlPooledConnection (com.mysql.cj.jdbc.MysqlPooledConnection)3 MysqlXADataSource (com.mysql.cj.jdbc.MysqlXADataSource)3 SuspendableXAConnection (com.mysql.cj.jdbc.SuspendableXAConnection)3 PooledConnection (javax.sql.PooledConnection)3 Test (org.junit.jupiter.api.Test)3 MysqlXid (com.mysql.cj.jdbc.MysqlXid)2 Connection (java.sql.Connection)2 XAResource (javax.transaction.xa.XAResource)2 Xid (javax.transaction.xa.Xid)2 JdbcConnection (com.mysql.cj.jdbc.JdbcConnection)1 SQLException (java.sql.SQLException)1 XAConnection (javax.sql.XAConnection)1 XAException (javax.transaction.xa.XAException)1