This commit is contained in:
parent
5d407ad65b
commit
c3c02a9473
|
@ -1,58 +1,58 @@
|
|||
package com.recovery.admin.boot.config.ds;
|
||||
|
||||
import com.alibaba.druid.pool.DruidDataSource;
|
||||
import org.apache.ibatis.session.SqlSessionFactory;
|
||||
import org.mybatis.spring.SqlSessionFactoryBean;
|
||||
import org.springframework.beans.factory.annotation.Qualifier;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
|
||||
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
|
||||
//@Configuration
|
||||
//// 扫描 Mapper 接口并容器管理
|
||||
//@MapperScan(basePackages = TeachingDataSourceConfig.PACKAGE, sqlSessionFactoryRef = "teachingSqlSessionFactory")
|
||||
public class TeachingDataSourceConfig {
|
||||
// 精确到 course 目录,以便跟其他数据源隔离
|
||||
static final String PACKAGE = "com.recovery.admin.boot.mapper";
|
||||
static final String MAPPER_LOCATION = "classpath:mapper/*.xml";
|
||||
|
||||
@Value("${admin.datasource.url}")
|
||||
private String url;
|
||||
|
||||
@Value("${admin.datasource.username}")
|
||||
private String user;
|
||||
|
||||
@Value("${admin.datasource.password}")
|
||||
private String password;
|
||||
|
||||
@Value("${admin.datasource.driverClassName}")
|
||||
private String driverClass;
|
||||
|
||||
@Bean(name = "adminDataSource")
|
||||
public DataSource cpdDataSource() {
|
||||
DruidDataSource dataSource = new DruidDataSource();
|
||||
dataSource.setDriverClassName(driverClass);
|
||||
dataSource.setUrl(url);
|
||||
dataSource.setUsername(user);
|
||||
dataSource.setPassword(password);
|
||||
return dataSource;
|
||||
}
|
||||
|
||||
@Bean(name = "adminTransactionManager")
|
||||
public DataSourceTransactionManager courseTransactionManager() {
|
||||
return new DataSourceTransactionManager(cpdDataSource());
|
||||
}
|
||||
|
||||
@Bean(name = "adminSqlSessionFactory")
|
||||
public SqlSessionFactory cpdSqlSessionFactory(@Qualifier("adminDataSource") DataSource cpdDataSource)
|
||||
throws Exception {
|
||||
final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
|
||||
sessionFactory.setDataSource(cpdDataSource);
|
||||
sessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver()
|
||||
.getResources(TeachingDataSourceConfig.MAPPER_LOCATION));
|
||||
return sessionFactory.getObject();
|
||||
}
|
||||
}
|
||||
//package com.recovery.admin.boot.config.ds;
|
||||
//
|
||||
//import com.alibaba.druid.pool.DruidDataSource;
|
||||
//import org.apache.ibatis.session.SqlSessionFactory;
|
||||
//import org.mybatis.spring.SqlSessionFactoryBean;
|
||||
//import org.springframework.beans.factory.annotation.Qualifier;
|
||||
//import org.springframework.beans.factory.annotation.Value;
|
||||
//import org.springframework.context.annotation.Bean;
|
||||
//import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
|
||||
//import org.springframework.jdbc.datasource.DataSourceTransactionManager;
|
||||
//
|
||||
//import javax.sql.DataSource;
|
||||
//
|
||||
////@Configuration
|
||||
////// 扫描 Mapper 接口并容器管理
|
||||
////@MapperScan(basePackages = TeachingDataSourceConfig.PACKAGE, sqlSessionFactoryRef = "teachingSqlSessionFactory")
|
||||
//public class TeachingDataSourceConfig {
|
||||
// // 精确到 course 目录,以便跟其他数据源隔离
|
||||
// static final String PACKAGE = "com.recovery.admin.boot.mapper";
|
||||
// static final String MAPPER_LOCATION = "classpath:mapper/*.xml";
|
||||
//
|
||||
// @Value("${admin.datasource.url}")
|
||||
// private String url;
|
||||
//
|
||||
// @Value("${admin.datasource.username}")
|
||||
// private String user;
|
||||
//
|
||||
// @Value("${admin.datasource.password}")
|
||||
// private String password;
|
||||
//
|
||||
// @Value("${admin.datasource.driverClassName}")
|
||||
// private String driverClass;
|
||||
//
|
||||
// @Bean(name = "adminDataSource")
|
||||
// public DataSource cpdDataSource() {
|
||||
// DruidDataSource dataSource = new DruidDataSource();
|
||||
// dataSource.setDriverClassName(driverClass);
|
||||
// dataSource.setUrl(url);
|
||||
// dataSource.setUsername(user);
|
||||
// dataSource.setPassword(password);
|
||||
// return dataSource;
|
||||
// }
|
||||
//
|
||||
// @Bean(name = "adminTransactionManager")
|
||||
// public DataSourceTransactionManager courseTransactionManager() {
|
||||
// return new DataSourceTransactionManager(cpdDataSource());
|
||||
// }
|
||||
//
|
||||
// @Bean(name = "adminSqlSessionFactory")
|
||||
// public SqlSessionFactory cpdSqlSessionFactory(@Qualifier("adminDataSource") DataSource cpdDataSource)
|
||||
// throws Exception {
|
||||
// final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
|
||||
// sessionFactory.setDataSource(cpdDataSource);
|
||||
// sessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver()
|
||||
// .getResources(TeachingDataSourceConfig.MAPPER_LOCATION));
|
||||
// return sessionFactory.getObject();
|
||||
// }
|
||||
//}
|
||||
|
|
|
@ -1,20 +1,43 @@
|
|||
package com.recovery.common.base.ds;
|
||||
|
||||
/**
|
||||
* Created by RyanWang on 2019-07-24
|
||||
*/
|
||||
|
||||
* @ClassName: DataSourceContextHolder
|
||||
* @Description: 数据源上下文
|
||||
* @Author:
|
||||
* @Date: 2022/2/18 10:04
|
||||
**/
|
||||
public class DataSourceContextHolder {
|
||||
private static final ThreadLocal<String> contextHolder = new ThreadLocal<String>();
|
||||
|
||||
/**
|
||||
* @MonthName: setDBType
|
||||
* @Description: 设置当前线程持有的数据源
|
||||
* @Author:
|
||||
* @Date: 2022/2/18 10:07
|
||||
* @Param: [dbType]
|
||||
* @return: void
|
||||
**/
|
||||
public static synchronized void setDBType(String dbType){
|
||||
contextHolder.set(dbType);
|
||||
}
|
||||
|
||||
/**
|
||||
* @MonthName: getDBType
|
||||
* @Description: 获取当前线程持有的数据源
|
||||
* @Author: tanyp
|
||||
* @Date: 2022/2/18 10:07
|
||||
* @Param: []
|
||||
* @return: java.lang.String
|
||||
**/
|
||||
public static String getDBType(){
|
||||
return contextHolder.get();
|
||||
}
|
||||
|
||||
/**
|
||||
* @MonthName: clearDBType
|
||||
* @Description: 清除当前线程持有的数据源
|
||||
* @Author: tanyp
|
||||
* @Date: 2022/2/18 10:07
|
||||
* @Param: []
|
||||
* @return: void
|
||||
**/
|
||||
public static void clearDBType(){
|
||||
contextHolder.remove();
|
||||
}
|
||||
|
|
|
@ -70,9 +70,9 @@ public class HspHostUtil {
|
|||
//log.info("maxWait : " + EnvironmentConfig.getProperty("master.datasource.maxWait"));
|
||||
|
||||
|
||||
// dynamicDataSource.setBreakAfterAcquireFailure(true);//失败之后中断
|
||||
// dynamicDataSource.setConnectionErrorRetryAttempts(3);//失败后重试次数
|
||||
// dynamicDataSource.setMaxWait(3000);//超时时间
|
||||
dynamicDataSource.setBreakAfterAcquireFailure(true);//失败之后中断
|
||||
dynamicDataSource.setConnectionErrorRetryAttempts(3);//失败后重试次数
|
||||
dynamicDataSource.setMaxWait(3000);//超时时间
|
||||
dataSourceMap.put(dataSourceKey, dynamicDataSource);
|
||||
DynamicDataSource.getInstance().setTargetDataSources(dataSourceMap);
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue