Java Spring批处理的集成在以前运行的@Transactional方法中导致TransactionRequiredException
摘要 将Spring批处理配置添加到Spring引导应用程序后,执行标记为@Transactional的现有方法将不再有效。抛出TransactionRequiredException 环境和设置 Maven层次结构+依赖项:Java Spring批处理的集成在以前运行的@Transactional方法中导致TransactionRequiredException,java,spring-boot,jpa,spring-batch,entitymanager,Java,Spring Boot,Jpa,Spring Batch,Entitymanager,摘要 将Spring批处理配置添加到Spring引导应用程序后,执行标记为@Transactional的现有方法将不再有效。抛出TransactionRequiredException 环境和设置 Maven层次结构+依赖项: <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent&
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.6.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-websocket</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
...
</dependencies>
我们的数据源是通过.properties文件定义的
spring.datasource.platform=postgres
spring.datasource.url=jdbc:postgresql://localhost:5432/localDatabase
spring.datasource.username=...
spring.datasource.password=...
通过这种配置,可以使用@Transactional注释标记方法,并在那里使用EntityManager(例如在@Service中)
问题
SpringBatch(通过SpringBootStarter父级管理的版本)与以下配置的集成导致了这些@Transactional方法中的javax.persistence.TransactionRequiredException
@Configuration
@EnableBatchProcessing
public class BatchConfig {
@Autowired
DataSource dataSource;
@Bean
public BatchConfigurer batchConfigurer() {
return new DefaultBatchConfigurer() {
@Override
protected JobRepository createJobRepository() throws Exception {
JobRepositoryFactoryBean factory = new JobRepositoryFactoryBean();
factory.setDataSource(dataSource);
factory.setTransactionManager(getTransactionManager());
factory.setIsolationLevelForCreate("ISOLATION_SERIALIZABLE");
factory.setTablePrefix("BATCH_");
factory.setMaxVarCharLength(1000);
return factory.getObject();
}
};
}
}
搜索web后,我尝试扩展DefaultBatchConfigurer,覆盖getTransactionManager()方法,并使用autowired EntityManagerFactory提供JPATransactionManager:
@Override
public PlatformTransactionManager getTransactionManager() {
return new JpaTransactionManager(emFactory);
}
但这并没有解决问题。如何配置Spring Batch,使事务管理在同一Spring Boot应用程序中对Spring Batch和其他功能都能正常工作?
执行标记为@Transactional的现有方法不再工作。
:这些方法在批处理作业内部或应用程序中的其他地方使用时是否不再工作?这是否正确回答你的问题?它正在工作。不幸的是,我不知道确切的原因。Spring配置痛苦:/i我发布的解决方案尝试已经是正确的方法。谢谢你的努力!(这是关于应用程序中其他地方的方法)标记为@Transactional的现有方法的执行不再有效
:这些方法在批处理作业或应用程序中其他地方使用时是否不再有效?这是否回答了您的问题?它正在工作。不幸的是,我不知道确切的原因。Spring配置痛苦:/i我发布的解决方案尝试已经是正确的方法。谢谢你的努力!(是关于应用程序中其他地方的方法)
@Override
public PlatformTransactionManager getTransactionManager() {
return new JpaTransactionManager(emFactory);
}