对多数据源进行aop声明式事务管理
当在对数据库表进行横向切分(将一个表的数据拆分为到多个数据库中)之后, 在操作数据库的时候需要路由到合适的数据源, 这里我参照了Spring对多数据源的路由处理方式(http://blog.springsource.com/2007/01/23/dynamic-datasource-routing). 具体实现如下.
首先是TransactionManager的配置:
<?xml version="1.0" encoding="gb2312"?><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"xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd"><bean id="db1Tx"/></property></bean><bean id="db2Tx"/></property></bean><bean id="dbcTx"/></property></bean><bean id="routingTransactionManager" value-ref="db1Tx" /><entry key="db2" value-ref="db2Tx" /><entry key="dbc" value-ref="dbcTx" /></map></property></bean><!-- 配置事务回滚的场景 --><tx:advice id="txAdvice" transaction-manager="routingTransactionManager"><tx:attributes> <tx:method name="*" rollback-for="java.lang.Exception"/></tx:attributes></tx:advice><!-- 指定那些类的哪些方法参与事务 --><aop:config> <aop:advisor advice-ref="txAdvice" pointcut="execution(* com.mysoft.manager.Manager1.publish(..))"/> <aop:advisor advice-ref="txAdvice" pointcut="execution(* com.mysoft.manager.Manager2.audit(..))"/></aop:config></beans>
/*** 用来存储路由到指定tx的Context * * @since 2009-10-28 下午01:54:46 */@SuppressWarnings("unchecked")public class RoutingContextHolder<T> { private static final ThreadLocal contextHolder = new ThreadLocal(); public static <T> void setContext(T context) { Validate.notNull(context, "必须指定路由的context"); contextHolder.set(context); } public static <T> T getContext() { return (T) contextHolder.get(); }}
/*** 根据给定的路由规则来路由到合适的tx类 * * @since 2009-10-28 下午01:34:11 * @see RoutingContextHolder */public class RoutingTransactionManager implements PlatformTransactionManager { private Map<Object, PlatformTransactionManager> targetTransactionManagers = new HashMap<Object, PlatformTransactionManager>(); /** * 根据给定的规则获取指定的tx * * @return */ protected PlatformTransactionManager getTargetTransactionManager() { Object context = RoutingContextHolder.getContext(); Validate.notNull(context, "必须指定路由的context"); return targetTransactionManagers.get(context); } public void setTargetTransactionManagers(Map<Object, PlatformTransactionManager> targetTransactionManagers) { this.targetTransactionManagers = targetTransactionManagers; } public void commit(TransactionStatus status) throws TransactionException { getTargetTransactionManager().commit(status); } public TransactionStatus getTransaction(TransactionDefinition definition) throws TransactionException { return getTargetTransactionManager().getTransaction(definition); } public void rollback(TransactionStatus status) throws TransactionException { getTargetTransactionManager().rollback(status); }}
// 指定tx的路由contextRoutingContextHolder.setContext("db1");return manager1.publish(item);