dubbo序列化,hibernate.LazyInitializationException could not initialize proxy - no Session懒加载异常的解决html
转载声明:请务必保留原文连接java
原文链接:http://www.cnblogs.com/sunyl/p/7772260.html
git
版本github
1.JDK 1.8redis
2.Spring Boot ,spring boot jpa 1.5.8spring
3.dubbo 2.5.6框架
4.zookeeper 3.4.9spa
异常说明:hibernate
使用dubbo做为RPC框架时 发现若是有实体对象有懒加载属性会出现以下异常,我在设 #spring.jpa.open-in-view=true未能解决。htm
Caused by: org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: cn.syl.blogmain.pojo.User.roles, could not initialize proxy - no Session
at org.hibernate.collection.internal.AbstractPersistentCollection.throwLazyInitializationException(AbstractPersistentCollection.java:587)
at org.hibernate.collection.internal.AbstractPersistentCollection.withTemporarySessionIfNeeded(AbstractPersistentCollection.java:204)
at org.hibernate.collection.internal.AbstractPersistentCollection.readSize(AbstractPersistentCollection.java:148)
at org.hibernate.collection.internal.PersistentSet.size(PersistentSet.java:143)
at com.alibaba.com.caucho.hessian.io.CollectionSerializer.writeObject(CollectionSerializer.java:92)
at com.alibaba.com.caucho.hessian.io.Hessian2Output.writeObject(Hessian2Output.java:383)
at com.alibaba.com.caucho.hessian.io.JavaSerializer$FieldSerializer.serialize(JavaSerializer.java:294)
... 29 more
三种解决方法:
缘由是dubbo使用的序列化hessian 对PersistentCollection对象没法识别,有以下三种解决方法:
1.JPA Hibernate禁止懒加载
2.添加自定义dubbo序列化工厂(请参考:http://wzalong.iteye.com/blog/2323787)
3.修改dubbo序列化方式:<dubbo:protocol name="dubbo" port="20880" serialization="java" />
因为本人对dubbo研究甚少少 因此目前采用了第三种方式.
若有高人使用了第二种自定义序列化工厂或其余更好的方式 还请指教,我不太清楚dubbo自定义序列化工厂如何配置.
可参考的项目配置(仅包含配置,集成spring boot +dubbo +redis +jpa+shiro): 若是项目对您有帮助请给我一颗star.谢谢~
https://github.com/mmdsyl/BLOG-Microservice/