189 8069 5689

sharding-jdbc的XAConnectionWrapper怎么用

这篇文章将为大家详细讲解有关sharding-jdbc的XAConnectionWrapper怎么用,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。

目前创新互联已为近1000家的企业提供了网站建设、域名、虚拟空间、网站托管、企业网站设计、佳木斯网站维护等服务,公司将坚持客户导向、应用为本的策略,正道将秉承"和谐、参与、激情"的文化,与客户和合作伙伴齐心协力一起成长,共同发展。

主要研究一下sharding-jdbc的XAConnectionWrapper

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

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

@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方法

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验证了wrap及unwrap方法

小结

MySQLXAConnectionWrapper实现了XAConnectionWrapper接口,其wrap方法首先通过unwrapPhysicalConnection获取physicalConnection,然后反射执行XADataSource的wrapConnection方法

关于sharding-jdbc的XAConnectionWrapper怎么用就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。


本文标题:sharding-jdbc的XAConnectionWrapper怎么用
本文URL:http://jkwzsj.com/article/gdjooc.html