关于 Hibernate LazyInitialization 错误
我在 EJB2.0 层上使用事务,并从那里调用 DAO 层,其中对数据库进行休眠调用。
我现在遇到的问题是,hibernate 正在对数据库进行多次调用,因为我设置了 lazy="false"
,现在我尝试更改 lazy="true"
但现在得到我们自己的:
org.hibernate.LazyInitializationException:无法初始化代理 - 所属会话已关闭
错误消息。
我对 SO
进行了一些研究,发现解决方案可能是
<prop key="hibernate.default_batch_fetch_size">30</prop>
<prop key="hibernate.jdbc.fetch_size"></prop>
,我的问题是 fetch_size 的默认大小是多少,以及这是否是解决此问题的正确方法。
我想要做的就是确保应用程序不会多次调用数据库来运行所有这些 N
查询,因为现在在页面之间导航非常痛苦,并且使应用程序在每个页面单击时都非常慢,有 N
个查询在后台运行,这会增加应用程序加载时间。
更新
这是我如何设置事务:
* @hibernate.class table="SCHEDULE_ENTRY" discriminator-value="task" lazy="true"
* @hibernate.discriminator column="KIND" length="4"
非常感谢任何提高休眠性能的建议。
I am using transactions
on EJB2.0
layer and from there am making call to DAO
layer wherein am making hibernate calls to db.
Issue am having right now is that hibernate is making multiple calls to database because am setting lazy="false"
, now i tried to change lazy="true"
but now am getting our own :
org.hibernate.LazyInitializationException: could not initialize proxy
- the owning Session was closed
error message.
I did some research on SO
and found that solution could be
<prop key="hibernate.default_batch_fetch_size">30</prop>
<prop key="hibernate.jdbc.fetch_size"></prop>
and my question is what would be default size of fetch_size and if this is right way to solve this problem.
All i want to do is make sure that application does not make multiple calls to database to run all those N
queries as right now navigating between page is lot of pain and makes application very slow as on every page click, there are N
of queries that are run in the background and it increases application load time.
Update
Here is the how am setting transactions:
* @hibernate.class table="SCHEDULE_ENTRY" discriminator-value="task" lazy="true"
* @hibernate.discriminator column="KIND" length="4"
Would highly appreciate any suggestions to improve performance of hibernate.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在关闭会话之前访问对象的成员。这是主要问题。
Access the member of the object before closing the session. Thats the main problem.
当您尝试访问尚未初始化的对象(代理)的成员并且该对象未绑定到会话(分离)时,您会遇到该异常。在考虑更改批量大小之前,您必须首先解决这个问题。
要解决这个问题,请确保您要访问的所有属性都已初始化。
这取决于您可以在其中使用延迟加载的事务/会话配置。如果您发布交易/会话配置,将会很有帮助。
您可以使用 Hibernate.initialize() 来初始化成员,但我建议仅在需要时更改您的获取策略。
看看这里。
我希望我能帮忙:)
You get that exception when you try to access a member of a object (proxy) that has not been initialized yet AND that object is not bound to a session(detached). You have to get rid of that problem first before thinking about changing the batch size.
To get rid of that problem ensure that all the properties you want to access are initialized.
It depends on your transaction/session configuration where you can use lazy loading. It would be helpful if you post your transaction/session configuration.
You can use
Hibernate.initialize()
to initialize members, but i would recommend changing your fetching strategy only where needed.Take a look at here.
I hope i could help :)