Search in sources :

Example 6 with DataSourceProxyXA

use of io.seata.rm.datasource.xa.DataSourceProxyXA in project seata by seata.

the class DataSourceProxyXATest method test_constructor.

@Test
public void test_constructor() {
    DataSource dataSource = new MockDataSource();
    DataSourceProxyXA dataSourceProxy = new DataSourceProxyXA(dataSource);
    Assertions.assertEquals(dataSourceProxy.getTargetDataSource(), dataSource);
    DataSourceProxyXA dataSourceProxy2 = new DataSourceProxyXA(dataSourceProxy);
    Assertions.assertEquals(dataSourceProxy2.getTargetDataSource(), dataSource);
}
Also used : DataSourceProxyXA(io.seata.rm.datasource.xa.DataSourceProxyXA) MockDataSource(io.seata.rm.datasource.mock.MockDataSource) MockDataSource(io.seata.rm.datasource.mock.MockDataSource) DruidDataSource(com.alibaba.druid.pool.DruidDataSource) DataSource(javax.sql.DataSource) Test(org.junit.jupiter.api.Test)

Example 7 with DataSourceProxyXA

use of io.seata.rm.datasource.xa.DataSourceProxyXA in project seata by seata.

the class DataSourceProxyXATest method testGetConnection.

@Test
public void testGetConnection() throws SQLException {
    // Mock
    Driver driver = Mockito.mock(Driver.class);
    JDBC4MySQLConnection connection = Mockito.mock(JDBC4MySQLConnection.class);
    Mockito.when(connection.getAutoCommit()).thenReturn(true);
    DatabaseMetaData metaData = Mockito.mock(DatabaseMetaData.class);
    Mockito.when(metaData.getURL()).thenReturn("jdbc:mysql:xxx");
    Mockito.when(connection.getMetaData()).thenReturn(metaData);
    Mockito.when(driver.connect(any(), any())).thenReturn(connection);
    DruidDataSource druidDataSource = new DruidDataSource();
    druidDataSource.setDriver(driver);
    DataSourceProxyXA dataSourceProxyXA = new DataSourceProxyXA(druidDataSource);
    Connection connFromDataSourceProxyXA = dataSourceProxyXA.getConnection();
    Assertions.assertFalse(connFromDataSourceProxyXA instanceof ConnectionProxyXA);
    RootContext.bind("test");
    connFromDataSourceProxyXA = dataSourceProxyXA.getConnection();
    Assertions.assertTrue(connFromDataSourceProxyXA instanceof ConnectionProxyXA);
    ConnectionProxyXA connectionProxyXA = (ConnectionProxyXA) dataSourceProxyXA.getConnection();
    Connection wrappedConnection = connectionProxyXA.getWrappedConnection();
    Assertions.assertTrue(wrappedConnection instanceof PooledConnection);
    Connection wrappedPhysicalConn = ((PooledConnection) wrappedConnection).getConnection();
    Assertions.assertTrue(wrappedPhysicalConn == connection);
    XAConnection xaConnection = connectionProxyXA.getWrappedXAConnection();
    Connection connectionInXA = xaConnection.getConnection();
    Assertions.assertTrue(connectionInXA instanceof JDBC4ConnectionWrapper);
}
Also used : DataSourceProxyXA(io.seata.rm.datasource.xa.DataSourceProxyXA) PooledConnection(javax.sql.PooledConnection) ConnectionProxyXA(io.seata.rm.datasource.xa.ConnectionProxyXA) JDBC4ConnectionWrapper(com.mysql.jdbc.jdbc2.optional.JDBC4ConnectionWrapper) Connection(java.sql.Connection) XAConnection(javax.sql.XAConnection) JDBC4MySQLConnection(com.mysql.jdbc.JDBC4MySQLConnection) PooledConnection(javax.sql.PooledConnection) Driver(java.sql.Driver) JDBC4MySQLConnection(com.mysql.jdbc.JDBC4MySQLConnection) DatabaseMetaData(java.sql.DatabaseMetaData) DruidDataSource(com.alibaba.druid.pool.DruidDataSource) XAConnection(javax.sql.XAConnection) Test(org.junit.jupiter.api.Test)

Example 8 with DataSourceProxyXA

use of io.seata.rm.datasource.xa.DataSourceProxyXA in project Mycat2 by MyCATApache.

the class SeataMySQLXADatasourceProvider method createDataSource.

@Override
public JdbcDataSource createDataSource(DatasourceConfig config) {
    String username = config.getUser();
    String password = config.getPassword();
    String url = Objects.requireNonNull(config.getUrl());
    String dbType = config.getDbType();
    int maxRetryCount = config.getMaxRetryCount();
    List<String> initSQLs = config.getInitSqls();
    int maxCon = config.getMaxCon();
    int minCon = config.getMinCon();
    MysqlXADataSource datasource = new MysqlXADataSource();
    datasource.setPassword(password);
    datasource.setUser(username);
    datasource.setUrl(url);
    return new JdbcDataSource(config, new DataSourceProxyXA(datasource));
}
Also used : DataSourceProxyXA(io.seata.rm.datasource.xa.DataSourceProxyXA) MysqlXADataSource(com.mysql.cj.jdbc.MysqlXADataSource) JdbcDataSource(io.mycat.datasource.jdbc.datasource.JdbcDataSource)

Example 9 with DataSourceProxyXA

use of io.seata.rm.datasource.xa.DataSourceProxyXA in project dynamic-datasource-spring-boot-starter by baomidou.

the class AbstractDataSourceCreator method wrapDataSource.

private DataSource wrapDataSource(DataSource dataSource, DataSourceProperty dataSourceProperty) {
    String name = dataSourceProperty.getPoolName();
    DataSource targetDataSource = dataSource;
    Boolean enabledP6spy = properties.getP6spy() && dataSourceProperty.getP6spy();
    if (enabledP6spy) {
        targetDataSource = new P6DataSource(dataSource);
        log.debug("dynamic-datasource [{}] wrap p6spy plugin", name);
    }
    Boolean enabledSeata = properties.getSeata() && dataSourceProperty.getSeata();
    SeataMode seataMode = properties.getSeataMode();
    if (enabledSeata) {
        if (SeataMode.XA == seataMode) {
            targetDataSource = new DataSourceProxyXA(targetDataSource);
        } else {
            targetDataSource = new DataSourceProxy(targetDataSource);
        }
        log.debug("dynamic-datasource [{}] wrap seata plugin transaction mode ", name);
    }
    return new ItemDataSource(name, dataSource, targetDataSource, enabledP6spy, enabledSeata, seataMode);
}
Also used : P6DataSource(com.p6spy.engine.spy.P6DataSource) DataSourceProxyXA(io.seata.rm.datasource.xa.DataSourceProxyXA) DataSourceProxy(io.seata.rm.datasource.DataSourceProxy) ItemDataSource(com.baomidou.dynamic.datasource.ds.ItemDataSource) ItemDataSource(com.baomidou.dynamic.datasource.ds.ItemDataSource) P6DataSource(com.p6spy.engine.spy.P6DataSource) DataSource(javax.sql.DataSource) SeataMode(com.baomidou.dynamic.datasource.enums.SeataMode)

Aggregations

DataSourceProxyXA (io.seata.rm.datasource.xa.DataSourceProxyXA)9 DruidDataSource (com.alibaba.druid.pool.DruidDataSource)6 DruidXADataSource (com.alibaba.druid.pool.xa.DruidXADataSource)5 MysqlXADataSource (com.mysql.jdbc.jdbc2.optional.MysqlXADataSource)4 AbstractDataSourceProxyXA (io.seata.rm.datasource.xa.AbstractDataSourceProxyXA)4 Connection (java.sql.Connection)4 XAConnection (javax.sql.XAConnection)4 XADataSource (javax.sql.XADataSource)4 PGXADataSource (org.postgresql.xa.PGXADataSource)4 DataSourceProxyXANative (io.seata.rm.datasource.xa.DataSourceProxyXANative)3 ResultSet (java.sql.ResultSet)3 Statement (java.sql.Statement)3 DataSource (javax.sql.DataSource)3 JdbcDataSource (io.mycat.datasource.jdbc.datasource.JdbcDataSource)2 Test (org.junit.jupiter.api.Test)2 ItemDataSource (com.baomidou.dynamic.datasource.ds.ItemDataSource)1 SeataMode (com.baomidou.dynamic.datasource.enums.SeataMode)1 MysqlXADataSource (com.mysql.cj.jdbc.MysqlXADataSource)1 JDBC4MySQLConnection (com.mysql.jdbc.JDBC4MySQLConnection)1 JDBC4ConnectionWrapper (com.mysql.jdbc.jdbc2.optional.JDBC4ConnectionWrapper)1