Java 什么时候关闭休眠状态下的数据库连接?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24906834/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 15:31:29  来源:igfitidea点击:

When to close database connection in hibernate?

javahibernatesession

提问by Fresher

Firstly it may sound like a duplicate question but i didn't get solution i was expecting so I am posting this new question?I have started learning hibernate a couple of days ago.I am stuck on this 1 thing:

首先,这听起来像是一个重复的问题,但我没有得到我期待的解决方案,所以我发布了这个新问题?几天前我开始学习休眠。我被困在这件事上:

Here is my code:

这是我的代码:

public static void open_connection()

sessionfactory=new Configuration().configure().buildSessionFactory();
Listsession = sessionfactory.openSession();
}

public  List select(String qry)
{ 
    open_connection();  
    Listsession.beginTransaction();
    query =Listsession.createQuery(qry);
    list=query.list();
    Listsession.getTransaction().commit();
    Listsession.close();
    sessionfactory.close();
    }

Q1.I have closed the sessionfactory when a query has run.Is it a good approach?I want to close database connection when we don't need it as we do in JDBC(My teacher taught me that).

一季度。我在查询运行时关闭了 sessionfactory。这是一个好方法吗?我想在我们不需要时关闭数据库连接,就像我们在 JDBC 中所做的那样(我的老师教过我)。

Q2.Should I close connection when user is getting logout from my site?

Q2。当用户从我的网站注销时,我应该关闭连接吗?

Q3.Will sessionfactory.close(); also destroy my session variable(session.setattribute("user",ur);).

Q3。将 sessionfactory.close(); 还销毁我的会话变量(session.setattribute("user",ur);)。

Q4.Does Listsession.getTransaction().commit(); also close the transaction?

第 4 季度。Listsession.getTransaction().commit(); 还关闭交易?

I want to know this because many times i run my project on netbeans i am getting null pointer exception but when i run same project online i don't get null pointer exception and i think this happens because openconnection is called everytime i run my project. Sorry for posting so many questions as i couldn't get exact answers i was looking for.

我想知道这一点,因为很多次我在 netbeans 上运行我的项目时,我都遇到了空指针异常,但是当我在线运行同一个项目时,我没有遇到空指针异常,我认为这是因为每次运行我的项目时都会调用 openconnection。很抱歉发布了这么多问题,因为我无法得到我正在寻找的确切答案。

采纳答案by Mr.Chowdary

1.You should close the Sessionbut not SessionFactory
2.You are already closing Sessionafter executing query, so where is the point of again closing when logoutfrom site ?
3.HttpSessionis different from Session in Hibernate. HttpSessionfor storing attributes for maintaining user sequence of requests. But Sessionin Hibernateto interact with Databaseonly. So closing Sessionin Hibernatedoesn't reflect on HttpSession.
4.If you are using openSession(), you should close the session manually.But if you are using getCurrentSession(), you don't need to bother of it, once transaction committed, session will be closed automatically.
Hope it helps,

1.您应该关闭Session但不是SessionFactory
2.您Session在执行查询后已经关闭,那么logout从站点再次关闭的重点在哪里?
3.HttpSessionHibernate 中的Session 不同。HttpSession用于存储用于维护用户请求序列的属性。但SessionHibernate中只能与数据库交互。所以SessionHibernate中关闭不会反映HttpSession.
4.如果你正在使用openSession(),你应该手动关闭会话。但是如果你正在使用getCurrentSession(),你就不用管它了,一旦事务提交,会话将自动关闭。
希望能帮助到你,