In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces "the usage of XATransactionManager in sharding-jdbc". In daily operation, I believe many people have doubts about the usage of XATransactionManager in sharding-jdbc. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful for you to answer the doubts about the usage of XATransactionManager in sharding-jdbc! Next, please follow the editor to study!
Order
This paper mainly studies the XATransactionManager of 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 singleXA resource * / void enlistResource (SingleXAResource singleXAResource); / * * Get transaction manager. * * @ return transaction manager * / TransactionManager getTransactionManager ();}
XATransactionManager inherits the AutoCloseable interface, which defines init, registerRecoveryResource, removeRecoveryResource, enlistResource, getTransactionManager methods
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 implements the XATransactionManager interface with built-in UserTransactionManager and UserTransactionServiceImp attributes
Init method executes userTransactionService.init (); registerRecoveryResource method executes userTransactionService.registerResource (new AtomikosXARecoverableResource (dataSourceName, xaDataSource)); removeRecoveryResource method executes userTransactionService.removeResource (new AtomikosXARecoverableResource (dataSourceName, xaDataSource))
EnlistResource executes transactionManager.getTransaction () .enlistResource (xaResource); getTransactionManager returns the UserTransactionManager;close method to execute 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.is (userTransactionManager));} @ Test public void assertClose () {atomikosTransactionManager.close (); verify (userTransactionService) .shutdown (true);}}
Here mock userTransactionManager, userTransactionService, xaDataSource
Summary
XATransactionManager inherits the AutoCloseable interface, which defines init, registerRecoveryResource, removeRecoveryResource, enlistResource, getTransactionManager methods
At this point, the study of "the use of XATransactionManager in sharding-jdbc" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.