In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article will explain in detail how to use the XAConnectionWrapper of sharding-jdbc. The content of the article is of high quality, so the editor shares it for you as a reference. I hope you will have a certain understanding of the relevant knowledge after reading this article.
Order
Mainly study the XAConnectionWrapper of sharding-jdbc
XAConnectionWrapper
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/jta/connection/XAConnectionWrapper.java
Public interface XAConnectionWrapper {/ * * Wrap a normal connection to XA connection. * * @ param xaDataSource XA data source * @ param connection connection * @ return sharding XA connection * / XAConnection wrap (XADataSource xaDataSource, Connection connection);}
XAConnectionWrapper defines the wrap interface and returns XAConnection.
MySQLXAConnectionWrapper
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/jta/connection/dialect/MySQLXAConnectionWrapper.java
@ RequiredArgsConstructorpublic final class MySQLXAConnectionWrapper implements XAConnectionWrapper {private static final String MYSQL_XA_DATASOURCE_5 = "com.mysql.jdbc.jdbc2.optional.MysqlXADataSource"; private static final String MYSQL_XA_DATASOURCE_8 = "com.mysql.cj.jdbc.MysqlXADataSource"; @ SneakyThrows @ Override public XAConnection wrap (final XADataSource xaDataSource, final Connection connection) {Connection physicalConnection = unwrapPhysicalConnection (xaDataSource.getClass () .getName (), connection) Method method = xaDataSource.getClass (). GetDeclaredMethod ("wrapConnection", Connection.class); method.setAccessible (true); return (XAConnection) method.invoke (xaDataSource, physicalConnection) } @ SneakyThrows private Connection unwrapPhysicalConnection (final String xaDataSourceClassName, final Connection connection) {switch (xaDataSourceClassName) {case MYSQL_XA_DATASOURCE_5: return (Connection) connection.unwrap (Class.forName ("com.mysql.jdbc.Connection"); case MYSQL_XA_DATASOURCE_8: return (Connection) connection.unwrap (Class.forName ("com.mysql.cj.jdbc.JdbcConnection")) Default: throw new UnsupportedOperationException ("Cannot support xa datasource: `% s`", xaDataSourceClassName));}
MySQLXAConnectionWrapper implements the XAConnectionWrapper interface. Its wrap method first acquires physicalConnection through unwrapPhysicalConnection, and then reflects the wrapConnection method that executes XADataSource.
MySQLXAConnectionWrapperTest
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/jta/connection/dialect/MySQLXAConnectionWrapperTest.java
@ RunWith (MockitoJUnitRunner.class) public final class MySQLXAConnectionWrapperTest {private XADataSource xaDataSource; @ Mock private Connection connection; @ Before @ SuppressWarnings ("unchecked") public void setUp () throws SQLException, ClassNotFoundException {Connection connection = (Connection) mock (Class.forName ("com.mysql.jdbc.Connection")); DataSource dataSource = DataSourceUtils.build (HikariDataSource.class, DatabaseType.MySQL, "ds1"); xaDataSource = XADataSourceFactory.build (DatabaseType.MySQL, dataSource) When (this.connection.unwrap ((Class) any ()) .thenReturn (connection);} @ Test public void assertCreateMySQLConnection () throws SQLException {XAConnection actual = new MySQLXAConnectionWrapper (). Wrap (xaDataSource, connection); assertThat (actual.getXAResource (), instanceOf (XAResource.class)); assertThat (actual.getConnection (), instanceOf (Connection.class));}}
MySQLXAConnectionWrapperTest validates wrap and unwrap methods.
Summary
MySQLXAConnectionWrapper implements the XAConnectionWrapper interface. Its wrap method first acquires physicalConnection through unwrapPhysicalConnection, and then reflects the wrapConnection method that executes XADataSource.
About how to use sharding-jdbc XAConnectionWrapper to share here, I hope the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.
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.