Hibenate and Jsf (using also Ajax4Jsf)

Hi,
untill now I always user the 1 Session per Session pattern to manage Hibernate session in JSF.

Today I tried to convert an existing application (JSF 1.2, Facelets, Ajax4Jsf, RichFaces) to 1 Session per Request pattern.

I installed the ORM Filter that open and close the session, this works good in non-ajax application: when I want to use a detached object I just call session.lock(obj, LockMode.NONE) to reattach it after using.

Now there are problems in this application. I didn’t understand very good where is the trick but in some case it did not close the session (it seems that if I make an ajax request the orm filter is bypassed, so in the next request when I call lock hibernate give me a NonUniqueObject exception).

I tried to lock before using it and evict after, but there is some problem with evict cascade (I set the configuration but I don’t know why it seems it doesn’t work, so I have to manually evict all the subobject I used and loaded with lazy fetch). Also I tried to clear the cache after using the reattached objects, but I have problems.

Is there a better pattern? May I can use a phase listener so that the close session works for ajax and non-ajax request? And is it good to close and re-open the session in every ajax request?

Thanks a lot for you answers and sorry for making so many! :smiley:

Demetrio

P.S.: I didn’t find a way to know if an object is detached or not (so I can decide to call or not the lock method).

Hello Demetrio,

Thank you for your post.

If each AJAX request is not related (e.g. only query some data), it is recommended to close the session every time. You can close session at the end of each request, or any other ways that make sure it is called at the end of each request.

Hope this helps. Please feel free to contact me if there are any questions.

Best regards,
Jick

I tried the close every time method but it doesn’t work for me, also may be because I have object I need to re-use (not just query)…

I have a lot of exceptions…is there a well defined pattern on how to do that?

how can I do?

Also even if I close the session if I load data out of hibernate I can’t see it in hibernate until I disconnect the PM or restart the application, is that a normal behaviour?

Demetrio

hi Demetrio.

You need to load object with new session or lock it with new session.

Also even if I close the session if I load data out of hibernate I can’t see it in hibernate until I disconnect the PM or restart the application, is that a normal behaviour?

Sorry, but we do not quite get. Would you mind explaining this point a little bit? Thank you in advance!

Best regards,
Jick

Hi, thanks for the answer, I tried those things:

  1. Install the orm filter and just use lock before using the deteached objects. I have problems with this coz objects were sometime deteached sometimes not, and it seems there is no way to understand if an object is deteached or not.

  2. Install the orm filter, use lock before using the object and close the session after every use of the session (also when I make a query, after storing the collection I close the session so I detach the objects). This I don’t know why it doent’s work! Sometimes it works other times not and guve me various Exceptions.

  3. As the point 2 but without installing orm filter, it still give various problems.

About the point you want more explanation: if I execute an external update query on the same database (opened by hibernate) I can see the updated data only if I restart tomcat or call the method disposePersistentManager(), is there a way to get the new external data without disposing the persisten manager?

Thanks

Demetrio

Hello Demetrio,

In response to your reply…

  1. It should be attached if called lock.

  2. Session need to keep opening when working with collection, because it is lazy when loading from db.

  3. Without filter, you can close session manually.

The data should be refreshed by either:

  1. session.refresh
  2. close session and query in new session

Hope this helps.

Best regards,
Jick

Hi,

here the answers:

  1. I called lock but it give me problems because sometimes it is detached before calling lock, sometimes it is not. I think the problem is with the collections.

  2. So how can I do? I use ArrayList with JSF to list collections, those collection stays opened also between request so the session will be closed by the orm filter.

  3. I did it: I closed the session manually but I got a lot of problems too, maybe is because I close the session also after getting a collection.

I have to use lock coz the data I get are modified by JSF so I don’t want to refresh it.

is there a pattern to use Hibernate with JSF?

thanks

Demetrio

Hi Demetrio,

If you need to use the collection and close session, you can call any method on the collection, e.g. .size(), which result in initializing the collection. But it will still have problem if the collection element itself has collection/lazy properties that will be used after close session.

Session per request may be suitable to you - keep session open until end of request.

Best regards,
Jick

Hi,
I would like to use session per request (now I’m using session per session) but I store some collections in the http session (using a managed bean) so I have those problems…

Demetrio