package com.gkhy.safePlatform.config.database;
|
|
import com.atomikos.icatch.jta.UserTransactionImp;
|
import com.atomikos.icatch.jta.UserTransactionManager;
|
import com.mysql.cj.jdbc.MysqlXADataSource;
|
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.boot.jta.atomikos.AtomikosDataSourceBean;
|
import org.springframework.context.annotation.Bean;
|
import org.springframework.context.annotation.Configuration;
|
import org.springframework.context.annotation.Primary;
|
import org.springframework.core.env.Environment;
|
import org.springframework.stereotype.Component;
|
import org.springframework.transaction.jta.JtaTransactionManager;
|
|
import javax.sql.DataSource;
|
import javax.sql.XADataSource;
|
import javax.transaction.UserTransaction;
|
import java.util.Properties;
|
|
/**
|
* Druid配置
|
*
|
*
|
*/
|
@Component
|
@Configuration
|
public class DruidConfig {
|
|
|
@Autowired
|
private DataCourceProperties dataCourceProperties;
|
|
@Bean(name = "dsAccount")
|
@Primary
|
@Autowired
|
public DataSource dsAccount(Environment env) {
|
final MysqlXADataSource mysqlXADataSource = new MysqlXADataSource();
|
Properties prop = build(env, "spring.datasource.account.");
|
mysqlXADataSource.setUrl(dataCourceProperties.getAccount().getUrl());
|
mysqlXADataSource.setUser(dataCourceProperties.getAccount().getUsername());
|
mysqlXADataSource.setPassword(dataCourceProperties.getAccount().getPassword());
|
final AtomikosDataSourceBean ds = new AtomikosDataSourceBean();
|
ds.setXaDataSource(mysqlXADataSource);
|
ds.setUniqueResourceName("account");
|
ds.setPoolSize(dataCourceProperties.getAccount().getInitialSize());
|
ds.setMinPoolSize(dataCourceProperties.getAccount().getMinIdle());
|
ds.setMaxPoolSize(dataCourceProperties.getAccount().getMaxActive());
|
ds.setXaProperties(prop);
|
ds.setMaxIdleTime(3600);
|
ds.setMaxLifetime(3000);
|
ds.setTestQuery(dataCourceProperties.getAccount().getValidationQuery());
|
return ds;
|
|
}
|
|
|
@Autowired
|
@Primary
|
@Bean(name = "dsDoublePrevent")
|
public AtomikosDataSourceBean dsDoublePrevent(Environment env) {
|
final MysqlXADataSource mysqlXADataSource = new MysqlXADataSource();
|
mysqlXADataSource.setUrl(dataCourceProperties.getDoubleprevent().getUrl());
|
mysqlXADataSource.setUser(dataCourceProperties.getDoubleprevent().getUsername());
|
mysqlXADataSource.setPassword(dataCourceProperties.getDoubleprevent().getPassword());
|
final AtomikosDataSourceBean ds = new AtomikosDataSourceBean();
|
Properties prop = build(env, "spring.datasource.doubleprevent.");
|
ds.setXaDataSource(mysqlXADataSource);
|
ds.setUniqueResourceName("doubleprevent");
|
ds.setPoolSize(dataCourceProperties.getDoubleprevent().getInitialSize());
|
ds.setMinPoolSize(dataCourceProperties.getDoubleprevent().getMinIdle());
|
ds.setMaxPoolSize(dataCourceProperties.getDoubleprevent().getMaxActive());
|
ds.setXaProperties(prop);
|
ds.setMaxIdleTime(3600);
|
ds.setMaxLifetime(3000);
|
ds.setTestQuery(dataCourceProperties.getDoubleprevent().getValidationQuery());
|
return ds;
|
}
|
|
@Autowired
|
@Primary
|
@Bean(name = "dsSafeCheck")
|
public AtomikosDataSourceBean dsSafeCheck(Environment env) {
|
final MysqlXADataSource mysqlXADataSource = new MysqlXADataSource();
|
mysqlXADataSource.setUrl(dataCourceProperties.getSafecheck().getUrl());
|
mysqlXADataSource.setUser(dataCourceProperties.getSafecheck().getUsername());
|
mysqlXADataSource.setPassword(dataCourceProperties.getSafecheck().getPassword());
|
final AtomikosDataSourceBean ds = new AtomikosDataSourceBean();
|
Properties prop = build(env, "spring.datasource.safecheck.");
|
ds.setXaDataSource(mysqlXADataSource);
|
ds.setUniqueResourceName("safecheck");
|
ds.setPoolSize(dataCourceProperties.getSafecheck().getInitialSize());
|
ds.setMinPoolSize(dataCourceProperties.getSafecheck().getMinIdle());
|
ds.setMaxPoolSize(dataCourceProperties.getSafecheck().getMaxActive());
|
ds.setXaProperties(prop);
|
ds.setMaxIdleTime(3600);
|
ds.setMaxLifetime(3000);
|
ds.setTestQuery(dataCourceProperties.getSafecheck().getValidationQuery());
|
return ds;
|
}
|
|
@Autowired
|
@Primary
|
@Bean(name = "dsSpecialWork")
|
public AtomikosDataSourceBean dsSpecialWork(Environment env) {
|
final MysqlXADataSource mysqlXADataSource = new MysqlXADataSource();
|
mysqlXADataSource.setUrl(dataCourceProperties.getSpecialwork().getUrl());
|
mysqlXADataSource.setUser(dataCourceProperties.getSpecialwork().getUsername());
|
mysqlXADataSource.setPassword(dataCourceProperties.getSpecialwork().getPassword());
|
final AtomikosDataSourceBean ds = new AtomikosDataSourceBean();
|
Properties prop = build(env, "spring.datasource.specialwork.");
|
ds.setXaDataSource(mysqlXADataSource);
|
ds.setUniqueResourceName("specialwork");
|
ds.setPoolSize(dataCourceProperties.getSpecialwork().getInitialSize());
|
ds.setMinPoolSize(dataCourceProperties.getSpecialwork().getMinIdle());
|
ds.setMaxPoolSize(dataCourceProperties.getSpecialwork().getMaxActive());
|
ds.setXaProperties(prop);
|
ds.setMaxIdleTime(3600);
|
ds.setMaxLifetime(3000);
|
ds.setTestQuery(dataCourceProperties.getSpecialwork().getValidationQuery());
|
return ds;
|
}
|
|
@Autowired
|
@Primary
|
@Bean(name = "dsRiskControl")
|
public AtomikosDataSourceBean dsRiskControl(Environment env) {
|
final MysqlXADataSource mysqlXADataSource = new MysqlXADataSource();
|
mysqlXADataSource.setUrl(dataCourceProperties.getRiskcontrol().getUrl());
|
mysqlXADataSource.setUser(dataCourceProperties.getRiskcontrol().getUsername());
|
mysqlXADataSource.setPassword(dataCourceProperties.getRiskcontrol().getPassword());
|
final AtomikosDataSourceBean ds = new AtomikosDataSourceBean();
|
Properties prop = build(env, "spring.datasource.riskcontrol.");
|
ds.setXaDataSource(mysqlXADataSource);
|
ds.setUniqueResourceName("riskcontrol");
|
ds.setPoolSize(dataCourceProperties.getRiskcontrol().getInitialSize());
|
ds.setMinPoolSize(dataCourceProperties.getRiskcontrol().getMinIdle());
|
ds.setMaxPoolSize(dataCourceProperties.getRiskcontrol().getMaxActive());
|
ds.setXaProperties(prop);
|
ds.setMaxIdleTime(3600);
|
ds.setMaxLifetime(3000);
|
ds.setTestQuery(dataCourceProperties.getRiskcontrol().getValidationQuery());
|
return ds;
|
}
|
|
|
/**
|
* 注入事物管理器
|
* @return
|
*/
|
@Bean(name = "xatx")
|
public JtaTransactionManager regTransactionManager () {
|
UserTransactionManager userTransactionManager = new UserTransactionManager();
|
UserTransaction userTransaction = new UserTransactionImp();
|
return new JtaTransactionManager(userTransaction, userTransactionManager);
|
}
|
|
|
private Properties build(Environment env, String prefix) {
|
|
Properties prop = new Properties();
|
prop.put("url", env.getProperty(prefix + "url"));
|
prop.put("username", env.getProperty(prefix + "username"));
|
prop.put("password", env.getProperty(prefix + "password"));
|
prop.put("driverClassName", env.getProperty(prefix + "driver-class-name", ""));
|
// prop.put("initialSize", env.getProperty(prefix + "initialSize", Integer.class));
|
// prop.put("maxActive", env.getProperty(prefix + "maxActive", Integer.class));
|
// prop.put("minIdle", env.getProperty(prefix + "minIdle", Integer.class));
|
// prop.put("maxWait", env.getProperty(prefix + "maxWait", Integer.class));
|
// prop.put("poolPreparedStatements", env.getProperty(prefix + "poolPreparedStatements", Boolean.class));
|
//
|
// prop.put("maxPoolPreparedStatementPerConnectionSize",
|
// env.getProperty(prefix + "maxPoolPreparedStatementPerConnectionSize", Integer.class));
|
//
|
// prop.put("maxPoolPreparedStatementPerConnectionSize",
|
// env.getProperty(prefix + "maxPoolPreparedStatementPerConnectionSize", Integer.class));
|
// prop.put("validationQuery", env.getProperty(prefix + "validationQuery"));
|
// prop.put("validationQueryTimeout", env.getProperty(prefix + "validationQueryTimeout", Integer.class));
|
// prop.put("testOnBorrow", env.getProperty(prefix + "testOnBorrow", Boolean.class));
|
// prop.put("testOnReturn", env.getProperty(prefix + "testOnReturn", Boolean.class));
|
// prop.put("testWhileIdle", env.getProperty(prefix + "testWhileIdle", Boolean.class));
|
// prop.put("timeBetweenEvictionRunsMillis",
|
// env.getProperty(prefix + "timeBetweenEvictionRunsMillis", Integer.class));
|
// prop.put("minEvictableIdleTimeMillis", env.getProperty(prefix + "minEvictableIdleTimeMillis", Integer.class));
|
// prop.put("filters", env.getProperty(prefix + "filters"));
|
|
return prop;
|
}
|
|
// @Bean
|
// public ServletRegistrationBean druidServlet() {
|
// ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(new StatViewServlet(), "/druid/*");
|
//
|
// //控制台管理用户,加入下面2行 进入druid后台就需要登录
|
// //servletRegistrationBean.addInitParameter("loginUsername", "admin");
|
// //servletRegistrationBean.addInitParameter("loginPassword", "admin");
|
// return servletRegistrationBean;
|
// }
|
|
// @Bean
|
// public FilterRegistrationBean filterRegistrationBean() {
|
// FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
|
// filterRegistrationBean.setFilter(new WebStatFilter());
|
// filterRegistrationBean.addUrlPatterns("/*");
|
// filterRegistrationBean.addInitParameter("exclusions", "*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*");
|
// filterRegistrationBean.addInitParameter("profileEnable", "true");
|
// return filterRegistrationBean;
|
// }
|
|
// @Bean
|
// public StatFilter statFilter(){
|
// StatFilter statFilter = new StatFilter();
|
// statFilter.setLogSlowSql(true); //slowSqlMillis用来配置SQL慢的标准,执行时间超过slowSqlMillis的就是慢。
|
// statFilter.setMergeSql(true); //SQL合并配置
|
// statFilter.setSlowSqlMillis(1000);//slowSqlMillis的缺省值为3000,也就是3秒。
|
// return statFilter;
|
// }
|
|
// @Bean
|
// public WallFilter wallFilter(){
|
// WallFilter wallFilter = new WallFilter();
|
// //允许执行多条SQL
|
// WallConfig config = new WallConfig();
|
// config.setMultiStatementAllow(true);
|
// wallFilter.setConfig(config);
|
// return wallFilter;
|
// }
|
|
}
|