1算墨、application.properties中配置多數(shù)據(jù)源
# qss數(shù)據(jù)庫
datasource.qss.driver-class-name=com.mysql.jdbc.Driver
datasource.qss.url=jdbc:mysql://ip:3306/qssdb?autoReconnectForPools=true&useUnicode=true&characterEncoding=utf8&autoReconnect=true&failOverReadOnly=false
datasource.qss.username=***
datasource.qss.password=***
# foc測試數(shù)據(jù)庫
datasource.foc.driver-class-name=oracle.jdbc.driver.OracleDriver
datasource.foc.url=jdbc:oracle:thin:@ip:ora817
datasource.foc.username=***
datasource.foc.password=***
2.application.properties中 配置主數(shù)據(jù)源MySQL連接池(一定要注意連接池配置的前綴吊骤,此處用于配置qss數(shù)據(jù)源)
#參考:http://blog.csdn.net/catoop/article/details/50507516
#我們使用Tomcat數(shù)據(jù)源連接池废岂,需要依賴tomcat-jdbc,只要應(yīng)用中添加了spring-boot-starter-jdbc 或 spring-boot-starter-data-jpa依賴唬滑,則無需擔(dān)心這點(diǎn)司蔬,因?yàn)閷詣?dòng)添加 tomcat-jdbc 依賴。
#假如我們想用其他方式的連接池技術(shù)蛋叼,只要配置自己的DataSource bean,即可覆蓋Spring Boot的自動(dòng)配置剂陡。
datasource.qss.max-idle=10
# Number of ms to wait before throwing an exception if no connection is available.
datasource.qss.max-wait=10000
datasource.qss.min-idle=5
datasource.qss.initial-size=5
# Maximum number of active connections that can be allocated from this pool at the same time.
datasource.qss.max-active=100
datasource.qss.validation-query=SELECT 1
#使用testOnBorrow為true(即在獲取Connection對象時(shí)檢測其可用性)狈涮,不過這樣會影響性能
# Validate the connection before borrowing it from the pool.
datasource.qss.test-on-borrow=true
#有些數(shù)據(jù)庫連接的時(shí)候有超時(shí)限制(mysql連接在8小時(shí)后斷開),或者由于網(wǎng)絡(luò)中斷等原因鸭栖,連接池的連接會出現(xiàn)失效的情況歌馍,這時(shí)候設(shè)置一個(gè)testWhileIdle參數(shù)為true,可以保證連接池內(nèi)部定時(shí)檢測連接的可用性纤泵,不可用的連接會被拋棄或者重建骆姐,最大情況的保證從連接池中得到的Connection對象是可用的镜粤。
datasource.qss.test-while-idle=true
#設(shè)置當(dāng)連接被歸還時(shí)捏题,是否要提交所有還未完成的事務(wù)
datasource.qss.test-on-return=false
#如果當(dāng)前連接池中某個(gè)連接在空閑了timeBetweenEvictionRunsMillis時(shí)間后任然沒有使用,則被物理性的關(guān)閉掉肉渴。
datasource.qss.time-between-eviction-runs-millis=60000
# 配置一個(gè)連接在池中最小生存的時(shí)間公荧,單位是毫秒
datasource.qss.min-evictable-idle-time-millis=300000
datasource.qss.jdbc-interceptors=ConnectionState;SlowQueryReport(threshold=0)
3. 配置其他數(shù)據(jù)源連接池(有多少個(gè)配置多少個(gè))
datasource.foc.max-idle=10
datasource.foc.max-wait=10000
datasource.foc.min-idle=5
datasource.foc.initial-size=5
datasource.foc.max-active=100
#SELECT 1(MySQL) select 1 from dual(Oracle) SELECT 1(MySQL Server)。
datasource.foc.validation-query=select 1 from dual
datasource.foc.test-on-borrow=true
datasource.foc.test-while-idle=true
datasource.foc.test-on-return=false
datasource.foc.time-between-eviction-runs-millis=60000
datasource.foc.min-evictable-idle-time-millis=300000
datasource.foc.jdbc-interceptors=ConnectionState;SlowQueryReport(threshold=0)
4. DataSource Bean 主數(shù)據(jù)源(主數(shù)據(jù)源添加@Primary注解)
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(basePackages = {"cn.com.scal.qss.dao"}, entityManagerFactoryRef = "qssEntityManagerFactory",? transactionManagerRef="qssTransactionManager") //
// basePackages設(shè)置Repository所在位置
@ComponentScan
@PropertySource("classpath:application.properties")
public class QssRepositoryConfig {
@Bean(name = "qssDataSource")
@Qualifier("qssDataSource")
@Primary
@ConfigurationProperties(prefix="datasource.qss")
public DataSource qssDataSource() {
return DataSourceBuilder.create().build();
}
@Bean(name = "qssEntityManagerFactory")
@Primary
public EntityManagerFactory? qssEntityManagerFactory(DataSource qssDataSource) {
LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean();
factory.setDataSource(qssDataSource);
factory.setJpaVendorAdapter(new HibernateJpaVendorAdapter());
factory.setJpaProperties(additionalJpaProperties());
factory.setPersistenceUnitName("qssPersistent");
factory.setPackagesToScan("cn.com.scal.qss.domain");? ? //設(shè)置實(shí)體類所在位置
factory.afterPropertiesSet();
return factory.getObject();
}
@Bean(name = "qssTransactionManager")
@Primary
public PlatformTransactionManager qssTransactionManager(EntityManagerFactory qssEntityManagerFactory) {
JpaTransactionManager transactionManager = new JpaTransactionManager();
transactionManager.setEntityManagerFactory(qssEntityManagerFactory);
return transactionManager;
}
public Properties additionalJpaProperties(){
Properties properties = new Properties();
properties.setProperty("hibernate.hbm2ddl.auto", "update");
properties.setProperty("hibernate.dialect", "org.hibernate.dialect.MySQL5Dialect");
properties.setProperty("hibernate.show_sql", "true");
properties.setProperty("hibernate.format_sql", "true");
return properties;
}
}
5. DataSource Bean 其他數(shù)據(jù)源
@Configuration
@EnableTransactionManagement? ? ? ? // 只涉及查詢同规,暫不需要事務(wù)管理
@EnableJpaRepositories(basePackages = {"cn.com.scal.qss.foc.dao"}, entityManagerFactoryRef = "focEntityManagerFactory" ,? transactionManagerRef="focTransactionManager")
@ComponentScan
@PropertySource("classpath:application.properties")
public class FocRepositoryConfig {
@Bean(name = "focDataSource")
@Qualifier("focDataSource")
@ConfigurationProperties(prefix="datasource.foc")
public DataSource focDataSource() {
return DataSourceBuilder.create().build();
}
@Bean(name = "focEntityManagerFactory")
@Qualifier("focEntityManagerFactory")
public EntityManagerFactory focEntityManagerFactory(@Qualifier("focDataSource") DataSource focDataSource) {
LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean();
factory.setDataSource(focDataSource);
factory.setPersistenceProviderClass(HibernatePersistenceProvider.class);
factory.setJpaVendorAdapter(new HibernateJpaVendorAdapter());
factory.setJpaProperties(additionalJpaProperties());
factory.setPersistenceUnitName("focPersistent");
factory.setPackagesToScan("cn.com.scal.qss.foc.domain");
factory.afterPropertiesSet();
return factory.getObject();
}
@Bean(name = "focTransactionManager")
@Qualifier("focTransactionManager")
public PlatformTransactionManager focTransactionManager(@Qualifier("focEntityManagerFactory") EntityManagerFactory focEntityManagerFactory) {
JpaTransactionManager transactionManager = new JpaTransactionManager();
transactionManager.setEntityManagerFactory(focEntityManagerFactory);
// JpaTransactionManager transactionManager = new JpaTransactionManager(focEntityManagerFactory);
return transactionManager;
}
public Properties additionalJpaProperties(){
Properties properties = new Properties();
// properties.setProperty("hibernate.hbm2ddl.auto", "update");
properties.setProperty("hibernate.dialect", "org.hibernate.dialect.Oracle10gDialect");
properties.setProperty("hibernate.show_sql", "true");
properties.setProperty("hibernate.format_sql", "true");
return properties;
}
}
6. finally it works!!!
注意事項(xiàng):
1循狰、每個(gè)數(shù)據(jù)源都必須要有自己的數(shù)據(jù)庫連接池配置,使用前綴區(qū)分券勺;
2绪钥、數(shù)據(jù)庫連接驗(yàn)證sql查詢語句validation-query區(qū)分?jǐn)?shù)據(jù)庫
#SELECT 1(MySQL) select 1 from dual(Oracle) SELECT 1(MySQL Server)。
3关炼、EntityManager不能配置在配置文件中(單例)程腹,不許要在使用的時(shí)候才創(chuàng)建,使用完成后關(guān)閉儒拂。