sharding-jdbc中XATransactionManager的用法

发布时间:2021-07-09 16:48:34 作者:chen
来源:亿速云 阅读:451

这篇文章主要介绍“sharding-jdbc中XATransactionManager的用法”,在日常操作中,相信很多人在sharding-jdbc中XATransactionManager的用法问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”sharding-jdbc中XATransactionManager的用法”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!

本文主要研究一下sharding-jdbc的XATransactionManager

XATransactionManager

incubator-shardingsphere-4.0.0-RC1/sharding-transaction/sharding-transaction-2pc/sharding-transaction-xa/sharding-transaction-xa-spi/src/main/java/org/apache/shardingsphere/transaction/xa/spi/XATransactionManager.java

public interface XATransactionManager extends AutoCloseable {
    
    /**
     * Initialize XA transaction manager.
     */
    void init();
    
    /**
     * Register recovery resource.
     *
     * @param dataSourceName data source name
     * @param xaDataSource XA data source
     */
    void registerRecoveryResource(String dataSourceName, XADataSource xaDataSource);
    
    /**
     * Remove recovery resource.
     *
     * @param dataSourceName data source name
     * @param xaDataSource   XA data source
     */
    void removeRecoveryResource(String dataSourceName, XADataSource xaDataSource);
    
    /**
     * Enlist single XA resource.
     * 
     * @param singleXAResource single XA resource
     */
    void enlistResource(SingleXAResource singleXAResource);
    
    /**
     * Get transaction manager.
     * 
     * @return transaction manager
     */
    TransactionManager getTransactionManager();
}

AtomikosTransactionManager

incubator-shardingsphere-4.0.0-RC1/sharding-transaction/sharding-transaction-2pc/sharding-transaction-xa/sharding-transaction-xa-core/src/main/java/org/apache/shardingsphere/transaction/xa/manager/atomikos/AtomikosTransactionManager.java

public final class AtomikosTransactionManager implements XATransactionManager {
    
    private final UserTransactionManager transactionManager = new UserTransactionManager();
    
    private final UserTransactionService userTransactionService = new UserTransactionServiceImp();
    
    @Override
    public void init() {
        userTransactionService.init();
    }
    
    @Override
    public void registerRecoveryResource(final String dataSourceName, final XADataSource xaDataSource) {
        userTransactionService.registerResource(new AtomikosXARecoverableResource(dataSourceName, xaDataSource));
    }
    
    @Override
    public void removeRecoveryResource(final String dataSourceName, final XADataSource xaDataSource) {
        userTransactionService.removeResource(new AtomikosXARecoverableResource(dataSourceName, xaDataSource));
    }
    
    @Override
    @SneakyThrows
    public void enlistResource(final SingleXAResource xaResource) {
        transactionManager.getTransaction().enlistResource(xaResource);
    }
    
    @Override
    public TransactionManager getTransactionManager() {
        return transactionManager;
    }
    
    @Override
    public void close() {
        userTransactionService.shutdown(true);
    }
}

AtomikosTransactionManagerTest

incubator-shardingsphere-4.0.0-RC1/sharding-transaction/sharding-transaction-2pc/sharding-transaction-xa/sharding-transaction-xa-core/src/test/java/org/apache/shardingsphere/transaction/xa/manager/atomikos/AtomikosTransactionManagerTest.java

@RunWith(MockitoJUnitRunner.class)
public final class AtomikosTransactionManagerTest {
    
    private AtomikosTransactionManager atomikosTransactionManager = new AtomikosTransactionManager();
    
    @Mock
    private UserTransactionManager userTransactionManager;
    
    @Mock
    private UserTransactionService userTransactionService;
    
    @Mock
    private XADataSource xaDataSource;
    
    @Before
    public void setUp() {
        ReflectiveUtil.setProperty(atomikosTransactionManager, "transactionManager", userTransactionManager);
        ReflectiveUtil.setProperty(atomikosTransactionManager, "userTransactionService", userTransactionService);
    }
    
    @Test
    public void assertInit() {
        atomikosTransactionManager.init();
        verify(userTransactionService).init();
    }
    
    @Test
    public void assertRegisterRecoveryResource() {
        atomikosTransactionManager.registerRecoveryResource("ds1", xaDataSource);
        verify(userTransactionService).registerResource(any(AtomikosXARecoverableResource.class));
    }
    
    @Test
    public void assertRemoveRecoveryResource() {
        atomikosTransactionManager.removeRecoveryResource("ds1", xaDataSource);
        verify(userTransactionService).removeResource(any(AtomikosXARecoverableResource.class));
    }
    
    @Test
    public void assertEnListResource() throws Exception {
        SingleXAResource singleXAResource = mock(SingleXAResource.class);
        Transaction transaction = mock(Transaction.class);
        when(userTransactionManager.getTransaction()).thenReturn(transaction);
        atomikosTransactionManager.enlistResource(singleXAResource);
        verify(transaction).enlistResource(singleXAResource);
    }
    
    @Test
    public void assertTransactionManager() {
        assertThat(atomikosTransactionManager.getTransactionManager(), CoreMatchers.<TransactionManager>is(userTransactionManager));
    }
    
    @Test
    public void assertClose() {
        atomikosTransactionManager.close();
        verify(userTransactionService).shutdown(true);
    }
}

小结

XATransactionManager继承了AutoCloseable接口,它定义了init、registerRecoveryResource、removeRecoveryResource、enlistResource、getTransactionManager方法

到此,关于“sharding-jdbc中XATransactionManager的用法”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注亿速云网站,小编会继续努力为大家带来更多实用的文章!

推荐阅读:
  1. ShardingSphere中如何进行Sharding-JDBC分库的实战
  2. sharding-jdbc中SQLExecutionHook的用法

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

sharding-jdbc

上一篇:IntelliJ IDEA有哪些常用快捷键

下一篇:如何解决git commit 大文件推送失败问题

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》