Search in sources :

Example 1 with ItemDataSource

use of com.baomidou.dynamic.datasource.ds.ItemDataSource 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)

Example 2 with ItemDataSource

use of com.baomidou.dynamic.datasource.ds.ItemDataSource 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

ItemDataSource (com.baomidou.dynamic.datasource.ds.ItemDataSource)2 P6DataSource (com.p6spy.engine.spy.P6DataSource)2 DataSourceProxy (io.seata.rm.datasource.DataSourceProxy)2 SeataMode (com.baomidou.dynamic.datasource.enums.SeataMode)1 CannotFindDataSourceException (com.baomidou.dynamic.datasource.exception.CannotFindDataSourceException)1 DataSourceProxyXA (io.seata.rm.datasource.xa.DataSourceProxyXA)1 Field (java.lang.reflect.Field)1 Method (java.lang.reflect.Method)1 DataSource (javax.sql.DataSource)1