mercredi 25 mars 2015

@Async is killing Hibernate transaction

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();
}


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();
}
}


and:



@Component
public class AsyncMarketCaller {

@Inject
MarketManagerFactory marketManagerFactory;

@Async
public Future<List<Product>> getProducts(){

MarketManager productManager = marketManagerFactory.obtainMarketManager(market);
result = productManager.getProducts();
....
}
}


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);
}
}


however, for upcEanDAO.find(identifier) I get an exception:



Caused by: org.hibernate.HibernateException: get is not valid without active transaction


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.


I tried adding based on another answer here, @Transactional to the method annotated with @Async but it doesn't help.


Any idea?


Aucun commentaire:

Enregistrer un commentaire