這篇文章主要介紹“sharding-jdbc中XATransactionManager的用法”,在日常操作中,相信很多人在sharding-jdbc中XATransactionManager的用法問題上存在疑惑,小編查閱了各式資料,整理出簡(jiǎn)單好用的操作方法,希望對(duì)大家解答”sharding-jdbc中XATransactionManager的用法”的疑惑有所幫助!接下來,請(qǐng)跟著小編一起來學(xué)習(xí)吧!
成都創(chuàng)新互聯(lián)專注為客戶提供全方位的互聯(lián)網(wǎng)綜合服務(wù),包含不限于網(wǎng)站制作、成都做網(wǎng)站、啟東網(wǎng)絡(luò)推廣、重慶小程序開發(fā)、啟東網(wǎng)絡(luò)營(yíng)銷、啟東企業(yè)策劃、啟東品牌公關(guān)、搜索引擎seo、人物專訪、企業(yè)宣傳片、企業(yè)代運(yùn)營(yíng)等,從售前售中售后,我們都將竭誠(chéng)為您服務(wù),您的肯定,是我們最大的嘉獎(jiǎng);成都創(chuàng)新互聯(lián)為所有大學(xué)生創(chuàng)業(yè)者提供啟東建站搭建服務(wù),24小時(shí)服務(wù)熱線:18980820575,官方網(wǎng)址:www.cdcxhl.com
本文主要研究一下sharding-jdbc的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方法
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實(shí)現(xiàn)了XATransactionManager接口,它內(nèi)置了UserTransactionManager、UserTransactionServiceImp屬性
init方法執(zhí)行userTransactionService.init();registerRecoveryResource方法執(zhí)行userTransactionService.registerResource(new AtomikosXARecoverableResource(dataSourceName, xaDataSource));removeRecoveryResource方法執(zhí)行userTransactionService.removeResource(new AtomikosXARecoverableResource(dataSourceName, xaDataSource))
enlistResource執(zhí)行transactionManager.getTransaction().enlistResource(xaResource);getTransactionManager返回UserTransactionManager;close方法執(zhí)行userTransactionService.shutdown(true)
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.is(userTransactionManager)); } @Test public void assertClose() { atomikosTransactionManager.close(); verify(userTransactionService).shutdown(true); } }
這里mock了userTransactionManager、userTransactionService、xaDataSource
XATransactionManager繼承了AutoCloseable接口,它定義了init、registerRecoveryResource、removeRecoveryResource、enlistResource、getTransactionManager方法
到此,關(guān)于“sharding-jdbc中XATransactionManager的用法”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實(shí)踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識(shí),請(qǐng)繼續(xù)關(guān)注創(chuàng)新互聯(lián)網(wǎng)站,小編會(huì)繼續(xù)努力為大家?guī)砀鄬?shí)用的文章!