Search in sources :

Example 6 with ConnectionProxyXA

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

the class ConnectionProxyXATest method testXACommit.

@Test
public void testXACommit() throws Throwable {
    Connection connection = Mockito.mock(Connection.class);
    Mockito.when(connection.getAutoCommit()).thenReturn(true);
    XAResource xaResource = Mockito.mock(XAResource.class);
    XAConnection xaConnection = Mockito.mock(XAConnection.class);
    Mockito.when(xaConnection.getXAResource()).thenReturn(xaResource);
    BaseDataSourceResource<ConnectionProxyXA> baseDataSourceResource = Mockito.mock(BaseDataSourceResource.class);
    String xid = "xxx";
    ConnectionProxyXA connectionProxyXA = new ConnectionProxyXA(connection, xaConnection, baseDataSourceResource, xid);
    connectionProxyXA.init();
    connectionProxyXA.xaCommit("xxx", 123L, null);
    Mockito.verify(xaResource).commit(any(Xid.class), any(Boolean.class));
    Mockito.verify(xaResource, times(0)).rollback(any(Xid.class));
}
Also used : XAResource(javax.transaction.xa.XAResource) Xid(javax.transaction.xa.Xid) ConnectionProxyXA(io.seata.rm.datasource.xa.ConnectionProxyXA) Connection(java.sql.Connection) XAConnection(javax.sql.XAConnection) XAConnection(javax.sql.XAConnection) Test(org.junit.jupiter.api.Test)

Example 7 with ConnectionProxyXA

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

the class ConnectionProxyXATest method testXABranchCommit.

@Test
public void testXABranchCommit() throws Throwable {
    Connection connection = Mockito.mock(Connection.class);
    Mockito.when(connection.getAutoCommit()).thenReturn(true);
    XAResource xaResource = Mockito.mock(XAResource.class);
    XAConnection xaConnection = Mockito.mock(XAConnection.class);
    Mockito.when(xaConnection.getXAResource()).thenReturn(xaResource);
    BaseDataSourceResource<ConnectionProxyXA> baseDataSourceResource = Mockito.mock(BaseDataSourceResource.class);
    String xid = "xxx";
    ResourceManager resourceManager = Mockito.mock(ResourceManager.class);
    Mockito.doNothing().when(resourceManager).registerResource(any(Resource.class));
    DefaultResourceManager.get();
    DefaultResourceManager.mockResourceManager(BranchType.XA, resourceManager);
    ConnectionProxyXA connectionProxyXA = new ConnectionProxyXA(connection, xaConnection, baseDataSourceResource, xid);
    connectionProxyXA.init();
    connectionProxyXA.setAutoCommit(false);
    // Assert setAutoCommit = false was NEVER invoked on the wrapped connection
    Mockito.verify(connection, times(0)).setAutoCommit(false);
    // Assert XA start was invoked
    Mockito.verify(xaResource).start(any(Xid.class), any(Integer.class));
    connectionProxyXA.commit();
    Mockito.verify(xaResource).end(any(Xid.class), any(Integer.class));
    Mockito.verify(xaResource).prepare(any(Xid.class));
}
Also used : XAResource(javax.transaction.xa.XAResource) Xid(javax.transaction.xa.Xid) ConnectionProxyXA(io.seata.rm.datasource.xa.ConnectionProxyXA) Connection(java.sql.Connection) XAConnection(javax.sql.XAConnection) XAResource(javax.transaction.xa.XAResource) Resource(io.seata.core.model.Resource) BaseDataSourceResource(io.seata.rm.BaseDataSourceResource) DefaultResourceManager(io.seata.rm.DefaultResourceManager) ResourceManager(io.seata.core.model.ResourceManager) XAConnection(javax.sql.XAConnection) Test(org.junit.jupiter.api.Test)

Example 8 with ConnectionProxyXA

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

the class ConnectionProxyXATest method testInit.

@Test
public void testInit() throws Throwable {
    Connection connection = Mockito.mock(Connection.class);
    Mockito.when(connection.getAutoCommit()).thenReturn(false);
    XAConnection xaConnection = Mockito.mock(XAConnection.class);
    BaseDataSourceResource<ConnectionProxyXA> baseDataSourceResource = Mockito.mock(BaseDataSourceResource.class);
    String xid = "xxx";
    ConnectionProxyXA connectionProxyXA = new ConnectionProxyXA(connection, xaConnection, baseDataSourceResource, xid);
    Assertions.assertThrows(IllegalStateException.class, connectionProxyXA::init, "Connection[autocommit=false] as default is NOT supported");
}
Also used : ConnectionProxyXA(io.seata.rm.datasource.xa.ConnectionProxyXA) Connection(java.sql.Connection) XAConnection(javax.sql.XAConnection) XAConnection(javax.sql.XAConnection) Test(org.junit.jupiter.api.Test)

Example 9 with ConnectionProxyXA

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

the class DataSourceProxyXATest method testGetConnection.

@Test
public void testGetConnection() throws SQLException {
    // Mock
    Driver driver = Mockito.mock(Driver.class);
    JDBC4MySQLConnection connection = Mockito.mock(JDBC4MySQLConnection.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);
    Mockito.when(driver.connect(any(), any())).thenReturn(connection);
    DruidDataSource druidDataSource = new DruidDataSource();
    druidDataSource.setDriver(driver);
    DataSourceProxyXA dataSourceProxyXA = new DataSourceProxyXA(druidDataSource);
    Connection connFromDataSourceProxyXA = dataSourceProxyXA.getConnection();
    Assertions.assertFalse(connFromDataSourceProxyXA instanceof ConnectionProxyXA);
    RootContext.bind("test");
    connFromDataSourceProxyXA = dataSourceProxyXA.getConnection();
    Assertions.assertTrue(connFromDataSourceProxyXA instanceof ConnectionProxyXA);
    ConnectionProxyXA connectionProxyXA = (ConnectionProxyXA) dataSourceProxyXA.getConnection();
    Connection wrappedConnection = connectionProxyXA.getWrappedConnection();
    Assertions.assertTrue(wrappedConnection instanceof PooledConnection);
    Connection wrappedPhysicalConn = ((PooledConnection) wrappedConnection).getConnection();
    Assertions.assertTrue(wrappedPhysicalConn == connection);
    XAConnection xaConnection = connectionProxyXA.getWrappedXAConnection();
    Connection connectionInXA = xaConnection.getConnection();
    Assertions.assertTrue(connectionInXA instanceof JDBC4ConnectionWrapper);
}
Also used : DataSourceProxyXA(io.seata.rm.datasource.xa.DataSourceProxyXA) PooledConnection(javax.sql.PooledConnection) ConnectionProxyXA(io.seata.rm.datasource.xa.ConnectionProxyXA) JDBC4ConnectionWrapper(com.mysql.jdbc.jdbc2.optional.JDBC4ConnectionWrapper) Connection(java.sql.Connection) XAConnection(javax.sql.XAConnection) JDBC4MySQLConnection(com.mysql.jdbc.JDBC4MySQLConnection) PooledConnection(javax.sql.PooledConnection) Driver(java.sql.Driver) JDBC4MySQLConnection(com.mysql.jdbc.JDBC4MySQLConnection) DatabaseMetaData(java.sql.DatabaseMetaData) DruidDataSource(com.alibaba.druid.pool.DruidDataSource) XAConnection(javax.sql.XAConnection) Test(org.junit.jupiter.api.Test)

Aggregations

ConnectionProxyXA (io.seata.rm.datasource.xa.ConnectionProxyXA)9 Connection (java.sql.Connection)9 XAConnection (javax.sql.XAConnection)9 Test (org.junit.jupiter.api.Test)9 XAResource (javax.transaction.xa.XAResource)4 Xid (javax.transaction.xa.Xid)4 Resource (io.seata.core.model.Resource)2 ResourceManager (io.seata.core.model.ResourceManager)2 BaseDataSourceResource (io.seata.rm.BaseDataSourceResource)2 DefaultResourceManager (io.seata.rm.DefaultResourceManager)2 DatabaseMetaData (java.sql.DatabaseMetaData)2 DruidDataSource (com.alibaba.druid.pool.DruidDataSource)1 JDBC4MySQLConnection (com.mysql.jdbc.JDBC4MySQLConnection)1 JDBC4ConnectionWrapper (com.mysql.jdbc.jdbc2.optional.JDBC4ConnectionWrapper)1 DataSourceProxyXA (io.seata.rm.datasource.xa.DataSourceProxyXA)1 DataSourceProxyXANative (io.seata.rm.datasource.xa.DataSourceProxyXANative)1 StatementProxyXA (io.seata.rm.datasource.xa.StatementProxyXA)1 Driver (java.sql.Driver)1 Statement (java.sql.Statement)1 PooledConnection (javax.sql.PooledConnection)1