content: package me.flyray.erp.config;
  
  import org.springframework.jdbc.datasource.DataSourceTransactionManager;
  import org.springframework.transaction.TransactionDefinition;
  
  /** 
  * @author: bolei
  * @date：2017年4月8日 下午10:39:18 
  * @description:
   */
  public class DynamicDataSourceTransactionManager extends DataSourceTransactionManager {
  
      /**
       * 只读事务到读库，读写事务到写库
       * @param transaction
       * @param definition
       */
      @Override
      protected void doBegin(Object transaction, TransactionDefinition definition) {
  
          //设置数据源
          boolean readOnly = definition.isReadOnly();
          if(readOnly) {
              DynamicDataSourceHolder.putDataSource(DynamicDataSourceGlobal.READ);
          } else {
              DynamicDataSourceHolder.putDataSource(DynamicDataSourceGlobal.WRITE);
          }
          super.doBegin(transaction, definition);
      }
  
      /**
       * 清理本地线程的数据源
       * @param transaction
       */
      @Override
      protected void doCleanupAfterCompletion(Object transaction) {
          super.doCleanupAfterCompletion(transaction);
          DynamicDataSourceHolder.clearDataSource();
      }
  }
  