這篇文章將為大家詳細講解有關sharding-jdbc的XAConnectionWrapper怎么用,文章內容質量較高,因此小編分享給大家做個參考,希望大家閱讀完這篇文章后對相關知識有一定的了解。
目前創新互聯已為近1000家的企業提供了網站建設、域名、虛擬空間、網站托管、企業網站設計、佳木斯網站維護等服務,公司將堅持客戶導向、應用為本的策略,正道將秉承"和諧、參與、激情"的文化,與客戶和合作伙伴齊心協力一起成長,共同發展。
主要研究一下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定義了wrap接口,返回XAConnection
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
@RequiredArgsConstructor public 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(String.format("Cannot support xa datasource: `%s`", xaDataSourceClassName)); } } }
MySQLXAConnectionWrapper實現了XAConnectionWrapper接口,其wrap方法首先通過unwrapPhysicalConnection獲取physicalConnection,然后反射執行XADataSource的wrapConnection方法
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<Object>) 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驗證了wrap及unwrap方法
MySQLXAConnectionWrapper實現了XAConnectionWrapper接口,其wrap方法首先通過unwrapPhysicalConnection獲取physicalConnection,然后反射執行XADataSource的wrapConnection方法
關于sharding-jdbc的XAConnectionWrapper怎么用就分享到這里了,希望以上內容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
網站題目:sharding-jdbc的XAConnectionWrapper怎么用
標題來源:http://vcdvsql.cn/article2/gdjooc.html
成都網站建設公司_創新互聯,為您提供自適應網站、品牌網站建設、建站公司、網站導航、Google、ChatGPT
聲明:本網站發布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創新互聯