Java實現資料庫的讀寫分離
引言
1、讀寫分離:可以通過Spring提供的AbstractRoutingDataSource類,重寫determineCurrentLookupKey方法,實現動態切換資料來源的功能;讀寫分離可以有效減輕寫庫的壓力,又可以把查詢資料的請求分發到不同讀庫;
2、寫資料庫:當呼叫insert、update、delete及一些實時資料用到的庫;
3、讀資料庫:當呼叫select查詢資料用到的庫;
4、JaveWeb工程通過AbstractRoutingDataSource類實現讀寫分離;
一、jdbc.properties檔案配置讀寫資料來源
datasource.type=mysql
datasource.driverClassName=com.mysql.jdbc.Driver
datasource.username=root
#寫庫w.datasource.url=jdbc\:mysql\://127.0.0.1\:3306/ddt?characterEncoding\=utf-8w.datasource.password=write123
#讀庫r.datasource.url=jdbc\:mysql\://IP\:3306/ddt?characterEncoding\=utf-8r.datasource.password=read123
#連線池配置
c3p0.acquireIncrement=3
c3p0.acquireRetryAttempts=10
c3p0.acquireRetryDelay=1000
c3p0.initialPoolSize=20
c3p0.idleConnectionTestPeriod=3600
c3p0.testConnectionOnCheckout=true
c3p0.minPoolSize=10
c3p0.maxPoolSize=80
c3p0.maxStatements=100
c3p0.numHelperThreads=10
c3p0.maxIdleTime=10800
二、application.xml檔案
<?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:jee="http://www.springframework.org/schema/jee"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/jee
http://www.springframework.org/schema/jee/spring-jee-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task-3.0.xsd"> <!-- 使用annotation 自動註冊bean,並保證@Required,@Autowired的屬性被注入 -->
<context:component-scan base-package="com.eb3">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Service" />
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller" />
</context:component-scan>
<bean class="org.springframework.beans.factory.config.PreferencesPlaceholderConfigurer" >
<property name="ignoreResourceNotFound" value="true" />
<property name="properties" ref="configProperties" />
</bean>
<bean id="configProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
<property name="locations">
<list>
<value>classpath*:jdbc.properties</value>
</list>
</property>
</bean>
<context:property-placeholder location="classpath:jdbc.properties"/>
<!-- 定義Hibernate讀資料來源 -->
<bean id="dataSourceRead" class="com.mchange.v2.c3p0.ComboPooledDataSource"
destroy-method="close">
<property name="driverClass">
<value>${datasource.driverClassName}</value>
</property>
<property name="jdbcUrl">
<value>${r.datasource.url}</value>
</property>
<property name="user">
<value>${datasource.username}</value>
</property>
<property name="password">
<value>${r.datasource.password}</value>
</property>
<!--當連線池中的連線耗盡的時候c3p0一次同時獲取的連線數。-->
<property name="acquireIncrement">
<value>${c3p0.acquireIncrement}</value>
</property>
<!--定義在從資料庫獲取新連線失敗後重復嘗試的次數。-->
<property name="acquireRetryAttempts">
<value>${c3p0.acquireRetryAttempts}</value>
</property>
<!--兩次連線中間隔時間,單位毫秒。-->
<property name="acquireRetryDelay">
<value>${c3p0.acquireRetryDelay}</value>
</property>
<property name="initialPoolSize">
<value>${c3p0.initialPoolSize}</value>
</property>
<property name="testConnectionOnCheckout">
<value>${c3p0.testConnectionOnCheckout}</value>
</property>
<property name="minPoolSize">
<value>${c3p0.minPoolSize}</value>
</property>
<property name="maxPoolSize">
<value>${c3p0.maxPoolSize}</value>
</property>
<property name="maxIdleTime">
<value>${c3p0.maxIdleTime}</value>
</property>
<property name="idleConnectionTestPeriod">
<value>${c3p0.idleConnectionTestPeriod}</value>
</property>
<property name="maxStatements">
<value>${c3p0.maxStatements}</value>
</property>
<property name="numHelperThreads">
<value>${c3p0.numHelperThreads}</value>
</property>
</bean>
<!-- 定義Hibernate寫資料來源 -->
<bean id="dataSourceWrite" class="com.mchange.v2.c3p0.ComboPooledDataSource"
destroy-method="close">
<property name="driverClass">
<value>${datasource.driverClassName}</value>
</property>
<property name="jdbcUrl">
<value>${w.datasource.url}</value>
</property>
<property name="user">
<value>${datasource.username}</value>
</property>
<property name="password">
<value>${w.datasource.password}</value>
</property>
<!--當連線池中的連線耗盡的時候c3p0一次同時獲取的連線數。-->
<property name="acquireIncrement">
<value>${c3p0.acquireIncrement}</value>
</property>
<!--定義在從資料庫獲取新連線失敗後重復嘗試的次數。-->
<property name="acquireRetryAttempts">
<value>${c3p0.acquireRetryAttempts}</value>
</property>
<!--兩次連線中間隔時間,單位毫秒。-->
<property name="acquireRetryDelay">
<value>${c3p0.acquireRetryDelay}</value>
</property>
<property name="initialPoolSize">
<value>${c3p0.initialPoolSize}</value>
</property>
<property name="testConnectionOnCheckout">
<value>${c3p0.testConnectionOnCheckout}</value>
</property>
<property name="minPoolSize">
<value>${c3p0.minPoolSize}</value>
</property>
<property name="maxPoolSize">
<value>${c3p0.maxPoolSize}</value>
</property>
<property name="maxIdleTime">
<value>${c3p0.maxIdleTime}</value>
</property>
<property name="idleConnectionTestPeriod">
<value>${c3p0.idleConnectionTestPeriod}</value>
</property>
<property name="maxStatements">
<value>${c3p0.maxStatements}</value>
</property>
<property name="numHelperThreads">
<value>${c3p0.numHelperThreads}</value>
</property>
</bean>
<!-- 動態資料來源 -->
<bean id="dynamicDataSource" class="com.eb3.ddt.DynamicDataSource">
<!-- 通過key-value關聯資料來源 -->
<property name="targetDataSources">
<map>
<entry value-ref="dataSourceWrite" key="dataSourceWrite"></entry>
<entry value-ref="dataSourceRead" key="dataSourceRead"></entry>
</map>
</property>
<property name="defaultTargetDataSource" ref="dataSourceWrite" />
</bean>
<!-- 設定sessionFactory -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<!-- 依賴注入資料來源,注入正是上文定義的dataSource -->
<property name="dataSource" ref="dynamicDataSource" />
<property name="packagesToScan" value="com.eb3.ddt.pojo,com.eb3.loan.pojo"/>
<!--定義Hibernate的SessionFactory的屬性 -->
<property name="hibernateProperties">
<props>
<!-- 指定Hibernate的連線方言-->
<prop key="hibernate.dialect">
${hibernate.dialect}
</prop>
<prop key="hibernate.connection.autocommit">${hibernate.connection.autocommit}</prop>
<!-- 制定Hibernate是否列印SQL語句 -->
<prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
<prop key="hibernate.format_sql">${hibernate.format_sql}</prop>
<!-- 設create(啟動建立),create-drop(啟動建立,退出刪除),update(啟動更新),validate(啟動驗證) -->
<prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>
<prop key="connection.characterEncoding">utf-8</prop>
<!-- 設定二級快取 -->
<prop key="hibernate.cache.user_query_cache">${hibernate.cache.user_query_cache}</prop>
<prop key="hibernate.user_second_level_cache">${hibernate.user_second_level_cache}</prop>
<prop key="hibernate.cache.provider_class">${hibernate.cache.class}</prop>
<prop key="hibernate.cache.provider_configuration_file_resource_path">${hibernate.ehcache_config_file}</prop>
</props>
</property>
</bean>
<!-- 事務管理器配置,單資料來源事務 -->
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="save*" propagation="REQUIRED" rollback-for="Exception"/>
<tx:method name="add*" propagation="REQUIRED" rollback-for="Exception"/>
<tx:method name="delete*" propagation="REQUIRED" rollback-for="Exception"/>
<tx:method name="update*" propagation="REQUIRED" rollback-for="Exception"/>
<tx:method name="merge*" isolation="READ_COMMITTED" propagation="REQUIRED" rollback-for="Exception"/>
<tx:method name="get*" read-only="true"/>
<tx:method name="find*" read-only="true"/>
<tx:method name="list*" read-only="true"/>
<tx:method name="select*" read-only="true"/>
<tx:method name="*" propagation="REQUIRED" />
</tx:attributes>
</tx:advice>
<aop:config>
<aop:pointcut id="interceptorPointCuts" expression="execution(* com.eb3.*.service.*.*(..))" />
<aop:advisor advice-ref="txAdvice" pointcut-ref="interceptorPointCuts" />
</aop:config>
<aop:aspectj-autoproxy proxy-target-class="true" />
<!-- 定時器配置 task:[email protected]排程執行緒池的大小,排程執行緒在被排程任務完成前不會空閒 task:executor/@pool-size:可以指定執行執行緒池的初始大小、最大大小
task:executor/@queue-capacity:等待執行的任務佇列的容量 task:executor/@rejection-policy:當等待隊已滿時的策略,分為丟棄、由任務執行器直接執行等方式
@Async 非同步任務時 task任務執行執行緒數 task:scheduler 和 task:executor 兩個執行緒池同樣起作用 沒有非同步註解時
task任務執行執行緒數只受task:scheduler的執行緒池大小影響 -->
<!-- 宣告一個具有10個執行緒的池,每一個物件將獲取同樣的執行機會 -->
<task:scheduler id="scheduler" pool-size="10" />
<task:executor id="executor" keep-alive="3600" pool-size="100-300" queue-capacity="500" rejection-policy="CALLER_RUNS" />
<task:annotation-driven executor="executor" scheduler="scheduler" />
</beans>
三、繼承AbstractRoutingDataSource類的動態資料來源類DynamicDataSource
package com.eb3.ddt;
import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;
public class DynamicDataSource extends AbstractRoutingDataSource {
/**
* 重寫determineCurrentLookupKey方法
*/
@Override
protected Object determineCurrentLookupKey() {
Object obj = DBHelper.getDbType();
return obj;
}
}
四、DBHelper工具類
package com.eb3.ddt;
import org.apache.commons.lang.StringUtils;
public class DBHelper {
private static ThreadLocal<String> dbContext = new ThreadLocal<String>();
// 寫資料來源標識
public final static String DB_WRITE = "dataSourceWrite";
// 讀資料來源標識
public final static String DB_READ = "dataSourceRead";
/**
* 獲取資料來源型別,即是寫資料來源,還是讀資料來源
*
* @return
*/
public static String getDbType() {
String db_type = dbContext.get();
if (StringUtils.isEmpty(db_type)) {
// 預設是寫資料來源
db_type = DB_WRITE;
}
return db_type;
}
/**
* 設定該執行緒的資料來源型別
*
* @param str
*/
public static void setDbType(String str) {
dbContext.set(str);
}
}
五、服務層呼叫
/*@Aspect 此註解會影響資料來源切換,執行程式碼得知不加的話會先執行DynamicDataSource裡的determineCurrentLookupKey方法,後執行Service層裡DBHelper.setDbType()方法,導致資料來源切換失敗!*/
@Aspect
@Component("userService")public class UserServiceImpl extends BaseServiceImpl<User, User, Integer> implements UserService {
@Resource
private UserDao userDao;
@Override
protected BaseDao<User, Integer> getDao() {
return this.userDao;
}
@Override
public void save(User user) {
DBHelper.setDbType(DBHelper.DB_WRITE); // 寫庫 (向資料庫中寫)
this.userDao.save(user);
}
@Override
public User findByUserName(String username) {
DBHelper.setDbType(DBHelper.DB_READ); // 讀庫 (從資料庫中向外讀)
List<User> userList = this.userDao.findBy("username", username);
return CollectionUtils.isNotEmpty(userList) ? userList.get(0) : null;
}
}