updated
Ok, after years of programming I found myself to lack knowledge of how to use JPA properly in your webapp in cases if you use session-scoped forms and lazy loading and deferred commits.
The case is as follows:
- Web-app forms are generated once opened and stay session-scoped until explicitly closed or session is destroyed. So, multiple get requests to the same form do nothing but render the form itself.
- Form allows user to view/edit bean or hierarchy of beans of the same root (say, User, UserDocuments[] and UserSecurityGroup, last two are referenced from User).
- One could open child object from a form of a root object. Children objects are stored in same transaction as the root object is (
em.persist(rootObject);
usingCascade.ALL
). - One could cancel changes made in root/children form until latter is closed.
- Components on a form could reference children objects using lazy loading.
- Components have custom (configurable) logic (OnClick, OnChange, OnLoad, etc) and can reference (read or modify) latest modified version of edited bean.
According to the brief explanation at TomEE site http://tomee.apache.org/jpa-concepts.html, one can not use JTA persistence in webapp, so, given the fact that lazy loading works only when it's persistence context is opened, one should do all lazy-loading stuff before em.close()
. This fact prevents me from implementing points 3 and 6, if I use request-scoped EntityManager. I could solve it by moving EntityManager at session level, but if I do so, I can't satisfy point 4 - changes to children object already been made, object is still managed after form closed. If I move EntityManager to form-scope, application will become jdbc-resource intensive (or not? Does an entity manager create a connection to the database?, and OpenJPA settings at http://openjpa.apache.org/builds/1.0.2/apache-openjpa-1.0.2/docs/manual/ref_guide_dbsetup_retain).
What other alternatives do I have?