zoukankan      html  css  js  c++  java
  • dubbo序列化hibernate.LazyInitializationException could not initialize proxy

    dubbo序列化,hibernate.LazyInitializationException could not initialize proxy - no Session懒加载异常的解决

    转载声明:请务必保留原文链接

    原文连接:http://www.cnblogs.com/sunyl/p/7772260.html

    版本

    1.JDK 1.8

    2.Spring Boot ,spring boot jpa 1.5.8

    3.dubbo 2.5.6

    4.zookeeper 3.4.9

    异常说明:

    使用dubbo作为RPC框架时 发现如果有实体对象有懒加载属性会出现如下异常,我在设 #spring.jpa.open-in-view=true未能解决。

    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/

  • 相关阅读:
    在 mac iTerm2 中使用 cmd 终端
    在 jupyter 中添加菜单和自动完成功能
    Bash 和 Zsh 开启 vi-mode
    免密登录和远程执行命令
    图片的筛选
    win10 右键菜单很慢的解决方式
    ssh中的 Connection closed by ***
    NodeJS 获取网页源代码
    在 JSDOM v11 中使用jQuery
    kafaka学习
  • 原文地址:https://www.cnblogs.com