java-@Async正在杀死休眠事务
作者:互联网
我为我的REST API使用了Open-Session-In-View事务模型,如下所示:
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
sessionFactory.getCurrentSession().beginTransaction();
chain.doFilter(request, response);
sessionFactory.getCurrentSession().getTransaction().commit();
}
这项工作很好.我想添加@Async功能.所以我创建了:
@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
@Service
public class DefaultIdentifierManager implements IdentifierManager{
@Inject
UpcEanDAO upcEanDAO;
@Override
public String getTitleForIdentifier(String identifier){
UpcEan upcEan = upcEanDAO.find(identifier);
}
}
但是,对于upcEanDAO.find(identifier)我得到一个例外:
Caused by: org.hibernate.HibernateException: get is not valid without active transaction
在添加@Async功能以对getProducts()进行异步调用之前,它工作得很好,因此我假设@Async杀死了我在Hibernate中打开的事务.
我尝试根据此处的另一个答案将@Transactional添加到以@Async注释的方法,但这无济于事.
任何想法?
已编辑
我编辑了代码,所以
@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不会直接调用数据库,而是会调用其他方法,只有其中一个会进行调用.
我还添加了一个实际调用DB:@Transactional的函数
49992 DEBUG [localhost-startStop-1] org.springframework.transaction.annotation.AnnotationTransactionAttributeSource-添加事务方法’DefaultIdentifierManager.getTitleForIdentifier’,属性为:PROPAGATION_REQUIRED,ISOLATION_DEFAULT; ”
解决方法:
休眠事务基于ThreadLocal工作.
当您将另一个线程与@Async一起使用时,将没有活动的事务.
您可以通过使异步方法调用另一个由@Transactional注释的bean来实现此功能.
在这里,我将进一步解释这种方法:
How do I properly do a background thread when using Spring Data and Hibernate?
标签:spring-transactions,asynchronous,hibernate,spring,java 来源: https://codeday.me/bug/20191028/1953152.html