3

我正在尝试让 Atomikos 与我的 Spring Boot/Spring Batch 应用程序一起工作。

这是我的代码的相关部分:

数据源配置:

@Configuration
public class DatasourceConfiguration extends AbstractCloudConfig {

    @Bean
    @Qualifier("batch_database")
    public DataSource batchDatasource() {
        return connectionFactory().dataSource("batch_database");
    }

    @Bean
    public PlatformTransactionManager transactionManager(){
        return  new JtaTransactionManager();
    }

    @Bean
    public TaskConfigurer configurer(){
        return new DefaultTaskConfigurer(batchDatasource());
    }
}

Atomikos 自动配置依赖:

compile("org.springframework.boot:spring-boot-starter-jta-atomikos")

我的application.properties

spring.datasource.application.driver-class-name=org.postgresql.xa.PGXADataSource
spring.datasource.batch.driver-class-name=org.postgresql.xa.PGXADataSource

这是自动配置报告:

   DataSourceTransactionManagerAutoConfiguration matched
      - @ConditionalOnClass classes found: org.springframework.jdbc.core.JdbcTemplate,org.springframework.transaction.PlatformTransactionManager (OnClassCondition)

   DataSourceTransactionManagerAutoConfiguration.DataSourceTransactionManagerConfiguration matched
      - @ConditionalOnBean (types: javax.sql.DataSource; SearchStrategy: all) found the following [batchDatasource, applicationDatasource, batch_database, application_database, database] (OnBeanCondition)

   AtomikosJtaConfiguration did not match
      - @ConditionalOnClass classes found: org.springframework.transaction.jta.JtaTransactionManager,com.atomikos.icatch.jta.UserTransactionManager (OnClassCondition)
      - @ConditionalOnMissingBean (types: org.springframework.transaction.PlatformTransactionManager; SearchStrategy: all) found the following [transactionManager] (OnBeanCondition)

   AtomikosJtaConfiguration.AtomikosJtaJmsConfiguration did not match
      - required @ConditionalOnClass classes not found: javax.jms.Message (OnClassCondition)
      - Ancestor 'org.springframework.boot.autoconfigure.transaction.jta.AtomikosJtaConfiguration' did not match (ConditionEvaluationReport.AncestorsMatchedCondition)

我不确定为什么 Atomikos 没有自动配置...有人可以帮忙吗?

编辑:我已经注释掉了 JTA transactionManager bean,现在我得到了以下自动配置报告:

   DataSourceTransactionManagerAutoConfiguration matched
      - @ConditionalOnClass classes found: org.springframework.jdbc.core.JdbcTemplate,org.springframework.transaction.PlatformTransactionManager (OnClassCondition)

    DataSourceTransactionManagerAutoConfiguration.DataSourceTransactionManagerConfiguration matched
          - @ConditionalOnBean (types: javax.sql.DataSource; SearchStrategy: all) found the following [batchDatasource, applicationDatasource, batch_database, application_database, database] (OnBeanCondition)

   TransactionAutoConfiguration matched
      - @ConditionalOnClass classes found: org.springframework.transaction.support.TransactionTemplate,org.springframework.transaction.PlatformTransactionManager (OnClassCondition)
      - @ConditionalOnSingleCandidate (types: org.springframework.transaction.PlatformTransactionManager; SearchStrategy: all) found a primary candidate amongst the following [transactionManager] (OnBeanCondition)

   AtomikosJtaConfiguration did not match
      - @ConditionalOnClass classes found: org.springframework.transaction.jta.JtaTransactionManager,com.atomikos.icatch.jta.UserTransactionManager (OnClassCondition)
      - @ConditionalOnMissingBean (types: org.springframework.transaction.PlatformTransactionManager; SearchStrategy: all) found the following [transactionManager] (OnBeanCondition)

   AtomikosJtaConfiguration.AtomikosJtaJmsConfiguration did not match
      - required @ConditionalOnClass classes not found: javax.jms.Message (OnClassCondition)
      - Ancestor 'org.springframework.boot.autoconfigure.transaction.jta.AtomikosJtaConfiguration' did not match (ConditionEvaluationReport.AncestorsMatchedCondition)

我如何确保 Atomikos 事务管理器被拾取?

编辑2:我排除TransactionManager了如下类:

@EnableAutoConfiguration(exclude = {DataSourceTransactionManagerAutoConfiguration.class, TransactionAutoConfiguration.class})

但我似乎SimpleBatchConfiguration仍然对我SimpleTaskConfiguration施加了几个TransactionManager类中的一个:

2016-07-12 11:27:57.846  INFO 4851 --- [           main] o.s.b.f.s.DefaultListableBeanFactory     : Overriding bean definition for bean 'transactionManager' with a different definition: replacing [Root bean: class [null]; scope=; abstract=false; lazyInit=false; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=org.springframework.batch.core.configuration.annotation.SimpleBatchConfiguration; factoryMethodName=transactionManager; initMethodName=null; destroyMethodName=(inferred); defined in class path resource [org/springframework/batch/core/configuration/annotation/SimpleBatchConfiguration.class]] with [Root bean: class [null]; scope=; abstract=false; lazyInit=false; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=org.springframework.cloud.task.configuration.SimpleTaskConfiguration; factoryMethodName=transactionManager; initMethodName=null; destroyMethodName=(inferred); defined in org.springframework.cloud.task.configuration.SimpleTaskConfiguration]

导致AtomikosJtaConfiguration不匹配...

有人可以建议最佳实践以使 Atomikos 自动配置正常工作吗?

4

1 回答 1

-1

您无需添加 Atomikos 或 Bitronix 即可为您的数据源指定 PlatformTransactionManager。请参阅实体不持久。RepositoryItemWriter 和 SimpleJpaWriter 是线程安全的吗?.

于 2016-07-13T14:17:09.530 回答