3

I have developed a simple app with spring boot and hibernate. I used to get sessions by sessionfactory.opensession(); does it need to close session manually after all the things done?

Is it good idea to use opensession() rather than getcurrentsession() in multiple requests web app?

asanka12
  • 43
  • 1
  • 7
  • Possible duplicate of [Control the hibernate session(when to close it manually)](https://stackoverflow.com/questions/4040761/control-the-hibernate-sessionwhen-to-close-it-manually) – H.Tibat May 27 '18 at 03:48
  • 1
    You shouldn't do any of this. Use `EntityManager` instead of `Session` (`Session` now even extends `EntityManager`), inject it with `@PersistenceContext`, and use `@Transactional` to handle everything for you. Even better, you can use Spring Data JPA to autogenerate most of your DAOs for you. – chrylis -cautiouslyoptimistic- May 27 '18 at 03:55
  • @chrylis earlier I have used Spring JAP , now I need to do it through Hibernate. – asanka12 May 27 '18 at 09:26
  • @chrylis let's say for passionate. – asanka12 May 29 '18 at 14:26
  • In that case, I really recommend not dealing with it. It's a discouraged API that is now supported only for legacy reasons, and the JPA API is the recommended use case. – chrylis -cautiouslyoptimistic- May 29 '18 at 17:35
  • @chrylis oh seriously, i thought it would be useful cause in orm section of Spring data doc, it has been written about hibernate spring integration. – asanka12 May 29 '18 at 17:44
  • That's primarily for legacy code (the project I'm working on now is still using Hibernate 3.5, and we're going to be putting in a lot of effort to modernize to JPA 2.1). Hibernate itself has moved to JPA interfaces as their "native" API. – chrylis -cautiouslyoptimistic- May 29 '18 at 20:01
  • @chrylis thanks for informing.btw do you know way to save or update entity (if exsited update and if new just persist) using JPA repository. Currently I'm doing at first looking for entity by using find, if not decided to persist.any otherway? – asanka12 May 30 '18 at 05:57

1 Answers1

2

It should be closed when you're done with (but this can be done automatically for you as we'll see). It all depends on how you obtain the session.

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

For more info you can check http://docs.jboss.org/hibernate/core/3.3/reference/en/html/transactions.html#transactions-basics-apptx

Well, an edit for you:

SessionFactory.openSession() always opens a new session that you have to close once you are done with the operations. SessionFactory.getCurrentSession() returns a session bound to a context - you don't need to close this.

Edit2

hibernate.current_session_context_class to thread and then implement something like a servlet filter that opens the session - then you can access that session anywhere else by using the SessionFactory.getCurrentSession().

SessionFactory.openSession() always opens a new session that you have to close once you are done with the operations. SessionFactory.getCurrentSession() returns a session bound to a context - you don't need to close this.

If you are using Spring or EJBs to manage transactions you can configure them to open / close sessions along with the transactions.

You should never use one session per web app - session is not a thread safe object - cannot be shared by multiple threads. You should always use "one session per request" or "one session per transaction"

Adya
  • 1,084
  • 9
  • 17
  • @asanka12 did this answer satisfied your query? – Adya May 27 '18 at 03:11
  • Thanks @Adya for answer. It's helpful for me. Btw I'm in vegue context, due to spring container. I need to clarify whether spring close the session for me if I get session from opensession – asanka12 May 27 '18 at 03:30
  • @asanka12 I have added something more in ans, pls check and accept it as answer if it satisfies your query – Adya May 27 '18 at 03:41
  • Can u pls accept it as answer by checking the right tick? – Adya May 27 '18 at 08:09
  • if hibernate use in a muti threads environment like web application, do I need to use opensession or getcurrentsession ? – asanka12 May 31 '18 at 17:50
  • @asanka12 since your question needs explanation so I am edititng answer by adding content. You can read. – Adya Jun 01 '18 at 01:01