<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd"> <!-- 配置數據源 --> <bean name="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close"> <property name="url" value="${MessagePush.jdbc_url}" /> <property name="username" value="${MessagePush.jdbc_username}" /> <property name="password" value="${MessagePush.jdbc_password}" /> <!-- 初始化鏈接大小 --> <property name="initialSize" value="0" /> <!-- 鏈接池最大使用鏈接數量 --> <property name="maxActive" value="${MessagePush.jdbc_maxActive}" /> <!-- 鏈接池最大空閒 --> <!--<property name="maxIdle" value="20" /> --> <!-- 鏈接池最小空閒 --> <property name="minIdle" value="0" /> <!-- 獲取鏈接最大等待時間 --> <property name="maxWait" value="60000" /> <!-- <property name="poolPreparedStatements" value="true" /> <property name="maxPoolPreparedStatementPerConnectionSize" value="33" /> --> <property name="validationQuery" value="${MessagePush.validationQuery}" /> <property name="testOnBorrow" value="true" /> <property name="testOnReturn" value="true" /> <property name="testWhileIdle" value="true" /> <!-- 配置間隔多久才進行一次檢測,檢測須要關閉的空閒鏈接,單位是毫秒 --> <property name="timeBetweenEvictionRunsMillis" value="60000" /> <!-- 配置一個鏈接在池中最小生存的時間,單位是毫秒 --> <property name="minEvictableIdleTimeMillis" value="25200000" /> <!-- 打開removeAbandoned功能 --> <property name="removeAbandoned" value="true" /> <!-- 1800秒,也就是30分鐘 --> <property name="removeAbandonedTimeout" value="1800" /> <!-- 關閉abanded鏈接時輸出錯誤日誌 --> <property name="logAbandoned" value="true" /> <!-- 監控數據庫 --> <!-- <property name="filters" value="stat" /> --> <property name="filters" value="mergeStat" /> </bean> <bean name="dataSource1" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close"> <property name="url" value="${MessagePush.jdbc_url1}" /> <property name="username" value="${MessagePush.jdbc_username1}" /> <property name="password" value="${MessagePush.jdbc_password1}" /> <!-- 初始化鏈接大小 --> <property name="initialSize" value="0" /> <!-- 鏈接池最大使用鏈接數量 --> <property name="maxActive" value="${MessagePush.jdbc_maxActive1}" /> <!-- 鏈接池最大空閒 --> <!--<property name="maxIdle" value="20" /> --> <!-- 鏈接池最小空閒 --> <property name="minIdle" value="0" /> <!-- 獲取鏈接最大等待時間 --> <property name="maxWait" value="60000" /> <!-- <property name="poolPreparedStatements" value="true" /> <property name="maxPoolPreparedStatementPerConnectionSize" value="33" /> --> <property name="validationQuery" value="${MessagePush.validationQuery1}" /> <property name="testOnBorrow" value="true" /> <property name="testOnReturn" value="true" /> <property name="testWhileIdle" value="true" /> <!-- 配置間隔多久才進行一次檢測,檢測須要關閉的空閒鏈接,單位是毫秒 --> <property name="timeBetweenEvictionRunsMillis" value="60000" /> <!-- 配置一個鏈接在池中最小生存的時間,單位是毫秒 --> <property name="minEvictableIdleTimeMillis" value="25200000" /> <!-- 打開removeAbandoned功能 --> <property name="removeAbandoned" value="true" /> <!-- 1800秒,也就是30分鐘 --> <property name="removeAbandonedTimeout" value="1800" /> <!-- 關閉abanded鏈接時輸出錯誤日誌 --> <property name="logAbandoned" value="true" /> <!-- 監控數據庫 --> <!-- <property name="filters" value="stat" /> --> <property name="filters" value="mergeStat" /> </bean> <bean id="dataSourceBu" class="com.roi.patrol.datasources.ThreadLocalRountingDataSource"> <property name="defaultTargetDataSource" ref="dataSource" /> <property name="targetDataSources"> <map key-type="java.lang.String"> <entry key="DATASOURCE1" value-ref="dataSource1" /> <entry key="DATASOURCE" value-ref="dataSource" /> <!-- 這裏還能夠加多個dataSource --> </map> </property> </bean> <!-- 配置hibernate session工廠 --> <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> <property name="dataSource" ref="dataSourceBu" /> <property name="hibernateProperties"> <props> <prop key="javax.persistence.validation.mode">${MessagePush.javax.persistence.validation.mode} </prop> <!-- web項目啓動時是否更新表結構 --> <prop key="hibernate.hbm2ddl.auto">${MessagePush.hibernate.hbm2ddl.auto}</prop> <!-- 系統使用的數據庫方言,也就是使用的數據庫類型 --> <prop key="hibernate.dialect">${MessagePush.hibernate.dialect}</prop> <!-- 是否打印Hibernate生成的SQL到控制檯 --> <prop key="hibernate.show_sql">${MessagePush.hibernate.show_sql}</prop> <!-- 是否格式化打印出來的SQL --> <prop key="hibernate.format_sql">${MessagePush.hibernate.format_sql}</prop> <prop key="hibernate.current_session_context_class">${MessagePush.hibernate.current_session_context_class} </prop> <!-- <prop key="hibernate.default_schema">${MessagePush.hibernate.default_schema}</prop> --> </props> </property> <!-- 自動掃描註解方式配置的hibernate類文件 --> <!-- <property name="packagesToScan"> <list> <value>com.roi.patrol.entity</value> </list> </property> --> <property name="mappingLocations"> <value>classpath:/com/roi/patrol/entity/**/mappings/*.hbm.xml</value> </property> </bean> <!-- 配置事務管理器 --> <bean name="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory"></property> </bean> <!-- 註解方式配置事物 --> <!-- <tx:annotation-driven transaction-manager="transactionManager" /> --> <!-- 攔截器方式配置事物 --> <tx:advice id="transactionAdvice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="save*" propagation="REQUIRED" /> <!-- <tx:method name="add*" propagation="REQUIRED" /> <tx:method name="create*" propagation="REQUIRED" /> <tx:method name="insert*" propagation="REQUIRED" /> --> <tx:method name="update*" propagation="REQUIRED" /> <tx:method name="merge*" propagation="REQUIRED" /> <tx:method name="delete*" propagation="REQUIRED" /> <tx:method name="enable*" propagation="REQUIRED" /> <tx:method name="disable*" propagation="REQUIRED" /> <!-- <tx:method name="remove*" propagation="REQUIRED" /> <tx:method name="put*" propagation="REQUIRED" /> <tx:method name="use*" propagation="REQUIRED"/> --> <!--hibernate4必須配置爲開啓事務 不然 getCurrentSession()獲取不到 --> <tx:method name="get*" propagation="REQUIRED" read-only="true" /> <tx:method name="count*" propagation="REQUIRED" read-only="true" /> <tx:method name="find*" propagation="REQUIRED" read-only="true" /> <tx:method name="list*" propagation="REQUIRED" read-only="true" /> <tx:method name="*" propagation="REQUIRED" read-only="true" /> </tx:attributes> </tx:advice> <!-- 切面,將事物用在哪些對象上 --> <aop:config> <aop:pointcut id="transactionPointcut" expression="execution(* com.roi.patrol.service.*.impl.*.*(..))" /> <aop:advisor pointcut-ref="transactionPointcut" advice-ref="transactionAdvice" /> </aop:config> </beans>
#The Base DB Config For H—MessagePush MessagePush.hibernate.dialect=org.hibernate.dialect.MySQL5Dialect MessagePush.driverClassName=com.mysql.jdbc.Driver MessagePush.validationQuery=SELECT 1 #MessagePush.jdbc_url=jdbc:mysql://192.168.2.200:3306/roiac?useUnicode=true&characterEncoding=UTF-8 MessagePush.jdbc_url=jdbc:mysql://120.76.222.174:3306/roiac?useUnicode=true&characterEncoding=UTF-8 MessagePush.jdbc_username=root #MessagePush.jdbc_password=123456 MessagePush.jdbc_password=css4995 MessagePush.jdbc_maxActive=50 MessagePush.hibernate.hbm2ddl.auto=none #MessagePush.hibernate.default_schema=roiac MessagePush.hibernate.show_sql=true MessagePush.hibernate.format_sql=true MessagePush.javax.persistence.validation.mode=none MessagePush.hibernate.current_session_context_class=org.springframework.orm.hibernate4.SpringSessionContext ####能源庫 #The Base DB Config For H—MessagePush MessagePush.driverClassName1=com.mysql.jdbc.Driver MessagePush.validationQuery1=SELECT 1 #MessagePush.jdbc_url1=jdbc:mysql://192.168.2.200:3306/roiac?useUnicode=true&characterEncoding=UTF-8 MessagePush.jdbc_url1=jdbc:mysql://120.76.222.174:3306/energy?useUnicode=true&characterEncoding=UTF-8 MessagePush.jdbc_username1=root #MessagePush.jdbc_password1=123456 MessagePush.jdbc_password1=css4995 MessagePush.jdbc_maxActive1=50
注意須要使用spring管理控制類的初始化操做 在spring的配置文件上加入,寫控制類的地址css
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd "> <context:annotation-config /> <!-- 引入屬性文件,config.properties位於src/main/resources目錄下 --> <context:property-placeholder location="config.properties" /> <!-- 自動掃描dao和service包(自動注入) --> <context:component-scan base-package="com.roi.patrol.main.task,com.roi.patrol.service,com.roi.patrol.dao" > </context:component-scan> <import resource="hibernateConfig.xml"/> </beans>
控制類java
package com.roi.patrol.datasources; import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource; /** * @Copyright:Copyright (c) 2017 - 2066 * @Company:湖南瑞翼能源股份有限公司 * @Title: * @Description: * @Author:simon * @Since:2017年4月10日 下午4:09:14 * @Version:1.1.0 */ public class ThreadLocalRountingDataSource extends AbstractRoutingDataSource { @Override protected Object determineCurrentLookupKey() { return DataSourceTypeManager.get(); } }
DataSourceTypeManager 類的信息mysql
package com.roi.patrol.datasources; /** * @Copyright:Copyright (c) 2017 - 2066 * @Company:湖南瑞翼能源股份有限公司 * @Title:數據源管理類 * @Description:經過 TheadLocal 來保存每一個線程選擇哪一個數據源的標誌(key) * @Author:simon * @Since:2017年4月10日 下午4:08:58 * @Version:1.1.0 */ public class DataSourceTypeManager { private static final ThreadLocal<String> dataSourceTypes = new ThreadLocal<String>(); public static String get() { return dataSourceTypes.get(); } public static void set(String dataSourceType) { dataSourceTypes.set(dataSourceType); } public static void clearDataSourceType() { dataSourceTypes.remove(); } }
咱們須要把以前鏈接數據庫的鏈接方式設置成默認方式,那麼以前的代碼就能夠不須要改變了。web
//DataSourceInstances.SOURCE 配置的鏈接名稱 DataSourceTypeManager.set(DataSourceInstances.SOURCE); //若是操做完畢,最好切換到默認的。