Search in sources :

Example 1 with JDBC4ConnectionWrapper

use of com.mysql.jdbc.jdbc2.optional.JDBC4ConnectionWrapper 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)

Aggregations

DruidDataSource (com.alibaba.druid.pool.DruidDataSource)1 JDBC4MySQLConnection (com.mysql.jdbc.JDBC4MySQLConnection)1 JDBC4ConnectionWrapper (com.mysql.jdbc.jdbc2.optional.JDBC4ConnectionWrapper)1 ConnectionProxyXA (io.seata.rm.datasource.xa.ConnectionProxyXA)1 DataSourceProxyXA (io.seata.rm.datasource.xa.DataSourceProxyXA)1 Connection (java.sql.Connection)1 DatabaseMetaData (java.sql.DatabaseMetaData)1 Driver (java.sql.Driver)1 PooledConnection (javax.sql.PooledConnection)1 XAConnection (javax.sql.XAConnection)1 Test (org.junit.jupiter.api.Test)1