问题描述
我正在为我的REST api使用Open-Session-In-View事务模型:
I am using an Open-Session-In-View transaction model for my REST api like this:
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
sessionFactory.getCurrentSession().beginTransaction();
chain.doFilter(request, response);
sessionFactory.getCurrentSession().getTransaction().commit();
}
这项工作很好.我想添加@Async功能.所以我创建了:
This work just fine. I wanted to add @Async abilities. So I created:
@Configuration
@EnableAsync
public class AsyncConfig implements AsyncConfigurer {
@Override
@Bean(destroyMethod="shutdown")
public Executor getAsyncExecutor() {
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
executor.setCorePoolSize(18);
executor.setMaxPoolSize(18);
executor.initialize();
executor.setDaemon(true);
executor.setWaitForTasksToCompleteOnShutdown(false);
return executor;
}
@Override
public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() {
return new SimpleAsyncUncaughtExceptionHandler();
}
}
和:
@Component
public class AsyncMarketCaller {
@Inject
MarketManagerFactory marketManagerFactory;
@Async
public Future<List<Product>> getProducts(){
MarketManager productManager = marketManagerFactory.obtainMarketManager(market);
result = productManager.getProducts();
....
}
}
productManager调用另一个@Service
The productManager makes a call to another @Service
@Service
public class DefaultIdentifierManager implements IdentifierManager{
@Inject
UpcEanDAO upcEanDAO;
@Override
public String getTitleForIdentifier(String identifier){
UpcEan upcEan = upcEanDAO.find(identifier);
}
}
但是,对于upcEanDAO.find(identifier)
,我得到一个例外:
however, for upcEanDAO.find(identifier)
I get an exception:
Caused by: org.hibernate.HibernateException: get is not valid without active transaction
在添加@Async
对getProducts()
进行异步调用的功能之前,它工作得很好,因此我假设@Async
杀死了我用Hibernate打开的事务.
Before I added the @Async
ability to make async calls to getProducts()
it worked just fine so I assume that @Async
kills the transaction that I opened with Hibernate.
我尝试根据此处的另一个答案@Transactional
添加到以@Async
注释的方法,但这无济于事.
I tried adding based on another answer here, @Transactional
to the method annotated with @Async
but it doesn't help.
有什么主意吗?
已编辑
我对代码进行了编辑
@Component
public class AsyncMarketCaller {
@Inject
AsyncMarketService asyncMarketService;
@Async
public Future<List<Product>> getProducts(){
asyncMarketService.getProducts();
}
}
和
@Service
public class AsyncMarketService {
@Inject
MarketManagerFactory marketManagerFactory;
@Transactional
public Future<List<Product>> getProducts()
....
}
}
我在日志中看到
50689 DEBUG [localhost-startStop-1] org.springframework.transaction.annotation.AnnotationTransactionAttributeSource - Adding transactional method 'AsyncMarketService.getProducts' with attribute: PROPAGATION_REQUIRED,ISOLATION_DEFAULT; ''
但是没有帮助.请注意,我的方法AsyncMarketService.getProducts不会直接调用数据库,而是会调用其他方法,只有其中一个会进行调用.
but it doesn't help. Please note that my method AsyncMarketService.getProducts doesn't call directly the DB, it calls to other methods and only one of them will make the call.
我还添加了一个实际调用DB:@Transactional的对象
I also added above the one that actually make the call to DB:@Transactional
49992 DEBUG [localhost-startStop-1] org.springframework.transaction.annotation.AnnotationTransactionAttributeSource - Adding transactional method 'DefaultIdentifierManager.getTitleForIdentifier' with attribute: PROPAGATION_REQUIRED,ISOLATION_DEFAULT; '
'
推荐答案
休眠事务在ThreadLocal
基础上工作.
Hibernate transactions work on a ThreadLocal
basis.
当您将另一个线程与@Async
一起使用时,将没有活动的事务.
As you use another thread with @Async
there will be no transaction active.
您可以通过让async方法调用另一个由@Transactional
注释的bean来实现此功能.
You can achieve this functionality by having the async method calling another bean which is annotated by @Transactional
.
在这里,我将进一步解释这种方法:使用Spring Data和Hibernate时如何正确执行后台线程?
Here I explain this approach a little bit more:How do I properly do a background thread when using Spring Data and Hibernate?
这篇关于@Async正在杀死Hibernate事务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!