users@glassfish.java.net

Transaction Scope - Web Application with EJB Project

From: <glassfish_at_javadesktop.org>
Date: Sun, 26 Apr 2009 13:44:23 PDT

Hello,
I'm new to GlassFish and J2EE so I hope I'm asking it right.
I have an enterprise application which contain Web module and EJB Module.
The EJB is the business logic which contain entity classes as well as session (currently only stateless) bean which manage them.
There is nothing to fancy here, mostly boilerplate code for creating, updating, getting and deleting the entities.

The Web module is a RESTful interface to the EJB module.
I have resource and converter (item-container pattern) which expose the entities.

Everything is working good except of the transaction management (I think) and I'll explain.
I'm experiencing two issues: one is I get an LazyInitializationException when accessing a collection of a loaded entity in the web module. For example, I'm loading Process entity which has collection (OneToMany relation) of Activity entity. When I call getActivities on the Process I get the exception.
As I understand it means that the entity manager which was used to load the Process entity is close by now.

The other issue is when I try to delete an Activity entity. Before deleting it I remove it from the activities collection of the Process and set the back-pointer from Activity to Process to null. But this fail too. If I load the Process and Activity entities (using EntityManager.find) inside the EJB module everything works great.

------

So, as far as I understand the entity manager is bound to a transaction. It means that if the transaction will start when the http request start and will end when the response is sent to the client I'll have an entity manager for the duration of the request which will give me answer to both of the problems: lazy loading (since the entity manager will be around long enough) and the entities that are loaded and return to the web module can be return to the ejb module and still be valid.

The questions are: Am I right? and if so how can I make the transaction span the whole http request and not just the EJB method invocations?

Thank you,
Ido.
[Message sent by forum member 'ido_ran' (ido_ran)]

http://forums.java.net/jive/thread.jspa?messageID=343859