对多数据源进行aop声明式事务管理

当在对数据库表进行横向切分(将一个表的数据拆分为到多个数据库中)之后, 在操作数据库的时候需要路由到合适的数据源, 这里我参照了Spring对多数据源的路由处理方式(http://blog.springsource.com/2007/01/23/dynamic-datasource-routing). 具体实现如下.
首先是TransactionManager的配置:


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">
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">





class="org.springframework.jdbc.datasource.DataSourceTransactionManager">





class="org.springframework.jdbc.datasource.DataSourceTransactionManager">





























这里用到了spring的tx和aop标签, 都是spring的参考手册中的典型配置. 当调用指定的packageName.method(..)方法的时候, 将采用事务拦截, 如果在对该方法的调用过程中出现了任何异常将导致事务回滚.

接下来就是路由的问题, 其中RoutingContextHolder类内部使用一个ThreadLocal类用来指定db1, db2, db3等key值, RoutingTransactionManager类则根据当前线程中的key值取得对应的tx.

/**
* 用来存储路由到指定tx的Context
*
* @since 2009-10-28 下午01:54:46
*/
@SuppressWarnings("unchecked")
public class RoutingContextHolder {
private static final ThreadLocal contextHolder = new ThreadLocal();

public static void setContext(T context) {
Validate.notNull(context, "必须指定路由的context");
contextHolder.set(context);
}

public static T getContext() {
return (T) contextHolder.get();
}
}


/**
* 根据给定的路由规则来路由到合适的tx类
*
* @since 2009-10-28 下午01:34:11
* @see RoutingContextHolder
*/
public class RoutingTransactionManager implements PlatformTransactionManager {
private Map targetTransactionManagers =
new HashMap();

/**
* 根据给定的规则获取指定的tx
*
* @return
*/
protected PlatformTransactionManager getTargetTransactionManager() {
Object context = RoutingContextHolder.getContext();
Validate.notNull(context, "必须指定路由的context");
return targetTransactionManagers.get(context);
}

public void setTargetTransactionManagers(Map 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的路由context
RoutingContextHolder.setContext("db1");
return manager1.publish(item);

你可能感兴趣的:(Spring)