Spring 多数据源声明式事务控制(PlatformTransactionManager )

 
首先是TransactionManager的配置:


	
		
            
		
	

	
		
			
		
	

	
		
		      
		
	

	
		
			
				
				
				
			
		
	

	
	
		
            		
		
	
	
	
	
        
        
	

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

接下来就是路由的问题, 其中RoutingContextHolder类内部使用一个ThreadLocal类用来指定db1, db2, db3等key值, RoutingTransactionManager类则根据当前线程中的key值取得对应的tx.
/**
* 用来存储路由到指定tx的Context
 * 
 */
@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类
 * 
 * @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);
  

你可能感兴趣的:(MVC+ORM)