Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

When to close database connection in hibernate?

 
Prashant Soood
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is my code:



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).

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

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

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

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.
 
Campbell Ritchie
Sheriff
Posts: 51388
87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please tell us where you get the Exception. We need more details before we can help.
 
Prashant Soood
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Please tell us where you get the Exception. We need more details before we can help.

Just ignore the exception i just want to know the answer of questions posted above as i was unable to find any relevant answers on google.
 
E Armitage
Rancher
Posts: 989
9
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).
You don't directly open or close database connections when using Hibernate, it will close them for you. You do however open and close its session factory and session objects.
The correct approach to closing any closeable resources in Java is to do so in a finally block or (use the using clause) so that they are closed even if an exception occurs.
There should be only one SessionFactory in an application that you only close when the application is shutdown. Throughout the application you will be creating Sessions using that factory.


Q2. Should I close connection when user is getting logout from my site?
No, you only close the factory when the application shuts down.


Q3. Will sessionfactory.close(); also destroy my session variable(session.setattribute("user",ur);).
No. The hibernate session is not the same as the web session. Hibernate is a persistence framework which can work with or without web applications.

Q4. Does Listsession.getTransaction().commit(); also close the transaction?
No, you have to close by calling close


You should read the below links:
http://docs.jboss.org/hibernate/orm/4.1/javadocs/org/hibernate/SessionFactory.html
http://docs.jboss.org/hibernate/orm/4.1/javadocs/org/hibernate/Session.html

 
Prashant Soood
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
E Armitage wrote: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).
You don't directly open or close database connections when using Hibernate, it will close them for you. You do however open and close its session factory and session objects.
The correct approach to closing any closeable resources in Java is to do so in a finally block or (use the using clause) so that they are closed even if an exception occurs.
There should be only one SessionFactory in an application that you only close when the application is shutdown. Throughout the application you will be creating Sessions using that factory.


Q2. Should I close connection when user is getting logout from my site?
No, you only close the factory when the application shuts down.


Q3. Will sessionfactory.close(); also destroy my session variable(session.setattribute("user",ur);).
No. The hibernate session is not the same as the web session. Hibernate is a persistence framework which can work with or without web applications.

Q4. Does Listsession.getTransaction().commit(); also close the transaction?
No, you have to close by calling close


You should read the below links:
http://docs.jboss.org/hibernate/orm/4.1/javadocs/org/hibernate/SessionFactory.html
http://docs.jboss.org/hibernate/orm/4.1/javadocs/org/hibernate/Session.html




So that means i should just write Listsession.close() to close the transaction in select method and close sesssionfactory when user logouts.Are there any other session objects that i need to close before logout.
 
E Armitage
Rancher
Posts: 989
9
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you close the factory a the user logs out then what happens to other users still logged in? Remember I said that typically you have one SessionFactory per application. Read those links.
 
Prashant Soood
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
E Armitage wrote:If you close the factory a the user logs out then what happens to other users still logged in? Remember I said that typically you have one SessionFactory per application. Read those links.


Ok Sorry my bad i misunderstood it so i just close the session object every time i have run a query right.I am making a social networking website so I shouldn't close the sessionfactory.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic