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);
}
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);
}
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));
}
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);
}
Aggregations