@Async 正在杀死 Hibernate 事务

Dej*_*ell 4 java spring asynchronous hibernate spring-transactions

我正在为我的 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();
}
Run Code Online (Sandbox Code Playgroud)

这个工作就好了。我想添加@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();
    }
}
Run Code Online (Sandbox Code Playgroud)

和:

@Component
public class AsyncMarketCaller {

    @Inject
    MarketManagerFactory marketManagerFactory;

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

        MarketManager productManager = marketManagerFactory.obtainMarketManager(market);
        result = productManager.getProducts();
        ....
    }
}
Run Code Online (Sandbox Code Playgroud)

productManager 调用另一个@Service

@Service
public class DefaultIdentifierManager implements IdentifierManager{
     @Inject
    UpcEanDAO upcEanDAO;

      @Override
    public String getTitleForIdentifier(String identifier){
        UpcEan upcEan = upcEanDAO.find(identifier);
    }
}
Run Code Online (Sandbox Code Playgroud)

但是,因为 upcEanDAO.find(identifier)我得到了一个例外:

Caused by: org.hibernate.HibernateException: get is not valid without active transaction
Run Code Online (Sandbox Code Playgroud)

在我添加对它@Async进行异步调用的功能之前,getProducts()它工作得很好,所以我假设这会@Async杀死我用 Hibernate 打开的事务。

我尝试根据此处的另一个答案添加@Transactional到带有注释的方法中,@Async但它没有帮助。

任何的想法?

已编辑

我编辑了代码

@Component
public class AsyncMarketCaller {

    @Inject
    AsyncMarketService asyncMarketService;

    @Async
    public Future<List<Product>> getProducts(){
        asyncMarketService.getProducts();
    }
}
Run Code Online (Sandbox Code Playgroud)

@Service
public class AsyncMarketService {

    @Inject
    MarketManagerFactory marketManagerFactory;

    @Transactional
    public Future<List<Product>> getProducts()
     ....
}

}
Run Code Online (Sandbox Code Playgroud)

我在日志中看到

50689 DEBUG [localhost-startStop-1] org.springframework.transaction.annotation.AnnotationTransactionAttributeSource - Adding transactional method 'AsyncMarketService.getProducts' with attribute: PROPAGATION_REQUIRED,ISOLATION_DEFAULT; ''
Run Code Online (Sandbox Code Playgroud)

但它没有帮助。请注意,我的方法 AsyncMarketService.getProducts 不直接调用数据库,而是调用其他方法,并且只有其中一个方法会进行调用。

我还在上面添加了实际调用 DB 的那个:@Transactional

49992 DEBUG [localhost-startStop-1] org.springframework.transaction.annotation.AnnotationTransactionAttributeSource - Adding transactional method 'DefaultIdentifierManager.getTitleForIdentifier' with attribute: PROPAGATION_REQUIRED,ISOLATION_DEFAULT; ''

Mar*_*rey 9

Hibernate 事务在此ThreadLocal基础上工作。

当您使用另一个线程时@Async,将没有事务处于活动状态。

您可以通过让异步方法调用另一个由 注释的 bean 来实现此功能@Transactional

在这里我稍微解释一下这种方法: 如何在使用 Spring Data 和 Hibernate 时正确执行后台线程?

  • 正如您在编辑后的答案中看到的那样,我尝试了您的解决方案。但没有帮助。你有什么建议? (3认同)
  • 没有帮助。我什至将调用转移到直接调用 DAO 的类到 AsyncService(帮助程序),我得到了同样的错误。我怎么知道交易是否真的打开了? (2认同)