This commit is contained in:
ytChen 2024-03-11 09:26:46 +08:00
parent 5d407ad65b
commit c3c02a9473
3 changed files with 90 additions and 67 deletions

View File

@ -1,58 +1,58 @@
package com.recovery.admin.boot.config.ds; //package com.recovery.admin.boot.config.ds;
//
import com.alibaba.druid.pool.DruidDataSource; //import com.alibaba.druid.pool.DruidDataSource;
import org.apache.ibatis.session.SqlSessionFactory; //import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean; //import org.mybatis.spring.SqlSessionFactoryBean;
import org.springframework.beans.factory.annotation.Qualifier; //import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value; //import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean; //import org.springframework.context.annotation.Bean;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver; //import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager; //import org.springframework.jdbc.datasource.DataSourceTransactionManager;
//
import javax.sql.DataSource; //import javax.sql.DataSource;
//
//@Configuration ////@Configuration
//// 扫描 Mapper 接口并容器管理 ////// 扫描 Mapper 接口并容器管理
//@MapperScan(basePackages = TeachingDataSourceConfig.PACKAGE, sqlSessionFactoryRef = "teachingSqlSessionFactory") ////@MapperScan(basePackages = TeachingDataSourceConfig.PACKAGE, sqlSessionFactoryRef = "teachingSqlSessionFactory")
public class TeachingDataSourceConfig { //public class TeachingDataSourceConfig {
// 精确到 course 目录以便跟其他数据源隔离 // // 精确到 course 目录以便跟其他数据源隔离
static final String PACKAGE = "com.recovery.admin.boot.mapper"; // static final String PACKAGE = "com.recovery.admin.boot.mapper";
static final String MAPPER_LOCATION = "classpath:mapper/*.xml"; // static final String MAPPER_LOCATION = "classpath:mapper/*.xml";
//
@Value("${admin.datasource.url}") // @Value("${admin.datasource.url}")
private String url; // private String url;
//
@Value("${admin.datasource.username}") // @Value("${admin.datasource.username}")
private String user; // private String user;
//
@Value("${admin.datasource.password}") // @Value("${admin.datasource.password}")
private String password; // private String password;
//
@Value("${admin.datasource.driverClassName}") // @Value("${admin.datasource.driverClassName}")
private String driverClass; // private String driverClass;
//
@Bean(name = "adminDataSource") // @Bean(name = "adminDataSource")
public DataSource cpdDataSource() { // public DataSource cpdDataSource() {
DruidDataSource dataSource = new DruidDataSource(); // DruidDataSource dataSource = new DruidDataSource();
dataSource.setDriverClassName(driverClass); // dataSource.setDriverClassName(driverClass);
dataSource.setUrl(url); // dataSource.setUrl(url);
dataSource.setUsername(user); // dataSource.setUsername(user);
dataSource.setPassword(password); // dataSource.setPassword(password);
return dataSource; // return dataSource;
} // }
//
@Bean(name = "adminTransactionManager") // @Bean(name = "adminTransactionManager")
public DataSourceTransactionManager courseTransactionManager() { // public DataSourceTransactionManager courseTransactionManager() {
return new DataSourceTransactionManager(cpdDataSource()); // return new DataSourceTransactionManager(cpdDataSource());
} // }
//
@Bean(name = "adminSqlSessionFactory") // @Bean(name = "adminSqlSessionFactory")
public SqlSessionFactory cpdSqlSessionFactory(@Qualifier("adminDataSource") DataSource cpdDataSource) // public SqlSessionFactory cpdSqlSessionFactory(@Qualifier("adminDataSource") DataSource cpdDataSource)
throws Exception { // throws Exception {
final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean(); // final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
sessionFactory.setDataSource(cpdDataSource); // sessionFactory.setDataSource(cpdDataSource);
sessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver() // sessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver()
.getResources(TeachingDataSourceConfig.MAPPER_LOCATION)); // .getResources(TeachingDataSourceConfig.MAPPER_LOCATION));
return sessionFactory.getObject(); // return sessionFactory.getObject();
} // }
} //}

View File

@ -1,20 +1,43 @@
package com.recovery.common.base.ds; 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 { public class DataSourceContextHolder {
private static final ThreadLocal<String> contextHolder = new ThreadLocal<String>(); 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){ public static synchronized void setDBType(String dbType){
contextHolder.set(dbType); contextHolder.set(dbType);
} }
/**
* @MonthName getDBType
* @Description 获取当前线程持有的数据源
* @Author tanyp
* @Date 2022/2/18 10:07
* @Param []
* @return java.lang.String
**/
public static String getDBType(){ public static String getDBType(){
return contextHolder.get(); return contextHolder.get();
} }
/**
* @MonthName clearDBType
* @Description 清除当前线程持有的数据源
* @Author tanyp
* @Date 2022/2/18 10:07
* @Param []
* @return void
**/
public static void clearDBType(){ public static void clearDBType(){
contextHolder.remove(); contextHolder.remove();
} }

View File

@ -70,9 +70,9 @@ public class HspHostUtil {
//log.info("maxWait : " + EnvironmentConfig.getProperty("master.datasource.maxWait")); //log.info("maxWait : " + EnvironmentConfig.getProperty("master.datasource.maxWait"));
// dynamicDataSource.setBreakAfterAcquireFailure(true);//失败之后中断 dynamicDataSource.setBreakAfterAcquireFailure(true);//失败之后中断
// dynamicDataSource.setConnectionErrorRetryAttempts(3);//失败后重试次数 dynamicDataSource.setConnectionErrorRetryAttempts(3);//失败后重试次数
// dynamicDataSource.setMaxWait(3000);//超时时间 dynamicDataSource.setMaxWait(3000);//超时时间
dataSourceMap.put(dataSourceKey, dynamicDataSource); dataSourceMap.put(dataSourceKey, dynamicDataSource);
DynamicDataSource.getInstance().setTargetDataSources(dataSourceMap); DynamicDataSource.getInstance().setTargetDataSources(dataSourceMap);
} }