Spring JPA:从异步方法处理时,数据未保存到数据库 [英] Spring JPA: Data not saved to Database when processing from an Async method
问题描述
我有一个使用Spring JPA的应用程序,并使用@Async spring批注进行一些后台处理.该过程需要将参数保存在数据库中,或者如果参数已经存在,则更新数据库.
I have an app that uses Spring JPA and does some background processes using the @Async spring annotation. The process require saving the parameters in the database or if it already exist update the db.
这不能正常工作,因为在完成@Async方法后,我的数据实体无法持久保存在数据库中.但是,当我从方法中删除@Async批注时,一切正常.我的数据已保存或更新到数据库.
This doesn't work fine as my data entity doesn't get persisted in the database after the @Async method is done. But when I removed the @Async annotation from the method, all things work fine. My data saved or updated to DB.
有人知道什么可能是错的或我可能缺少的吗?
Does anyone have an idea what could be wrong or what I may be missing?
谢谢.
**更新[代码示例]
** UPDATE [CODE SAMPLE]
ASYNCSERVICE
ASYNCSERVICE
@Service
@EnableAsync
class AsyncService() {
@Async
public void process(firstEntity, secondEntity) {
firstEntity = prepareParams(firstEntity);
secondEntity = prepareParams(secondEntity);
save(firstEntity, secondEntity);
}
}
ENTITYRELATIONSERVICE
ENTITYRELATIONSERVICE
@Transactional
@Service
class EntityRelationService() {
private SecondEntityRepo secondEntityRepo;
@Autowired
public EntityRelationService(SecondEntityRepo secondEntityRepo) {
this.secondEntityRepo = secondEntityRepo;
}
@Transactional
public void save(p1, p2) {
p2.setRelation(p1);
this.secondEntityRepo(p2);
}
}
BEAN CONFIG
BEAN CONFIG
@Bean
@Primary
@ConfigurationProperties(prefix = "app.database")
public DataSource dataSource() {
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName(driver);
dataSource.setUrl(url);
dataSource.setUsername(username);
dataSource.setPassword(password);
return dataSource;
}
@Bean
@Primary
public LocalSessionFactoryBean sessionFactory() {
LocalSessionFactoryBean sf = new LocalSessionFactoryBean();
sf.setDataSource(dataSource());
sf.setPackagesToScan("com.app");
Properties props = new Properties();
props.put("hibernate.use_sql_comments", false);
props.put("hibernate.show_sql", true);
sf.setHibernateProperties(props);
return sf;
}
@Bean
public HibernateTransactionManager transactionManager() {
HibernateTransactionManager transactionManager =
new HibernateTransactionManager();
transactionManager.setSessionFactory(sessionFactory().getObject());
return transactionManager;
}
推荐答案
我猜想@Async方法与@Transactional在同一类上.
I'm guessing the @Async method is on the same class as @Transactional.
当心:@Async将创建一个新的线程,该线程将不是事务性的(因此,不会在DB中写入).使用Spring代理的东西,您的异步方法需要在另一个类上调用一个方法,以使@Transactional返回.
Be ware : @Async will make a new thread that will NOT be transactional (so no write in DB). With Spring proxy stuff, your async method need to call a method on another class in order to have the @Transactional back.
换句话说,使用@Async方法创建一个新的空类,该方法只在原始类(具有@Transactional)中调用您的main方法.
In other word, create a new empty class with a @Async method that just call your main method in original class (that have the @Transactional).
这篇关于Spring JPA:从异步方法处理时,数据未保存到数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!