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.beans.factory.annotation.Value;
|
import org.springframework.boot.jta.atomikos.AtomikosDataSourceBean;
|
import org.springframework.boot.web.servlet.FilterRegistrationBean;
|
import org.springframework.boot.web.servlet.ServletRegistrationBean;
|
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.transaction.UserTransaction;
|
import java.util.Properties;
|
|
/**
|
* Druid配置
|
*
|
*
|
*/
|
@Component
|
@Configuration
|
public class DruidConfig {
|
|
@Autowired
|
private DataCourceProperties dataCourceProperties;
|
|
@Bean(name = "dsGoalManage")
|
@Primary
|
@Autowired
|
public DataSource dsGoalManage(Environment env) {
|
final MysqlXADataSource mysqlXADataSource = new MysqlXADataSource();
|
Properties prop = build(env, "spring.datasource.goalmanage.");
|
mysqlXADataSource.setUrl(dataCourceProperties.getGoalmanage().getUrl());
|
mysqlXADataSource.setUser(dataCourceProperties.getGoalmanage().getUsername());
|
mysqlXADataSource.setPassword(dataCourceProperties.getGoalmanage().getPassword());
|
final AtomikosDataSourceBean ds = new AtomikosDataSourceBean();
|
ds.setXaDataSource(mysqlXADataSource);
|
ds.setUniqueResourceName("goalmanage");
|
ds.setPoolSize(dataCourceProperties.getGoalmanage().getInitialSize());
|
ds.setMinPoolSize(dataCourceProperties.getGoalmanage().getMinIdle());
|
ds.setMaxPoolSize(dataCourceProperties.getGoalmanage().getMaxActive());
|
ds.setXaProperties(prop);
|
ds.setMaxIdleTime(3600);
|
ds.setMaxLifetime(3000);
|
ds.setTestQuery(dataCourceProperties.getGoalmanage().getValidationQuery());
|
return ds;
|
}
|
|
|
@Autowired
|
@Primary
|
@Bean(name = "dsEmergency")
|
public AtomikosDataSourceBean dsEmergency(Environment env) {
|
final MysqlXADataSource mysqlXADataSource = new MysqlXADataSource();
|
Properties prop = build(env, "spring.datasource.emergency.");
|
mysqlXADataSource.setUrl(dataCourceProperties.getEmergency().getUrl());
|
mysqlXADataSource.setUser(dataCourceProperties.getEmergency().getUsername());
|
mysqlXADataSource.setPassword(dataCourceProperties.getEmergency().getPassword());
|
final AtomikosDataSourceBean ds = new AtomikosDataSourceBean();
|
ds.setXaDataSource(mysqlXADataSource);
|
ds.setUniqueResourceName("emergency");
|
ds.setPoolSize(dataCourceProperties.getEmergency().getInitialSize());
|
ds.setMinPoolSize(dataCourceProperties.getEmergency().getMinIdle());
|
ds.setMaxPoolSize(dataCourceProperties.getEmergency().getMaxActive());
|
ds.setXaProperties(prop);
|
ds.setMaxIdleTime(3600);
|
ds.setMaxLifetime(3000);
|
ds.setTestQuery(dataCourceProperties.getEmergency().getValidationQuery());
|
return ds;
|
}
|
|
@Autowired
|
@Primary
|
@Bean(name = "dsIncidentManage")
|
public AtomikosDataSourceBean dsIncidentManage(Environment env) {
|
final MysqlXADataSource mysqlXADataSource = new MysqlXADataSource();
|
Properties prop = build(env, "spring.datasource.incidentmanage.");
|
mysqlXADataSource.setUrl(dataCourceProperties.getIncidentmanage().getUrl());
|
mysqlXADataSource.setUser(dataCourceProperties.getIncidentmanage().getUsername());
|
mysqlXADataSource.setPassword(dataCourceProperties.getIncidentmanage().getPassword());
|
final AtomikosDataSourceBean ds = new AtomikosDataSourceBean();
|
ds.setXaDataSource(mysqlXADataSource);
|
ds.setUniqueResourceName("incidentmanage");
|
ds.setPoolSize(dataCourceProperties.getIncidentmanage().getInitialSize());
|
ds.setMinPoolSize(dataCourceProperties.getIncidentmanage().getMinIdle());
|
ds.setMaxPoolSize(dataCourceProperties.getIncidentmanage().getMaxActive());
|
ds.setXaProperties(prop);
|
ds.setMaxIdleTime(3600);
|
ds.setMaxLifetime(3000);
|
ds.setTestQuery(dataCourceProperties.getIncidentmanage().getValidationQuery());
|
return ds;
|
}
|
|
@Autowired
|
@Primary
|
@Bean(name = "dsEquipment")
|
public AtomikosDataSourceBean dsEquipment(Environment env) {
|
final MysqlXADataSource mysqlXADataSource = new MysqlXADataSource();
|
Properties prop = build(env, "spring.datasource.equipment.");
|
mysqlXADataSource.setUrl(dataCourceProperties.getEquipment().getUrl());
|
mysqlXADataSource.setUser(dataCourceProperties.getEquipment().getUsername());
|
mysqlXADataSource.setPassword(dataCourceProperties.getEquipment().getPassword());
|
final AtomikosDataSourceBean ds = new AtomikosDataSourceBean();
|
ds.setXaDataSource(mysqlXADataSource);
|
ds.setUniqueResourceName("equipment");
|
ds.setPoolSize(dataCourceProperties.getEquipment().getInitialSize());
|
ds.setMinPoolSize(dataCourceProperties.getEquipment().getMinIdle());
|
ds.setMaxPoolSize(dataCourceProperties.getEquipment().getMaxActive());
|
ds.setXaProperties(prop);
|
ds.setMaxIdleTime(3600);
|
ds.setMaxLifetime(3000);
|
ds.setTestQuery(dataCourceProperties.getEquipment().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;
|
}
|
|
}
|