@Transaction无法处理多数据源Spring Boot + MyBatis应用程序

时间:2017-08-29 14:00:47

标签: spring spring-boot transactions mybatis spring-mybatis

我正在尝试配置Spring Boot + MyBatis应用程序,该应用程序应该可以使用多个数据源。我试着这样做sample here

查询和更新数据是有效的,但是当我编写单元测试时,我发现@Transactional不起作用。事务必须在所有数据库之间工作这意味着,如果一个带有@Transactional的方法在两个数据库上进行更新,那么一切都应该在异常的情况下回滚。

这是一个用于测试目的和同事的示例应用程序。成功配置后,将以类似的方式配置和开发新应用程序。

的Maven:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.aze.mybatis</groupId>
    <artifactId>sample-one</artifactId>
    <version>1.0-SNAPSHOT</version>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.6.RELEASE</version>
    </parent>

    <dependencies>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.3.1</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>com.oracle</groupId>
            <artifactId>ojdbc6</artifactId>
            <version>11.2.0.4.0</version>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.5.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

以下是配置类:

package com.aze.mybatis.sampleone;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;

@SpringBootApplication
@EnableAutoConfiguration(exclude = {DataSourceAutoConfiguration.class})
public class Application {

    public static void main(String[] args) throws Exception {
        SpringApplication.run(Application.class, args);
    }

}

配置数据库BSCS(Oracle)

package com.aze.mybatis.sampleone.config;

import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import javax.sql.DataSource;


@Configuration
@MapperScan(basePackages = "com.aze.mybatis.sampleone.dao", annotationClass = BscsDataSource.class, sqlSessionFactoryRef = BscsDatabaseConfig.SQL_SESSION_FACTORY_NAME)
@EnableTransactionManagement
public class BscsDatabaseConfig {

    static final String SQL_SESSION_FACTORY_NAME = "sessionFactoryBscs";
    private static final String TX_MANAGER = "txManagerBscs";

    @Bean(name = "dataSourceBscs")
    @ConfigurationProperties(prefix = "bscs.datasource")
    public DataSource dataSource() {
        return DataSourceBuilder.create().build();
    }

    @Bean(name = TX_MANAGER)
    public PlatformTransactionManager txManagerBscs() {
        return new DataSourceTransactionManager(dataSource());
    }

    @Bean(name = BscsDatabaseConfig.SQL_SESSION_FACTORY_NAME)
    public SqlSessionFactory sqlSessionFactoryBean() throws Exception {
        SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
        sqlSessionFactoryBean.setDataSource(dataSource());
        return sqlSessionFactoryBean.getObject();
    }

}

配置数据库ONSUBS(Oracle)

package com.aze.mybatis.sampleone.config;

import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import javax.sql.DataSource;

@Configuration
@MapperScan(basePackages = "com.aze.mybatis.sampleone.dao", annotationClass = OnsubsDataSource.class, sqlSessionFactoryRef = OnsubsDatabaseConfig.SQL_SESSION_FACTORY_NAME)
@EnableTransactionManagement
public class OnsubsDatabaseConfig {

    static final String SQL_SESSION_FACTORY_NAME = "sessionFactoryOnsubs";
    private static final String TX_MANAGER = "txManagerOnsubs";

    @Bean(name = "dataSourceOnsubs")
    @Primary
    @ConfigurationProperties(prefix = "onsubs.datasource")
    public DataSource dataSource() {
        return DataSourceBuilder.create().build();
    }

    @Bean(name = TX_MANAGER)
    @Primary
    public PlatformTransactionManager txManagerOnsubs() {
        return new DataSourceTransactionManager(dataSource());
    }

    @Bean(name = OnsubsDatabaseConfig.SQL_SESSION_FACTORY_NAME)
    @Primary
    public SqlSessionFactory sqlSessionFactoryBean() throws Exception {
        SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
        sqlSessionFactoryBean.setDataSource(dataSource());
        return sqlSessionFactoryBean.getObject();
    }

}

BSCS注释:

package com.aze.mybatis.sampleone.config;

public @interface BscsDataSource {
}

和ONSUBS:

package com.aze.mybatis.sampleone.config;

public @interface OnsubsDataSource {
}

适用于ONSUBS的Mapper接口:

package com.aze.mybatis.sampleone.dao;

import com.aze.mybatis.sampleone.config.OnsubsDataSource;
import com.aze.mybatis.sampleone.domain.Payment;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;

@Mapper
@OnsubsDataSource
public interface PaymentDao {

    Payment getPaymentById(@Param("paymentId") Integer paymentId);

}

和BSCS:

package com.aze.mybatis.sampleone.dao;

import com.aze.mybatis.sampleone.config.BscsDataSource;
import com.aze.mybatis.sampleone.domain.PostpaidBalance;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;

@Mapper
@BscsDataSource
public interface PostpaidCustomerDao {

    PostpaidBalance getPostpaidBalance(@Param("customerId") Integer customerId);

    // BigDecimal amount may be used as second parameter, but I want to show, how to work with two parameters where second is object
    void updateDepositAmount(@Param("customerId") Integer customerId, @Param("balance") PostpaidBalance postpaidBalance);

    void updateAzFdlLastModUser(@Param("customerId") Integer customerId, @Param("username") String username);

}

以下是@Transactional

的代码
package com.aze.mybatis.sampleone.service;

import com.aze.mybatis.sampleone.dao.PaymentDao;
import com.aze.mybatis.sampleone.dao.PostpaidCustomerDao;
import com.aze.mybatis.sampleone.domain.Payment;
import com.aze.mybatis.sampleone.domain.PostpaidBalance;
import com.aze.mybatis.sampleone.exception.DataNotFoundException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;

@Service
public class PaymentServiceImpl implements PaymentService {

    private static final String MIN_DEPOSIT_AMOUNT = "150";

    @Autowired
    private PaymentDao paymentDao;

    @Autowired
    private PostpaidCustomerDao postpaidCustomerDao;

    @Override
    public PostpaidBalance getPostpaidBalance(Integer customerId) {
        PostpaidBalance balance = postpaidCustomerDao.getPostpaidBalance(customerId);

        if (balance == null) {
            throw new DataNotFoundException(String.format("Can't find any balance information for customer with customer_id = %d", customerId));
        }

        return balance;
    }

    // Note. By default rolling back on RuntimeException and Error but not on checked exceptions
    // If you want to rollback on check exception too then add "rollbackFor = Exception.class"
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void updateDepositAmount(Integer customerId, PostpaidBalance postpaidBalance, String username) {
        postpaidCustomerDao.updateDepositAmount(customerId, postpaidBalance);

        // In case of @Transactional annotation, you can use method from the same class if it doesn't change data on database
        PostpaidBalance balance = getPostpaidBalance(customerId);

        // This logic is for showing that how the @Transactional annotation works.
        // Because of the exception, the previous transaction will rollback
        if (balance.getDeposit().compareTo(new BigDecimal(MIN_DEPOSIT_AMOUNT)) == -1) {
            throw new IllegalArgumentException("The customer can not have deposit less than " + MIN_DEPOSIT_AMOUNT);
        }

        // In case of @Transactional annotation, you must not (!!!) use method from the same (!) class if it changes data on database
        // That is why, postpaidCustomerDao.updateAzFdlLastModUser() used here instead of this.updateAzFdlLastModUser()
        postpaidCustomerDao.updateAzFdlLastModUser(customerId, username);

        // If there is no exception, the transaction will commit
    }
}

以下是单元测试代码:

package com.aze.mybatis.sampleone.service;

import com.aze.mybatis.sampleone.Application;
import com.aze.mybatis.sampleone.config.BscsDatabaseConfig;
import com.aze.mybatis.sampleone.config.OnsubsDatabaseConfig;
import com.aze.mybatis.sampleone.domain.PostpaidBalance;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.TestPropertySource;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import java.math.BigDecimal;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = {Application.class, OnsubsDatabaseConfig.class, BscsDatabaseConfig.class})
@TestPropertySource(locations= "classpath:application.properties")
public class PaymentServiceImplTest extends Assert {


    // My goal is not to write a full and right unit tests, but just show you examples of working with MyBatis

    @Autowired
    private PaymentService paymentService;

    @Before
    public void setUp() throws Exception {
        assert paymentService != null;
    }

    @Test
    public void updateDepositAmount() throws Exception {
        final int customerId = 4301887; // not recommended way. Just for sample
        final String username = "ITCSC";
        boolean exceptionRaised = false;

        PostpaidBalance balance = paymentService.getPostpaidBalance(customerId);
        assertTrue("Find customer with deposit = 0", balance.getDeposit().compareTo(BigDecimal.ZERO) == 0);
        balance.setDeposit(BigDecimal.TEN);

        try {
            paymentService.updateDepositAmount(customerId, balance, username);
        } catch (Exception e) {
            exceptionRaised = true;
        }

        assertTrue(exceptionRaised);
        balance = paymentService.getPostpaidBalance(customerId);
        // We check that transaction was rollback and amount was not changed
        assertTrue(balance.getDeposit().compareTo(BigDecimal.ZERO) == 0);

        final BigDecimal minDepositAmount = new BigDecimal("150");
        balance.setDeposit(minDepositAmount);
        paymentService.updateDepositAmount(customerId, balance, username);

        balance = paymentService.getPostpaidBalance(customerId);
        assertTrue(balance.getDeposit().compareTo(minDepositAmount) != -1);
    }
}

assertTrue(balance.getDeposit().compareTo(BigDecimal.ZERO) == 0);上的单元测试失败。我检查数据库并看到尽管有@Transactional注释,但第一次更新postpaidCustomerDao.updateDepositAmount(customerId, postpaidBalance);没有回滚。

请帮助解决问题。

1 个答案:

答案 0 :(得分:0)

如果您有多个TransactionManager,则需要使用Bean名称或限定符引用要用于@Transactional的那个。

在您的Java Config中:

@Bean("myTM")
public PlatformTransactionManager transactionManager() {
    return new DataSourceTransactionManager(myDatasource());
}

在服务中:

@Transactional("myTM")
public void insertWithException(JdbcTemplate jdbcTemplate) {
}

我调试了我的应用程序,以查看Spring如何选择TransactionManager。这是在org.springframework.transaction.interceptor.TransactionAspectSupport#determineTransactionManager

中完成的
/**
 * Determine the specific transaction manager to use for the given transaction.
 */
protected PlatformTransactionManager determineTransactionManager(TransactionAttribute txAttr) {
    // Do not attempt to lookup tx manager if no tx attributes are set
    if (txAttr == null || this.beanFactory == null) {
        return getTransactionManager();
    }
    String qualifier = txAttr.getQualifier();
    if (StringUtils.hasText(qualifier)) {
        return determineQualifiedTransactionManager(qualifier);
    }
    else if (StringUtils.hasText(this.transactionManagerBeanName)) {
        return determineQualifiedTransactionManager(this.transactionManagerBeanName);
    }
    else {
        PlatformTransactionManager defaultTransactionManager = getTransactionManager();
        if (defaultTransactionManager == null) {
            defaultTransactionManager = this.transactionManagerCache.get(DEFAULT_TRANSACTION_MANAGER_KEY);
            if (defaultTransactionManager == null) {
                defaultTransactionManager = this.beanFactory.getBean(PlatformTransactionManager.class);
                this.transactionManagerCache.putIfAbsent(
                        DEFAULT_TRANSACTION_MANAGER_KEY, defaultTransactionManager);
            }
        }
        return defaultTransactionManager;
    }
}

因此,它只是获取默认的主PlatformTransactionManager bean。