专栏首页码匠的流水账聊聊sharding-jdbc的XAConnectionWrapper
原创

聊聊sharding-jdbc的XAConnectionWrapper

本文主要研究一下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<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方法

doc

原创声明,本文系作者授权云+社区发表,未经许可,不得转载。

如有侵权,请联系 yunjia_community@tencent.com 删除。

我来说两句

0 条评论
登录 后参与评论

相关文章

  • 聊聊sharding-jdbc的XAConnectionWrapper

    本文主要研究一下sharding-jdbc的XAConnectionWrapper

    codecraft
  • 聊聊puma的ClientPositionService

    puma/puma/src/main/java/com/dianping/puma/biz/service/ClientPositionService.java

    codecraft
  • 聊聊gorm的Locking

    Locking定义了Strength、Table、Options属性;Build方法先追加Strength,后面根据Table、Options属性追加语句;St...

    codecraft
  • 聊聊sharding-jdbc的XAConnectionWrapper

    本文主要研究一下sharding-jdbc的XAConnectionWrapper

    codecraft
  • PHP优化杂烩

    原文出处: 火丁笔记(@火丁笔记) 讲 PHP 优化的文章往往都是教大家如何编写高效的代码,本文打算从另一个角度来讨论问题,教大家如何配置高效的环境,如此同样...

    wangxl
  • DianNao系列加速器总结(1)——架构与运算单元简介整体架构运算模块

    月见樽
  • NIO效率高的原理之零拷贝与直接内存映射

    在笔者上一篇博客,详解了NIO,并总结NIO相比BIO的效率要高的三个原因,点击查看。

    全菜工程师小辉
  • [账号共享]分享一波免费企业QQ账号

    Youngxj
  • 内存盘软件Primo Ramdisk【v5.5+v5.7】【附注册&激活方法】

    用户2135432
  • 吴恩达悄然发布AI维基,另外他的技术岗已经招满了

    问耕 发自 凹非寺 量子位 出品 | 公众号 QbitAI ? 吴恩达又有新动作。 wiki 首先来说第一个。 这个长假期间,吴恩达团队在deeplearni...

    量子位

扫码关注云+社区

领取腾讯云代金券