为了账号安全,请及时绑定邮箱和手机立即绑定

Spring boot @Async 注解抛出 LazyInitialization

Spring boot @Async 注解抛出 LazyInitialization

达令说 2021-10-13 12:45:38
我有一个 emailsenderservice 来异步管理电子邮件通知。有 2 种异步方法,一种方法有效,但另一种方法抛出 LazyInitializationException:@Servicepublic class EmailSenderService {    // working    @Async    public void sendNewBidRequestEmail(BidRequest bidRequest) {        this.sendNewBidRequestEmailToSupplier(bidRequest);    }    @Transactional    public void sendNewBidRequestEmailToSupplier(BidRequest bidRequest) {        sendNewBidRequestEmailToSupplier(bidRequest, bidRequest.getHotels());    }    @Transactional    public void sendNewBidRequestEmailToSupplier(BidRequest bidRequest, List<Hotel> hotelList) {        for (Hotel hotel : hotelList) {           ...           this.sender.send()        }    }    // not working, throw exception    @Async    public void sendCancelledBidRequestEmail(BidRequest bidRequest, String reason) {        this.sendCancelledBidRequestEmailToSupplier(bidRequest, bidRequest.getHotels(), reason);    }    @Transactional    public void sendCancelledBidRequestEmailToSupplier(BidRequest bidRequest, List<Hotel> hotelList, String reason) {        for (Hotel hotel : hotelList) {   // throw exception here           ...           this.sender.send();        }}我完全按照这个线程。您可以看到两种异步方法具有几乎相同的结构。异步方法调用事务方法。而第二个抛出org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: com.corpobids.server.entity.BidRequest.hotels, could not initialize proxy - no Session。
查看完整描述

1 回答

  • 1 回答
  • 0 关注
  • 177 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信