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