小编典典

@Async正在杀死Hibernate事务

hibernate

我为我的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 - Adding transactional method 'DefaultIdentifierManager.getTitleForIdentifier' with attribute: PROPAGATION_REQUIRED,ISOLATION_DEFAULT; '


阅读 525

收藏
2020-06-20

共1个答案

小编典典

hibernate事务是ThreadLocal基于基础的。

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

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

2020-06-20