休眠会话关闭 [英] hibernate session close

查看:137
本文介绍了休眠会话关闭的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是遵循一个简单的hibernate struts教程,并通过hibernate成功完成了数据库调用。但是,我没有看到代码关闭任何地方的休眠连接。

  sessionFactory.getCurrentSession(); 

用于获取每个请求的会话。我应该在用户注销时关闭此会话吗?或者在每次请求之后,这个东西是否被框架照顾?第二个相关的问题是,在什么样的用例中我会使用hibernate会话拦截器?



和第三个问题(对不起,它只是在我脑海中弹出)用hibernate / struts2缓存的最佳选择是什么?

解决方案

关于第一个问题,


一个org.hibernate.Session在开始的时候开始第一次调用
getCurrentSession()是为当前线程创建的。然后通过Hibernate绑定
到当前线程。当事务结束时,通过提交或回滚
,Hibernate会自动从线程解绑定
org.hibernate.Session并为您关闭。


关于第二个问题,文档说:


拦截器接口提供从会话到
应用程序的回调,允许应用程序在保存,更新,删除
或加载之前检查和/或操作持久化对象的
属性。一个可能的用途是跟踪审计信息。
例如,以下拦截器会在创建Auditable时自动设置
createTimestamp,并在更新Auditable时更新
lastUpdateTimestamp属性。


关于第三个问题,Struts2是一个表示框架。它与持久性实体无关并因此缓存持久性实体。 文档详细介绍了第二级缓存。



正如你所看到的,文档中有很多有趣的信息。我会建议阅读它。


I just followed a simple hibernate struts tutorial and succesfully made db calls through hibernate. However I don't see the code closing the hibernate connection anywhere.

sessionFactory.getCurrentSession();

is used to get the session per request. Should I close this session when the user logs out? or after each request of is this something taken care of by the framework?

a second related question is, In what kind of use case would I use a hibernate session interceptor?

and a third question(sorry it just popped in my mind) what the best option for caching with hibernate/struts2?

解决方案

Regarding the first question, the reference documentation says:

A org.hibernate.Session begins when the first call to getCurrentSession() is made for the current thread. It is then bound by Hibernate to the current thread. When the transaction ends, either through commit or rollback, Hibernate automatically unbinds the org.hibernate.Session from the thread and closes it for you.

Regarding your second question, the documentation says:

The Interceptor interface provides callbacks from the session to the application, allowing the application to inspect and/or manipulate properties of a persistent object before it is saved, updated, deleted or loaded. One possible use for this is to track auditing information. For example, the following Interceptor automatically sets the createTimestamp when an Auditable is created and updates the lastUpdateTimestamp property when an Auditable is updated.

Regarding the third question, Struts2 is a presentation framework. It has nothing to do with persistence and thus caching of the persistent entities. The documentation talks in details about the second-level cache.

As you see, the documentation has a whole lot of interesting information. I would advise reading it.

这篇关于休眠会话关闭的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆