hibernate mysql 读写分离_Spring+Hibernate框架下Mysql读写分离、主从数据库配置

1. 配置AOP切面类 DataSourceAdvice.java

package until;

import java.lang.reflect.Method;

import org.springframework.aop.AfterReturningAdvice;

import org.springframework.aop.MethodBeforeAdvice;

import org.springframework.aop.ThrowsAdvice;

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");

}

}

2.数据源选择类 DataSourceSwitcher.java

package until;

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();

}

}

3. DynamicDataSource.java数据源动态切换类

package until;

import java.sql.SQLFeatureNotSupportedException;

import java.util.logging.Logger;

import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;

public class DynamicDataSource extends AbstractRoutingDataSource {

@Override

protected Object determineCurrentLookupKey() {

return DataSourceSwitcher.getDataSource();

}

}

4. 下面配置spring applicationContext.xml文件

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xmlns:context="http://www.springframework.org/schema/context"

xmlns:tx="http://www.springframework.org/schema/tx"

xmlns:aop="http://www.springframework.org/schema/aop"

xsi:schemaLocation="http://www.springframework.org/schema/beans

http://www.springframework.org/schema/beans/spring-beans-2.5.xsd

http://www.springframework.org/schema/context

http://www.springframework.org/schema/context/spring-context-2.5.xsd

http://www.springframework.org/schema/tx

http://www.springframework.org/schema/tx/spring-tx-2.5.xsd

http://www.springframework.org/schema/aop

http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">

hbm/User.hbm.xml

org.hibernate.dialect.MySQLDialect

true

pointcut="execution(* service..*Service.*(..))"

advice-ref="dataSourceAdvice" />

pointcut="execution(* service..*Service.*(..))"

advice-ref="txAdvice" />

pointcut="execution(* service..*ServiceImpl.*(..))"

advice-ref="txAdvice" />

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值