package com.bonus.canteen.core.data.dataset.tx;

import com.bonus.canteen.core.data.dataset.rule.RoutingRule;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.TransactionDefinition;

import javax.sql.DataSource;

public class XDataSourceTransactionManager extends DataSourceTransactionManager {
   public XDataSourceTransactionManager(DataSource dataSource) {
      super(dataSource);
   }

   protected void doBegin(Object transaction, TransactionDefinition definition) {
      RoutingRule.accessMaster();
      super.doBegin(transaction, definition);
   }

   protected void doCleanupAfterCompletion(Object transaction) {
      try {
         super.doCleanupAfterCompletion(transaction);
      } finally {
         RoutingRule.resetAccess();
      }

   }
}
