Search in sources :

Example 1 with BaseDataSourceResource

use of io.seata.rm.BaseDataSourceResource 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)

Example 2 with BaseDataSourceResource

use of io.seata.rm.BaseDataSourceResource 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)

Aggregations

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 ConnectionProxyXA (io.seata.rm.datasource.xa.ConnectionProxyXA)2 Connection (java.sql.Connection)2 XAConnection (javax.sql.XAConnection)2 XAResource (javax.transaction.xa.XAResource)2 Xid (javax.transaction.xa.Xid)2 Test (org.junit.jupiter.api.Test)2