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/