Create Hibernate-Session per Request

12,360

Solution 1

Hibernate's current recommended approach for managing Sessions is detailed on this wiki page. In particular, I think you need to read the last paragraph: This is all very difficult, can't this be done easier?

In the end, you do need to tell the persistence layer that "I'm about to do something" (which usually also gets the Session to do it with) and "I'm done doing it". You can do it with annotations, or JTA transactions, but that information still has to be communicated!

Solution 2

Inject SessionFactory to your Data Access Object and use sessionFactory.getCurrentSession() to access Hibernate Session object. you can make use of any of the Factory classes available to implement this..

Then your code should look like this..

sessionFactory.getCurrentSession().save(newInstance); 
Share:
12,360
duselbaer
Author by

duselbaer

Updated on July 21, 2022

Comments

  • duselbaer
    duselbaer almost 2 years

    I just started a simple Java testproject which manages some entities using Hibernate and provides a REST interface to manipulate these objects and provide some additional business logic. The REST interface is created using RESTEasy and Jetty.

    Everything works fine so far, but I have the feeling that I'm actually writing too much boilerplate code. As I don't have much experience in these Java frameworks I'm just wondering if anyone could give me a hint on how to improve the situation.

    1. Creting Hibernate Sessions per Request

    Well, as far as I understood I have to create a Hibernate session per request and at the end I have to close it. So currently all of my service methods look like this:

    Session session = HibernateUtil.getInstance().getSessionFactory().openSession();
    ...
    ...
    ...
    session.close();
    

    Is there any way to remove these two lines in order to somehow do this automatically? Currently my service is registered as a RestEASY singleton. Will changing to a RESTeasy ressource and creating the session in the constructor solve the problem? I think it will solve the problem of creating the session. But wherer to close it?

    In C++ this can easily done be creating a scoped object which closes the session at the end. But in Java?

    1. Whenever such a REST request is made I have to check for a valid session (the user has to login previously). Is a ServletFilter the right way to do this?

    General: Are there any other patterns or frameworks I should consider using? I mean I want to keep it as simple as possible and especially as I dont have that much experience I dont want to end up using Spring or whatever heavyweight framework. Seems that I'm used to the simplicity of Python and Django but for this little project I have to use Java.

    THanks so far!

  • duselbaer
    duselbaer about 12 years
    Ok, but where do I close the session?
  • Ankit Sharma
    Ankit Sharma over 9 years
    In case of struts2, u can use interceptors to initialize and close the session object.
  • Kumar Manish
    Kumar Manish about 2 years
    All the links are broken.