use of io.seata.rm.datasource.xa.DataSourceProxyXANative in project seata by seata.
the class DataSourceProxyXANativeTest method testGetConnection.
@Test
public void testGetConnection() throws SQLException {
// Mock
Connection connection = Mockito.mock(Connection.class);
Mockito.when(connection.getAutoCommit()).thenReturn(true);
DatabaseMetaData metaData = Mockito.mock(DatabaseMetaData.class);
Mockito.when(metaData.getURL()).thenReturn("jdbc:mysql:xxx");
Mockito.when(connection.getMetaData()).thenReturn(metaData);
XAConnection xaConnection = Mockito.mock(XAConnection.class);
Mockito.when(xaConnection.getConnection()).thenReturn(connection);
XADataSource xaDataSource = Mockito.mock(XADataSource.class);
Mockito.when(xaDataSource.getXAConnection()).thenReturn(xaConnection);
DataSourceProxyXANative dataSourceProxyXANative = new DataSourceProxyXANative(xaDataSource);
Connection connFromDataSourceProxyXANative = dataSourceProxyXANative.getConnection();
Assertions.assertTrue(connFromDataSourceProxyXANative instanceof ConnectionProxyXA);
XAConnection xaConnectionFromProxy = ((ConnectionProxyXA) connFromDataSourceProxyXANative).getWrappedXAConnection();
Assertions.assertTrue(xaConnection == xaConnectionFromProxy);
Connection connectionFromProxy = ((ConnectionProxyXA) connFromDataSourceProxyXANative).getWrappedConnection();
Assertions.assertTrue(connection == connectionFromProxy);
}
use of io.seata.rm.datasource.xa.DataSourceProxyXANative 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!");
}
use of io.seata.rm.datasource.xa.DataSourceProxyXANative in project seata by seata.
the class XAModeTest2 method createDataSourceProxyXANative.
private DataSourceProxyXANative createDataSourceProxyXANative() throws Throwable {
XADataSource originalDS = createNewDruidXADataSource();
DataSourceProxyXANative dataSourceProxyXA = new DataSourceProxyXANative(originalDS);
return dataSourceProxyXA;
}
use of io.seata.rm.datasource.xa.DataSourceProxyXANative 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!");
}
use of io.seata.rm.datasource.xa.DataSourceProxyXANative 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!");
}
Aggregations