控制休眠会话(何时手动关闭) [英] Control the hibernate session(when to close it manually)

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

问题描述

像这样:



Like this:

Session sess=getSession();
Transcration tx=sess.beginTranscration();
//do something using teh session
sess.save(obj);
tx.commit();
sess.close;

在独立应用程序中使用它时毫无疑问。
但是我不确定何时在web应用程序中使用。

I have no question when using it in a standlone application. However I am not sure when using in the web app.

例如,我有一个servlet: TestServlet 从客户端接收参数,然后我调用管理器根据参数查询某些内容,就像这样:

For example, I have a servlet: TestServlet to receive the parameters from the client, then I call a Manager to query something according to the parameters, just like this:

class TestServlet{
  doGet(HttpServletRequset,httpServletResponse){
    String para1=request.getParam...();
    String para2=.....
    new Manager().query(para1,para2);
  }
}

class Manager{
  public String query(String pa1,String pa2){
    Session=....// get the session
    //do query using para1 and 1
    session.close() //Here, I wonder if I should close it.
  }
}

我应该在查询方法中关闭会话吗?

Should I close the session in the query method?

由于有人告诉我,hibernate中的会话就像jdbc中的连接一样。所以经常打开和关闭它是正确的方式?

Since someone told me that session in hibernate is just like the connection in jdbc. So opening and closing it so frequently is the correct way?

顺便说一下,每次都需要tx.commit()吗?

BTW, does the tx.commit() is required each time?

另外,在servlet中使用会话的线程问题是什么,因为我看到会话在api中不是线程安全的。

Also what's the thread problem about using session in servlet, since I saw the session is not thread safe in api.

推荐答案


我是hibernate的新手,在阅读了hibernate api和tutorial之后,似乎会话在不使用时应该是cloesd。

I am new in hibernate,after read the hibernate api and tutorial,it seems that the session should cloesd when not used.

完成后应关闭(但可以自动为您完成)。

It should be closed when you're done with (but this can be done automatically for you as we'll see).


在独立应用程序中使用它时,我毫无疑问。不过,我不确定何时在Web应用程序中使用。

I have no question when using it in a standalone application. However I am not sure when using in the web app.

好的,正如 11.1.1。文档的工作单元 多用户客户机/服务器应用程序中的最常见的模式是每次请求会话
$ b

Well, as explained in the section 11.1.1. Unit of work of the documentation, the most common pattern in a multi-user client/server application is session-per-request.


例如,我有一个servlet:TestServlet从客户端接收参数,然后我调用管理器来根据参数:就像这样(...)我应该关闭查询方法中的会话?

For example, I have a servlet:TestServlet to recieve the parameters from the client,then I call a Manager to query something according to the parameters: just like this (...) Should I close the session in the query method?

这一切都取决于如何如果您使用 sessionFactory.getCurrentSession(),您将获得会话。

It all depends on how you obtain the session.


  • 会得到一个绑定到事务生命周期的当前会话,并在事务结束时自动刷新并关闭(提交或回滚)。
  • 如果您决定使用
  • sessionFactory.openSession(),您必须自己管理会话并手动刷新并关闭它。

  • if you use sessionFactory.getCurrentSession(), you'll obtain a "current session" which is bound to the lifecycle of the transaction and will be automatically flushed and closed when the transaction ends (commit or rollback).
  • if you decide to use sessionFactory.openSession(), you'll have to manage the session yourself and to flush and close it "manually".

实现一个 sessi每个请求模式,更喜欢第一种方法(更简单,更简洁)。使用第二种方法来实现 长谈话

To implement a session-per-request pattern, prefer the first approach (much easier and less verbose). Use the second approach to implement long conversations.

wiki页面会话和交易是对这个主题的文档的一个很好的补充。

The wiki page Sessions and transactions is a good complement to the documentation on this topic.


顺便说一下,每次都需要tx.commit()吗?

BTW, does the tx.commit() is required each time?

您可能想阅读非事务性数据访问和自动提交模式来阐明一些事情,但简单地说,您的Hibernate代码必须在事务中执行,我建议使用显式事务(即明确的 beginTransaction commit )。

You might want to read Non-transactional data access and the auto-commit mode to clarify a few things but, to put it simply, your Hibernate code has to be executed within a transaction and I'd suggest to use explicit transaction boundaries (i.e. explicit beginTransaction and commit).


另外,在servlet中使用会话的线程问题是什么,因为我看到会话在api中不是线程安全的。

Also what's the thread problem about using session in servlet, since I saw the session is not thread safe in api.

只是不要让它成为Servlet的实例变量你不会有任何问题。

Just don't make it an instance variable of the Servlet and you won't have any problem.


  • Hibernate Core 3.3参考指南

    • Hibernate Core 3.3 Reference Guide
      • Chapter 11. Transactions and Concurrency
      • Sessions and transactions
      • Non-transactional data access and the auto-commit mode

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

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