Search in sources :

Example 1 with DataSourceProxyXA

use of io.seata.rm.datasource.xa.DataSourceProxyXA in project seata by seata.

the class XAModeTest2 method doTestXAModeNormalCasePhase2.

private void doTestXAModeNormalCasePhase2(boolean globalCommit, String mockXid, Long mockBranchId) throws Throwable {
    // init DataSource: helper
    DruidDataSource helperDS = createNewDruidDataSource();
    Connection helperConn = null;
    Statement helperStat = null;
    ResultSet helperRes = null;
    // init RM
    initRM();
    AbstractDataSourceProxyXA dataSourceProxyXA = null;
    if (nativeXA) {
        // init XADataSource runnerXA
        XADataSource runnerXADS = createNewNativeXADataSource();
        dataSourceProxyXA = new DataSourceProxyXANative(runnerXADS);
    } else {
        // init DataSource: runner
        DruidDataSource runnerDS = createNewDruidDataSource();
        dataSourceProxyXA = new DataSourceProxyXA(runnerDS);
    }
    // Global Tx Phase 2:
    if (globalCommit) {
        DefaultResourceManager.get().branchCommit(dataSourceProxyXA.getBranchType(), mockXid, mockBranchId, dataSourceProxyXA.getResourceId(), null);
        // have a check
        helperConn = helperDS.getConnection();
        helperStat = helperConn.createStatement();
        helperRes = helperStat.executeQuery("select * from test where id = " + testRecordId);
        // should see the test record now
        Assertions.assertTrue(helperRes.next());
        Assertions.assertEquals(helperRes.getInt(1), testRecordId);
        Assertions.assertEquals(helperRes.getString(2), testRecordName);
        helperRes.close();
        helperStat.close();
        helperConn.close();
    } else {
        DefaultResourceManager.get().branchRollback(dataSourceProxyXA.getBranchType(), mockXid, mockBranchId, dataSourceProxyXA.getResourceId(), null);
        // have a check
        helperConn = helperDS.getConnection();
        helperStat = helperConn.createStatement();
        helperRes = helperStat.executeQuery("select * from test where id = " + testRecordId);
        // should NOT see the test record now
        Assertions.assertFalse(helperRes.next());
        helperRes.close();
        helperStat.close();
        helperConn.close();
    }
    System.out.println("Phase2 looks good!");
}
Also used : AbstractDataSourceProxyXA(io.seata.rm.datasource.xa.AbstractDataSourceProxyXA) DataSourceProxyXA(io.seata.rm.datasource.xa.DataSourceProxyXA) XADataSource(javax.sql.XADataSource) MysqlXADataSource(com.mysql.jdbc.jdbc2.optional.MysqlXADataSource) DruidXADataSource(com.alibaba.druid.pool.xa.DruidXADataSource) PGXADataSource(org.postgresql.xa.PGXADataSource) DataSourceProxyXANative(io.seata.rm.datasource.xa.DataSourceProxyXANative) Statement(java.sql.Statement) Connection(java.sql.Connection) XAConnection(javax.sql.XAConnection) ResultSet(java.sql.ResultSet) DruidDataSource(com.alibaba.druid.pool.DruidDataSource) AbstractDataSourceProxyXA(io.seata.rm.datasource.xa.AbstractDataSourceProxyXA)

Example 2 with DataSourceProxyXA

use of io.seata.rm.datasource.xa.DataSourceProxyXA in project Mycat2 by MyCATApache.

the class SeataXADatasourceProvider method createDataSource.

@Override
public JdbcDataSource createDataSource(DatasourceConfig config) {
    String username = config.getUser();
    String password = config.getPassword();
    String url = Objects.requireNonNull(config.getUrl());
    String dbType = config.getDbType();
    int maxRetryCount = config.getMaxRetryCount();
    List<String> initSQLs = config.getInitSqls();
    int maxCon = config.getMaxCon();
    int minCon = config.getMinCon();
    DruidXADataSource datasource = new DruidXADataSource();
    datasource.setPassword(password);
    datasource.setUsername(username);
    datasource.setUrl(url);
    datasource.setMaxWait(TimeUnit.SECONDS.toMillis(60));
    datasource.setMaxActive(maxCon);
    datasource.setMinIdle(minCon);
    datasource.setKeepAlive(true);
    datasource.setTestOnReturn(true);
    datasource.setTestOnBorrow(true);
    datasource.setValidationQuery("select 'x'");
    datasource.setTestWhileIdle(true);
    if (maxRetryCount > 0) {
        datasource.setConnectionErrorRetryAttempts(maxRetryCount);
    }
    if (dbType != null) {
        datasource.setDbType(dbType);
    }
    if (initSQLs != null) {
        datasource.setConnectionInitSqls(initSQLs);
    }
    return new JdbcDataSource(config, new DataSourceProxyXA(datasource));
}
Also used : DataSourceProxyXA(io.seata.rm.datasource.xa.DataSourceProxyXA) JdbcDataSource(io.mycat.datasource.jdbc.datasource.JdbcDataSource) DruidXADataSource(com.alibaba.druid.pool.xa.DruidXADataSource)

Example 3 with DataSourceProxyXA

use of io.seata.rm.datasource.xa.DataSourceProxyXA in project seata by seata.

the class XAModeTest2 method doTestXAModeNormalCasePhase1.

private void doTestXAModeNormalCasePhase1(String mockXid, Long mockBranchId) throws Throwable {
    // init DataSource: helper
    DruidDataSource helperDS = createNewDruidDataSource();
    Connection helperConn = null;
    Statement helperStat = null;
    ResultSet helperRes = null;
    // init RM
    initRM();
    AbstractDataSourceProxyXA dataSourceProxyXA = null;
    if (nativeXA) {
        // init XADataSource runnerXA
        XADataSource runnerXADS = createNewNativeXADataSource();
        dataSourceProxyXA = new DataSourceProxyXANative(runnerXADS);
    } else {
        // init DataSource: runner
        DruidDataSource runnerDS = createNewDruidDataSource();
        dataSourceProxyXA = new DataSourceProxyXA(runnerDS);
    }
    // Global Tx Phase 1:
    RootContext.bind(mockXid);
    Connection testConn = dataSourceProxyXA.getConnection();
    Statement testStat = testConn.createStatement();
    // >>> insert the test record with XA mode
    testStat.execute("insert into test(id, name) values(" + testRecordId + ", '" + testRecordName + "')");
    // >>> close the statement and connection
    testStat.close();
    testConn.close();
    RootContext.unbind();
    // have a check
    helperConn = helperDS.getConnection();
    helperStat = helperConn.createStatement();
    helperRes = helperStat.executeQuery("select * from test where id = " + testRecordId);
    // should NOT see the record(id=888) now
    Assertions.assertFalse(helperRes.next());
    helperRes.close();
    helperStat.close();
    helperConn.close();
    if (JdbcUtils.MYSQL.equals(dbType)) {
        XAXid xaXid = XAXidBuilder.build(mockXid, mockBranchId);
        dataSourceProxyXA.forceClosePhysicalConnection(xaXid);
    }
    System.out.println("Phase1 looks good!");
}
Also used : AbstractDataSourceProxyXA(io.seata.rm.datasource.xa.AbstractDataSourceProxyXA) DataSourceProxyXA(io.seata.rm.datasource.xa.DataSourceProxyXA) XADataSource(javax.sql.XADataSource) MysqlXADataSource(com.mysql.jdbc.jdbc2.optional.MysqlXADataSource) DruidXADataSource(com.alibaba.druid.pool.xa.DruidXADataSource) PGXADataSource(org.postgresql.xa.PGXADataSource) DataSourceProxyXANative(io.seata.rm.datasource.xa.DataSourceProxyXANative) Statement(java.sql.Statement) Connection(java.sql.Connection) XAConnection(javax.sql.XAConnection) ResultSet(java.sql.ResultSet) XAXid(io.seata.rm.datasource.xa.XAXid) DruidDataSource(com.alibaba.druid.pool.DruidDataSource) AbstractDataSourceProxyXA(io.seata.rm.datasource.xa.AbstractDataSourceProxyXA)

Example 4 with DataSourceProxyXA

use of io.seata.rm.datasource.xa.DataSourceProxyXA in project seata by seata.

the class XAModeTest2 method createDataSourceProxyXA.

private DataSourceProxyXA createDataSourceProxyXA() throws Throwable {
    DataSource originalDS = createNewDruidDataSource();
    DataSourceProxyXA dataSourceProxyXA = new DataSourceProxyXA(originalDS);
    return dataSourceProxyXA;
}
Also used : AbstractDataSourceProxyXA(io.seata.rm.datasource.xa.AbstractDataSourceProxyXA) DataSourceProxyXA(io.seata.rm.datasource.xa.DataSourceProxyXA) XADataSource(javax.sql.XADataSource) MysqlXADataSource(com.mysql.jdbc.jdbc2.optional.MysqlXADataSource) DataSource(javax.sql.DataSource) DruidXADataSource(com.alibaba.druid.pool.xa.DruidXADataSource) DruidDataSource(com.alibaba.druid.pool.DruidDataSource) PGXADataSource(org.postgresql.xa.PGXADataSource)

Example 5 with DataSourceProxyXA

use of io.seata.rm.datasource.xa.DataSourceProxyXA in project seata by seata.

the class XAModeTest2 method doTestXAModeNormalCaseAllInOne.

private void doTestXAModeNormalCaseAllInOne(String mockXid, Long mockBranchId) throws Throwable {
    // init DataSource: helper
    DruidDataSource helperDS = createNewDruidDataSource();
    Connection helperConn = null;
    Statement helperStat = null;
    ResultSet helperRes = null;
    // init RM
    initRM();
    AbstractDataSourceProxyXA dataSourceProxyXA = null;
    if (nativeXA) {
        // init XADataSource runnerXA
        XADataSource runnerXADS = createNewNativeXADataSource();
        dataSourceProxyXA = new DataSourceProxyXANative(runnerXADS);
    } else {
        // init DataSource: runner
        DruidDataSource runnerDS = createNewDruidDataSource();
        dataSourceProxyXA = new DataSourceProxyXA(runnerDS);
    }
    // Global Tx Phase 1:
    RootContext.bind(mockXid);
    Connection testConn = dataSourceProxyXA.getConnection();
    Statement testStat = testConn.createStatement();
    // >>> insert the test record with XA mode
    testStat.execute("insert into test(id, name) values(" + testRecordId + ", '" + testRecordName + "')");
    // >>> close the statement and connection
    testStat.close();
    testConn.close();
    RootContext.unbind();
    // have a check
    helperConn = helperDS.getConnection();
    helperStat = helperConn.createStatement();
    helperRes = helperStat.executeQuery("select * from test where id = " + testRecordId);
    // should NOT see the record(id=888) now
    Assertions.assertFalse(helperRes.next());
    helperRes.close();
    helperStat.close();
    helperConn.close();
    // Global Tx Phase 2: run phase 2 with the same runner DS
    DefaultResourceManager.get().branchCommit(dataSourceProxyXA.getBranchType(), mockXid, mockBranchId, dataSourceProxyXA.getResourceId(), null);
    // have a check
    helperConn = helperDS.getConnection();
    helperStat = helperConn.createStatement();
    helperRes = helperStat.executeQuery("select * from test where id = " + testRecordId);
    // should see the test record now
    Assertions.assertTrue(helperRes.next());
    Assertions.assertEquals(helperRes.getInt(1), testRecordId);
    Assertions.assertEquals(helperRes.getString(2), testRecordName);
    helperRes.close();
    helperStat.close();
    helperConn.close();
    System.out.println("All in one looks good!");
}
Also used : AbstractDataSourceProxyXA(io.seata.rm.datasource.xa.AbstractDataSourceProxyXA) DataSourceProxyXA(io.seata.rm.datasource.xa.DataSourceProxyXA) XADataSource(javax.sql.XADataSource) MysqlXADataSource(com.mysql.jdbc.jdbc2.optional.MysqlXADataSource) DruidXADataSource(com.alibaba.druid.pool.xa.DruidXADataSource) PGXADataSource(org.postgresql.xa.PGXADataSource) DataSourceProxyXANative(io.seata.rm.datasource.xa.DataSourceProxyXANative) Statement(java.sql.Statement) Connection(java.sql.Connection) XAConnection(javax.sql.XAConnection) ResultSet(java.sql.ResultSet) DruidDataSource(com.alibaba.druid.pool.DruidDataSource) AbstractDataSourceProxyXA(io.seata.rm.datasource.xa.AbstractDataSourceProxyXA)

Aggregations

DataSourceProxyXA (io.seata.rm.datasource.xa.DataSourceProxyXA)9 DruidDataSource (com.alibaba.druid.pool.DruidDataSource)6 DruidXADataSource (com.alibaba.druid.pool.xa.DruidXADataSource)5 MysqlXADataSource (com.mysql.jdbc.jdbc2.optional.MysqlXADataSource)4 AbstractDataSourceProxyXA (io.seata.rm.datasource.xa.AbstractDataSourceProxyXA)4 Connection (java.sql.Connection)4 XAConnection (javax.sql.XAConnection)4 XADataSource (javax.sql.XADataSource)4 PGXADataSource (org.postgresql.xa.PGXADataSource)4 DataSourceProxyXANative (io.seata.rm.datasource.xa.DataSourceProxyXANative)3 ResultSet (java.sql.ResultSet)3 Statement (java.sql.Statement)3 DataSource (javax.sql.DataSource)3 JdbcDataSource (io.mycat.datasource.jdbc.datasource.JdbcDataSource)2 Test (org.junit.jupiter.api.Test)2 ItemDataSource (com.baomidou.dynamic.datasource.ds.ItemDataSource)1 SeataMode (com.baomidou.dynamic.datasource.enums.SeataMode)1 MysqlXADataSource (com.mysql.cj.jdbc.MysqlXADataSource)1 JDBC4MySQLConnection (com.mysql.jdbc.JDBC4MySQLConnection)1 JDBC4ConnectionWrapper (com.mysql.jdbc.jdbc2.optional.JDBC4ConnectionWrapper)1