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
所以会导致再去查询懒加载的属性会报错!