我面临与事务管理相关的问题(特别是需要新的事务注释),详情如下:
我使用了Spring-boot(1.1.5.RELEASE) . spring-boot-starter-batch b . spring-boot-starter-data-jpa另外为此我使用了hibernate最新版本
我的jpa配置相关代码如下:
@Configuration
@ComponentScan
@EnableTransactionManagement
@EnableJpaRepositories(basePackages = "xyz.persistence.repository")
public class JpaConfig {
@Bean
public JpaVendorAdapter jpaVendorAdapter() {
HibernateJpaVendorAdapter hibernateJpaVendorAdapter = new HibernateJpaVendorAdapter();
hibernateJpaVendorAdapter.setShowSql(false);
hibernateJpaVendorAdapter.setGenerateDdl(false);
hibernateJpaVendorAdapter.setDatabase(Database.MYSQL);
return hibernateJpaVendorAdapter;
}
@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory(DataSource dataSource, JpaVendorAdapter jpaVendorAdapter) {
LocalContainerEntityManagerFactoryBean lef = new LocalContainerEntityManagerFactoryBean();
lef.setDataSource(dataSource);
lef.setJpaVendorAdapter(jpaVendorAdapter);
lef.setPackagesToScan("xyz.persistence.domain");
return lef;
}
@Bean
public PlatformTransactionManager transactionManager(EntityManagerFactory entityManagerFactory) {
return new JpaTransactionManager(entityManagerFactory);
}
}
但在执行期间,我面临与事务管理相关的问题 .
我在这个问题上挖了北斗,发现了下面列出的事件顺序:
Spring-boot会按预期加载我的事务管理器但是在相同的情况下,spring-boot会加载spring-batch,从而加载SimpleBatchConfiguration . 由于SimpleBatchConfiguration也有事务管理器的定义(没有@ConditionalOnMissingBean),它会覆盖我的事务管理器 . 现在是spring-data-jpa,即JpaBaseConfiguration . 这和transactionManager的定义一样,几乎与我在jpaconfig中定义的相似 . 但是这并没有覆盖早期的定义,因为使用了@ConditionalOnMissingBean .
因此,在所有初始化之后,我留下了Spring-batch注入的transactionmanager,导致事务管理器无法正常工作 .
PS:这是使用spring-boot 0.5.0.m6(因为@ConditionalOnMissingBean不存在于JpaBaseConfiguration中)**
我试图排除SimpleBatchConfiguration,但这也无效 .
任何帮助将不胜感激 .
阿沛