介绍下mysql数据库读写分离在spring,
hibernate框架下的配置。
1.mysql连接配置文件jdbc.properties
master.*.*表示主数据库连接参数,负责增,删,改;
slave.*.*表示从数据库连接参数,只负责读取;
jdbc.properties
master.jdbc.driverclassname=com.mysql.jdbc.drivermaster.jdbc.url=********master.jdbc.username=********master.jdbc.password=********slave.jdbc.driverclassname=com.mysql.jdbc.driverslave.jdbc.url=********slave.jdbc.username=********slave.jdbc.password=********
把**改成你所需的连接参数;
2.配置aop切面类 datasourceadvice.java
import java.lang.reflect.method;import org.springframework.aop.afterreturningadvice;import org.springframework.aop.methodbeforeadvice;import org.springframework.aop.throwsadvice;import com.company.datasource.datasourceswitcher;public class datasourceadvice implements methodbeforeadvice, afterreturningadvice, throwsadvice { // service方法执行之前被调用 public void before(method method, object[] args, object target) throws throwable { system.out.println("切入点: " + target.getclass().getname() + "类中" + method.getname() + "方法"); if(method.getname().startswith("add") || method.getname().startswith("create") || method.getname().startswith("save") || method.getname().startswith("edit") || method.getname().startswith("update") || method.getname().startswith("delete") || method.getname().startswith("remove")){ system.out.println("切换到: master"); datasourceswitcher.setmaster(); } else { system.out.println("切换到: slave"); datasourceswitcher.setslave(); } } // service方法执行完之后被调用 public void afterreturning(object arg0, method method, object[] args, object target) throws throwable { } // 抛出exception之后被调用 public void afterthrowing(method method, object[] args, object target, exception ex) throws throwable { datasourceswitcher.setslave(); system.out.println("出现异常,切换到: slave"); }}
数据源选择类 datasourceswitcher.java
package com.company.datasource;import org.springframework.util.assert;public class datasourceswitcher { @suppresswarnings("rawtypes") private static final threadlocal contextholder = new threadlocal(); @suppresswarnings("unchecked") public static void setdatasource(string datasource) { assert.notnull(datasource, "datasource cannot be null"); contextholder.set(datasource); } public static void setmaster(){ cleardatasource(); } public static void setslave() { setdatasource("slave"); } public static string getdatasource() { return (string) contextholder.get(); } public static void cleardatasource() { contextholder.remove(); }}
dynamicdatasource.java数据源动态切换类
package com.company.datasource;import org.springframework.jdbc.datasource.lookup.abstractroutingdatasource;public class dynamicdatasource extends abstractroutingdatasource { @override protected object determinecurrentlookupkey() { return datasourceswitcher.getdatasource(); }}
下面配置spring applicationcontext.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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" 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/tx http://www.springframework.org/schema/tx/spring-tx-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/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <context:annotation-config /> <!-- 自动加载service dao action --> <context:component-scan base-package="cn.com.company.dao.*" /> <context:component-scan base-package="cn.com.company.service.*" /> <context:component-scan base-package="cn.com.company.action" /> <!-- 加载properties配置文件 --> <bean id="propertyconfigurer" class="org.springframework.beans.factory.config.propertyplaceholderconfigurer"> <property name="locations"> <list> <value>classpath:jdbc.properties</value> </list> </property> </bean> <bean id="parentdatasource" class="com.mchange.v2.c3p0.combopooleddatasource"> //***c3p0配置 </bean> <!-- 主数据源--> <bean id="masterdatasource" parent="parentdatasource"> <property name="driverclass" value="${master.jdbc.driverclassname}" /> <property name="jdbcurl" value="${master.jdbc.url}" /> <property name="user" value="${master.jdbc.username}" /> <property name="password" value="${master.jdbc.password}" /> </bean> <!-- 从数据源--> <bean id="slavedatasource" parent="parentdatasource"> <property name="driverclass" value="${slave.jdbc.driverclassname}" /> <property name="jdbcurl" value="${slave.jdbc.url}" /> <property name="user" value="${slave.jdbc.username}" /> <property name="password" value="${slave.jdbc.password}" /> </bean> <bean id="datasource" class="com.company.datasource.dynamicdatasource"> <property name="targetdatasources"> <map key-type="java.lang.string"> <entry key="slave" value-ref="slavedatasource" /> </map> </property> <property name="defaulttargetdatasource" ref="masterdatasource" /> </bean> <!-- 配置sessionfactory --> <bean id="sessionfactory" class="org.springframework.orm.hibernate3.annotation.annotationsessionfactorybean"> <property name="datasource" ref="datasource"></property> <property name="packagestoscan" value="cn.com.company.entity" /> <property name="hibernateproperties"> <props> <prop>//***hibernate一些参数这里不写了</prop> </props> </property> </bean> <!-- 切换数据源 --> <bean id="datasourceadvice" class="com.company.aop.datasourceadvice" /> <aop:config> <aop:advisor pointcut="execution(* cn.com.company.service..*service.*(..))" advice-ref="datasourceadvice" /> </aop:config> <!-- 配置事务管理器 --> <bean id="transactionmanager" class="org.springframework.orm.hibernate3.hibernatetransactionmanager"> <property name="sessionfactory"> <ref bean="sessionfactory" /> </property> </bean> <!--配置事务的传播特性 --> <tx:advice id="txadvice" transaction-manager="transactionmanager"> <tx:attributes> <!-- 对增、删、改方法进行事务支持 --> <tx:method name="add*" propagation="required" /> <tx:method name="create*" propagation="required" /> <tx:method name="save*" propagation="required" /> <tx:method name="edit*" propagation="required" /> <tx:method name="update*" propagation="required" /> <tx:method name="delete*" propagation="required" /> <tx:method name="remove*" propagation="required" /> <!-- 对查找方法进行只读事务 --> <tx:method name="loadbyusername*" propagation="supports" read-only="true" /> <!-- 对其它方法进行只读事务 --> <tx:method name="*" propagation="supports" read-only="true" /> </tx:attributes> </tx:advice> <!--那些类的哪些方法参与事务 --> <aop:config> <aop:advisor pointcut="execution(* cn.com.company.service..*service.*(..))" advice-ref="txadvice" /> <aop:advisor pointcut="execution(* cn.com.company.service..*serviceimpl.*(..))" advice-ref="txadvice" /> </aop:config> </beans>
!注 applicationcontenxt.xml中
<!-- 切换数据源 -->
<bean id="datasourceadvice" class="com.company.aop.datasourceadvice" />
<aop:config>
<aop:advisor
pointcut="execution(* cn.com.company.service..*service.*(..))"
advice-ref="datasourceadvice" />
</aop:config>
一定要配置在事务aop之上