Search in sources :

Example 1 with ResourceManager

use of io.seata.core.model.ResourceManager in project seata by seata.

the class ConnectionProxyTest method initBeforeEach.

@BeforeEach
public void initBeforeEach() throws Exception {
    branchRollbackFlagField = ConnectionProxy.LockRetryPolicy.class.getDeclaredField("LOCK_RETRY_POLICY_BRANCH_ROLLBACK_ON_CONFLICT");
    Field modifiersField = Field.class.getDeclaredField("modifiers");
    modifiersField.setAccessible(true);
    modifiersField.setInt(branchRollbackFlagField, branchRollbackFlagField.getModifiers() & ~Modifier.FINAL);
    branchRollbackFlagField.setAccessible(true);
    boolean branchRollbackFlag = (boolean) branchRollbackFlagField.get(null);
    Assertions.assertTrue(branchRollbackFlag);
    dataSourceProxy = Mockito.mock(DataSourceProxy.class);
    Mockito.when(dataSourceProxy.getResourceId()).thenReturn(TEST_RESOURCE_ID);
    ResourceManager rm = Mockito.mock(ResourceManager.class);
    Mockito.when(rm.branchRegister(BranchType.AT, dataSourceProxy.getResourceId(), null, TEST_XID, null, lockKey)).thenThrow(new TransactionException(TransactionExceptionCode.LockKeyConflict));
    DefaultResourceManager defaultResourceManager = DefaultResourceManager.get();
    Assertions.assertNotNull(defaultResourceManager);
    DefaultResourceManager.mockResourceManager(BranchType.AT, rm);
}
Also used : Field(java.lang.reflect.Field) TransactionException(io.seata.core.exception.TransactionException) DefaultResourceManager(io.seata.rm.DefaultResourceManager) DefaultResourceManager(io.seata.rm.DefaultResourceManager) ResourceManager(io.seata.core.model.ResourceManager) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 2 with ResourceManager

use of io.seata.core.model.ResourceManager 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 3 with ResourceManager

use of io.seata.core.model.ResourceManager 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

ResourceManager (io.seata.core.model.ResourceManager)3 DefaultResourceManager (io.seata.rm.DefaultResourceManager)3 Resource (io.seata.core.model.Resource)2 BaseDataSourceResource (io.seata.rm.BaseDataSourceResource)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 TransactionException (io.seata.core.exception.TransactionException)1 Field (java.lang.reflect.Field)1 BeforeEach (org.junit.jupiter.api.BeforeEach)1