91超碰碰碰碰久久久久久综合_超碰av人澡人澡人澡人澡人掠_国产黄大片在线观看画质优化_txt小说免费全本

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

sharding-jdbc中XATransactionManager的用法

發布時間:2021-07-09 16:48:34 來源:億速云 閱讀:438 作者:chen 欄目:大數據

這篇文章主要介紹“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();
}
  • XATransactionManager繼承了AutoCloseable接口,它定義了init、registerRecoveryResource、removeRecoveryResource、enlistResource、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);
    }
}
  • AtomikosTransactionManager實現了XATransactionManager接口,它內置了UserTransactionManager、UserTransactionServiceImp屬性

  • init方法執行userTransactionService.init();registerRecoveryResource方法執行userTransactionService.registerResource(new AtomikosXARecoverableResource(dataSourceName, xaDataSource));removeRecoveryResource方法執行userTransactionService.removeResource(new AtomikosXARecoverableResource(dataSourceName, xaDataSource))

  • enlistResource執行transactionManager.getTransaction().enlistResource(xaResource);getTransactionManager返回UserTransactionManager;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);
    }
}
  • 這里mock了userTransactionManager、userTransactionService、xaDataSource

小結

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

到此,關于“sharding-jdbc中XATransactionManager的用法”的學習就結束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學習,快去試試吧!若想繼續學習更多相關知識,請繼續關注億速云網站,小編會繼續努力為大家帶來更多實用的文章!

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

霍州市| 滦南县| 秦皇岛市| 博乐市| 芮城县| 商都县| 西乌珠穆沁旗| 嘉祥县| 沁源县| 龙川县| 南京市| 宜兰市| 新乡县| 尼玛县| 车险| 巩义市| 雅安市| 乌海市| 建瓯市| 雷山县| 昆明市| 白朗县| 尼木县| 龙里县| 定日县| 清新县| 巴东县| 南康市| 徐州市| 肥乡县| 广河县| 拜城县| 宾阳县| 岳西县| 麻阳| 怀集县| 松滋市| 金寨县| 昭通市| 理塘县| 股票|