JPA懒加载异常:could not initialize proxy - no Session

Caused by: org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: xxx.xxx.xxx.xxx.xxx.xxx.xxx, could not initialize proxy - no Session

springboot项目,hibernate

调用一个接口:其过程有

一个service:事务@Transactional(propagation=Propagation.REQUIRED) 内

调用另一service:事务@Transactional(propagation=Propagation.NOT_SUPPORTED)

查询了一个对象,有懒加载属性,导致再获取此属性报错!

若直接从请求C开始请求执行,则正常执行!

调整日志打印级别,查看jpa打印日志

执行日志打印:

若正常情况:

JPA EntityManager是OpenEntityManagerInViewInterceptor打开:

Opening JPA EntityManager in OpenEntityManagerInViewInterceptor

并在一次完整的请求没有结束时,一直不会关闭,

Not closing pre-bound JPA EntityManager after transaction

为JPA事务,保存在当前线程:

Found thread-bound EntityManager [SessionImpl(1048802421<open>)] for JPA transaction!

在同一事务下可以传递:

Participating in existing transaction

 

但当事务传播特性为NOT_SUPPORTED时,

事务挂起,后面需要新建事务

Suspending current transaction

同时还需要重新打开一个EntityManager,

但不是OpenEntityManagerInViewInterceptor打开的:

Opened new EntityManager [SessionImpl(880287245<open>)] for JPA transaction

事务提交时EntityManager也关闭了:

Committing JPA transaction on EntityManager [SessionImpl(880287245<open>)]

Closing JPA EntityManager [SessionImpl(880287245<open>)] after transaction

所以会导致再去查询懒加载的属性会报错!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值