Search in sources :

Example 16 with DataSourceProxy

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

the class DynamicRoutingDataSource method closeDataSource.

/**
 * close db
 *
 * @param ds         dsName
 * @param dataSource db
 */
private void closeDataSource(String ds, DataSource dataSource) {
    try {
        if (dataSource instanceof ItemDataSource) {
            ((ItemDataSource) dataSource).close();
        } else {
            if (seata) {
                if (dataSource instanceof DataSourceProxy) {
                    DataSourceProxy dataSourceProxy = (DataSourceProxy) dataSource;
                    dataSource = dataSourceProxy.getTargetDataSource();
                }
            }
            if (p6spy) {
                if (dataSource instanceof P6DataSource) {
                    Field realDataSourceField = P6DataSource.class.getDeclaredField("realDataSource");
                    realDataSourceField.setAccessible(true);
                    dataSource = (DataSource) realDataSourceField.get(dataSource);
                }
            }
            Method closeMethod = ReflectionUtils.findMethod(dataSource.getClass(), "close");
            if (closeMethod != null) {
                closeMethod.invoke(dataSource);
            }
        }
    } catch (Exception e) {
        log.warn("dynamic-datasource closed datasource named [{}] failed", ds, e);
    }
}
Also used : P6DataSource(com.p6spy.engine.spy.P6DataSource) Field(java.lang.reflect.Field) DataSourceProxy(io.seata.rm.datasource.DataSourceProxy) ItemDataSource(com.baomidou.dynamic.datasource.ds.ItemDataSource) Method(java.lang.reflect.Method) CannotFindDataSourceException(com.baomidou.dynamic.datasource.exception.CannotFindDataSourceException)

Aggregations

DataSourceProxy (io.seata.rm.datasource.DataSourceProxy)16 DruidDataSource (com.alibaba.druid.pool.DruidDataSource)12 MockDriver (io.seata.rm.datasource.mock.MockDriver)12 Test (org.junit.jupiter.api.Test)10 MockStatement (com.alibaba.druid.mock.MockStatement)7 ConnectionProxy (io.seata.rm.datasource.ConnectionProxy)7 MockStatementBase (com.alibaba.druid.mock.MockStatementBase)6 TableMeta (io.seata.rm.datasource.sql.struct.TableMeta)6 StatementProxy (io.seata.rm.datasource.StatementProxy)5 Field (java.lang.reflect.Field)5 SQLException (java.sql.SQLException)5 Lists (com.google.common.collect.Lists)4 Types (java.sql.Types)4 List (java.util.List)4 BeforeAll (org.junit.jupiter.api.BeforeAll)4 SQLUtils (com.alibaba.druid.sql.SQLUtils)3 SQLStatement (com.alibaba.druid.sql.ast.SQLStatement)3 JdbcConstants (com.alibaba.druid.util.JdbcConstants)3 Assertions (org.junit.jupiter.api.Assertions)3 BeforeEach (org.junit.jupiter.api.BeforeEach)3