Search in sources :

Example 1 with ConnectionProxyXA

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

the class ConnectionProxyXATest method testXARollback.

@Test
public void testXARollback() 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.xaRollback("xxx", 123L, null);
    Mockito.verify(xaResource, times(0)).commit(any(Xid.class), any(Boolean.class));
    Mockito.verify(xaResource).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 2 with ConnectionProxyXA

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

the class ConnectionProxyXATest method testCreateStatement.

@Test
public void testCreateStatement() throws Throwable {
    Connection connection = Mockito.mock(Connection.class);
    Mockito.when(connection.getAutoCommit()).thenReturn(true);
    XAConnection xaConnection = Mockito.mock(XAConnection.class);
    BaseDataSourceResource<ConnectionProxyXA> baseDataSourceResource = Mockito.mock(BaseDataSourceResource.class);
    String xid = "xxx";
    ConnectionProxyXA connectionProxyXA = new ConnectionProxyXA(connection, xaConnection, baseDataSourceResource, xid);
    Statement statement = connectionProxyXA.createStatement();
    Assertions.assertTrue(statement instanceof StatementProxyXA);
}
Also used : ConnectionProxyXA(io.seata.rm.datasource.xa.ConnectionProxyXA) Statement(java.sql.Statement) StatementProxyXA(io.seata.rm.datasource.xa.StatementProxyXA) Connection(java.sql.Connection) XAConnection(javax.sql.XAConnection) XAConnection(javax.sql.XAConnection) Test(org.junit.jupiter.api.Test)

Example 3 with ConnectionProxyXA

use of io.seata.rm.datasource.xa.ConnectionProxyXA 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);
}
Also used : XADataSource(javax.sql.XADataSource) DataSourceProxyXANative(io.seata.rm.datasource.xa.DataSourceProxyXANative) ConnectionProxyXA(io.seata.rm.datasource.xa.ConnectionProxyXA) Connection(java.sql.Connection) XAConnection(javax.sql.XAConnection) DatabaseMetaData(java.sql.DatabaseMetaData) XAConnection(javax.sql.XAConnection) Test(org.junit.jupiter.api.Test)

Example 4 with ConnectionProxyXA

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

the class ConnectionProxyXATest method testClose.

@Test
public void testClose() throws Throwable {
    Connection connection = Mockito.mock(Connection.class);
    Mockito.when(connection.getAutoCommit()).thenReturn(true);
    XAConnection xaConnection = Mockito.mock(XAConnection.class);
    BaseDataSourceResource<ConnectionProxyXA> baseDataSourceResource = Mockito.mock(BaseDataSourceResource.class);
    String xid = "xxx";
    ConnectionProxyXA connectionProxyXA1 = new ConnectionProxyXA(connection, xaConnection, baseDataSourceResource, xid);
    connectionProxyXA1.init();
    // Kept
    connectionProxyXA1.setHeld(true);
    // call close on proxy
    connectionProxyXA1.close();
    // Assert the original connection was NOT closed
    Mockito.verify(connection, times(0)).close();
    ConnectionProxyXA connectionProxyXA2 = new ConnectionProxyXA(connection, xaConnection, baseDataSourceResource, xid);
    connectionProxyXA2.init();
    // Kept
    connectionProxyXA2.setHeld(false);
    // call close on proxy
    connectionProxyXA2.close();
    // Assert the original connection was ALSO closed
    Mockito.verify(connection).close();
}
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 5 with ConnectionProxyXA

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

the class ConnectionProxyXATest method testXABranchRollback.

@Test
public void testXABranchRollback() 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.rollback();
    Mockito.verify(xaResource).end(any(Xid.class), any(Integer.class));
    // Not prepared
    Mockito.verify(xaResource, times(0)).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)

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