Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create Hibernate-Session per Request

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!

like image 461
duselbaer Avatar asked Apr 24 '12 08:04

duselbaer


People also ask

How do I enable session per conversation?

open a conversation (=> the session is created with flush mode set to MANUAL and bind it in the ManagedSessionContext that held the current session) call the addObject() service which gets the conversation session, opens a transaction, create an entity, save it, disconnect the session and finally commit the transaction.

Can we create multiple session in hibernate?

Once we will have a SessionFactory object then we can have a Session object. We can have multiple SessionFactory for one application. That's all about SessionFactory and Session in hibernate.

What is session per request?

Session-per-request pattern. This is the most common transaction pattern. The term request here relates to the concept of a system that reacts to a series of requests from a client/user. Web applications are a prime example of this type of system, though certainly not the only one.

Is SessionFactory Singleton in hibernate?

@DarkHorse SessionFactory is not Singleton , it is used as singleton , it is immutable docs.jboss.org/hibernate/orm/3.5/javadocs/org/hibernate/…


1 Answers

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!

like image 166
sharakan Avatar answered Oct 04 '22 04:10

sharakan