@EnableTransactionManagment注释不适用于Spring JtaTransactionManager

时间:2016-06-08 04:21:30

标签: java spring transactions

我实际上在我的spring java配置中使用了两个事务管理器对象 如下图所示

    @Bean("jtaTranx")
    public PlatformTransactionManager jtaTransactionMangaer(){ 
        return new JtaTransactionManager();
    }

    @Bean
    @Primary
    public PlatformTransactionManager transactionManager() {
        JpaTransactionManager transactionManager = new JpaTransactionManager(entityManagerFactory());
        return transactionManager;
    }

使用@configuration和@EnableTransactionManagment注释类级别。 当我尝试使用@Transactional进行某些交易时,我发现异常无法在托管交易期间执行交易,任何人都可以告诉我如何解决此问题。

1 个答案:

答案 0 :(得分:0)

我能够使用Embedded JtaTransactionManager解决这个问题。我已经采用了atomikos参考实现表单here

    @Bean
    @Primary
    public PlatformTransactionManager transactionManager() throws PropertyVetoException, NamingException {
        EntityManagerFactory factory = entityManagerFactory().getObject();
        return new JpaTransactionManager(factory);
    }

    @Bean(name = "jtaTransactionManager")   
    public PlatformTransactionManager jtaTransactionManager() throws Throwable {
      UserTransaction userTransaction = userTransaction() ;
      TransactionManager transactionManager = atomikosTransactionManager() ;
      return new JtaTransactionManager(userTransaction, transactionManager );
    }

    @Bean
    public UserTransaction userTransaction() throws Throwable {
        UserTransactionImp userTransactionImp = new UserTransactionImp();
        userTransactionImp.setTransactionTimeout(10000);
        return userTransactionImp;
    }

    @Bean(initMethod = "init", destroyMethod = "close")
    public TransactionManager atomikosTransactionManager() throws Throwable {
        UserTransactionManager userTransactionManager = new UserTransactionManager();
        userTransactionManager.setForceShutdown(false);
        return userTransactionManager;
    }