Commit c3599816 authored by eddie.woo's avatar eddie.woo

modify

parent 33d528dd
package pwc.taxtech.atms.common.datasource;
public class DataSourceConfig {
private String url;
private String username;
private String password;
private Integer clientTimeout;
private Integer threadNum;
private Integer transferFileType;
private boolean renameUploaded;
private Integer retryTimes;
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public Integer getClientTimeout() {
return clientTimeout;
}
public void setClientTimeout(Integer clientTimeout) {
this.clientTimeout = clientTimeout;
}
public Integer getThreadNum() {
return threadNum;
}
public void setThreadNum(Integer threadNum) {
this.threadNum = threadNum;
}
public Integer getTransferFileType() {
return transferFileType;
}
public void setTransferFileType(Integer transferFileType) {
this.transferFileType = transferFileType;
}
public boolean isRenameUploaded() {
return renameUploaded;
}
public void setRenameUploaded(boolean renameUploaded) {
this.renameUploaded = renameUploaded;
}
public Integer getRetryTimes() {
return retryTimes;
}
public void setRetryTimes(Integer retryTimes) {
this.retryTimes = retryTimes;
}
}
package pwc.taxtech.atms.common.datasource;
import com.alibaba.druid.pool.DruidDataSource;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.pool2.KeyedPooledObjectFactory;
import org.apache.commons.pool2.PooledObject;
import org.apache.commons.pool2.impl.DefaultPooledObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class DataSourceFactory implements KeyedPooledObjectFactory<String, DruidDataSource> {
private static final Logger logger = LoggerFactory.getLogger(DataSourceFactory.class);
private DataSourceConfig dataSourceConfig;
private static final String SYMBOL = "demo_db_name";
public DataSourceFactory(DataSourceConfig dataSourceConfig) {
this.dataSourceConfig = dataSourceConfig;
}
@Override
public PooledObject<DruidDataSource> makeObject(String s) throws Exception {
DruidDataSource dynamicDataSource = new DruidDataSource();
String url = StringUtils.replace(dataSourceConfig.getUrl(), SYMBOL, s);
dynamicDataSource.setUrl(url);
dynamicDataSource.setUsername(dataSourceConfig.getUsername());
dynamicDataSource.setPassword(dataSourceConfig.getPassword());
return new DefaultPooledObject<>(dynamicDataSource);
}
@Override
public void destroyObject(String s, PooledObject<DruidDataSource> pooledObject) throws Exception {
pooledObject.getObject().close();
}
@Override
public boolean validateObject(String s, PooledObject<DruidDataSource> pooledObject) {
try {
return pooledObject.getObject().isEnable();
} catch (Exception e) {
logger.error("Failed to validate DruidDataSource: ", e);
}
return false;
}
@Override
public void activateObject(String s, PooledObject<DruidDataSource> pooledObject) throws Exception {
}
@Override
public void passivateObject(String s, PooledObject<DruidDataSource> pooledObject) throws Exception {
}
}
package pwc.taxtech.atms.common.datasource;
public class ShardingContextHolder {
private static final ThreadLocal<String> contextHolder = new ThreadLocal<>();
/**
* 设置数据库名
*
* @param key key
*/
public static void setDataSourceKey(String key) {
contextHolder.set(key);
}
/**
* 获取数据库名
*
* @return key
*/
public static String getDataSourceKey() {
return contextHolder.get();
}
}
package pwc.taxtech.atms.service.impl;
import com.alibaba.druid.pool.DruidDataSource;
import com.beust.jcommander.ParameterException;
import com.google.common.collect.Maps;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.pool2.impl.GenericKeyedObjectPool;
import org.apache.commons.pool2.impl.GenericKeyedObjectPoolConfig;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
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.lookup.AbstractRoutingDataSource;
import org.springframework.stereotype.Service;
import pwc.taxtech.atms.common.datasource.DataSourceConfig;
import pwc.taxtech.atms.common.datasource.DataSourceFactory;
import pwc.taxtech.atms.common.datasource.ShardingContextHolder;
import javax.annotation.PostConstruct;
import javax.sql.DataSource;
@Service("DynamicDataSource")
public class DataSourcePoolService extends AbstractRoutingDataSource {
private GenericKeyedObjectPool<String, DruidDataSource> dataSourcePool;
@Value("${jdbc_url_demo}")
private String demoUrl;
@Value("${jdbc_user}")
private String jdbcUser;
@Value("${jdbc_password}")
private String jdbcPwd;
@PostConstruct
public void init() throws Exception {
DataSourceConfig config = new DataSourceConfig();
config.setUrl(demoUrl);
config.setUsername(jdbcUser);
config.setPassword(jdbcPwd);
GenericKeyedObjectPoolConfig poolConfig = new GenericKeyedObjectPoolConfig();
poolConfig.setTestOnBorrow(true);
poolConfig.setTestOnReturn(true);
dataSourcePool = new GenericKeyedObjectPool<>(new DataSourceFactory(config), poolConfig);
this.setTargetDataSources(Maps.newHashMap());
}
@Override
protected Object determineCurrentLookupKey() {
return ShardingContextHolder.getDataSourceKey();
}
@Override
protected DataSource determineTargetDataSource() {
try {
String key = ShardingContextHolder.getDataSourceKey();
DruidDataSource dataSource = dataSourcePool.borrowObject(key);
dataSourcePool.returnObject(key, dataSource);
return dataSource;
} catch (Exception e) {
throw new RuntimeException("determineTargetDataSource error.", e);
}
}
private void createDataSource(String key) {
if (StringUtils.isBlank(key)) {
throw new ParameterException("DataSource name is empty.");
}
try {
dataSourcePool.addObject(key);
} catch (Exception e) {
throw new RuntimeException("createDataSource error.", e);
}
}
@Bean
public SqlSessionFactory dynamicSqlSessionFactory(
@Qualifier("DynamicDataSource") DataSource dynamicDataSource)
throws Exception {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(dynamicDataSource);
bean.setMapperLocations(new PathMatchingResourcePatternResolver()
.getResources("classpath*:mapper/*.xml"));
return bean.getObject();
}
@Bean
public SqlSessionTemplate dynamicSqlSessionTemplate(
@Qualifier("dynamicSqlSessionFactory") SqlSessionFactory sqlSessionFactory)
throws Exception {
return new SqlSessionTemplate(sqlSessionFactory);
}
}
......@@ -2,6 +2,8 @@ jdbc_url=${jdbc_url}
jdbc_user=${jdbc_user}
jdbc_password=${jdbc_password}
jdbc_url_demo=${jdbc_url_demo}
mail_jdbc_url=${mail_jdbc_url}
mail_jdbc_user=${mail_jdbc_user}
mail_jdbc_password=${mail_jdbc_password}
......
......@@ -2,6 +2,8 @@ jdbc_url=jdbc:mysql://10.157.107.89:3306/tax_admin?useUnicode=true&amp;character
jdbc_user=root
jdbc_password=tax@Admin2018
jdbc_url_demo=jdbc:mysql://10.157.107.89:3306/demo_db_name?useUnicode=true&amp;characterEncoding=utf-8&amp;zeroDateTimeBehavior=convertToNull&amp;allowMultiQueries=true
mail_jdbc_url=jdbc:sqlserver://192.168.1.102:1434;DatabaseName=MAILMaster
mail_jdbc_user=sa
mail_jdbc_password=atmsunittestSQL
......
......@@ -2,6 +2,8 @@ jdbc_url=jdbc:sqlserver://192.168.1.102:1434;DatabaseName=QATaxAdmin8
jdbc_user=sa
jdbc_password=atmsunittestSQL
jdbc_url_demo=jdbc:mysql://10.157.107.89:3306/demo_db_name?useUnicode=true&amp;characterEncoding=utf-8&amp;zeroDateTimeBehavior=convertToNull&amp;allowMultiQueries=true
mail_jdbc_url=jdbc:sqlserver://192.168.1.102:1434;DatabaseName=MAILMaster
mail_jdbc_user=sa
mail_jdbc_password=atmsunittestSQL
......
......@@ -2,6 +2,8 @@ jdbc_url=jdbc:mysql://10.157.107.89:3306/tax_admin?useUnicode=true&amp;character
jdbc_user=root
jdbc_password=tax@Admin2018
jdbc_url_demo=jdbc:mysql://10.157.107.89:3306/demo_db_name?useUnicode=true&amp;characterEncoding=utf-8&amp;zeroDateTimeBehavior=convertToNull&amp;allowMultiQueries=true
mail_jdbc_url=jdbc:sqlserver://192.168.1.102:1434;DatabaseName=MAILMaster
mail_jdbc_user=sa
mail_jdbc_password=atmsunittestSQL
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment