Let's remember good old EJB's. Inject an EntityManager in a session
bean facade and EJB container will handle all concurrency issues. No
need to write any boilerplate code.
BTW I have seen that some people use OpenEntityManagerInViewFilter and
map it to url pattern "/*". It seems that an EntityManager would be
created every time a static web resource is accessed. Isn't that terrible ?
Maris
Uwe Schäfer wrote:
i plan to use JPA together with Wicket. Are there any battle-proven
best-practices out there of how to handle EnityManagers and
Transactions?
What do other people use (no, not the spring crowd ;) ?
One EntityManager per Request seems to be the obvious idea and a
guice Provider may help with that. Does anyone have serious
experiences with that or other suggestions?
Subclass WebRequestCycle, and construct it with an EntityManager.
In onBeginRequest(), create a transaction, storing it in a private
variable.
In onEndRequest(), commit the transaction if it hasn't already been
rolled back and clean up the EntityManager.
In onRuntimeException(...), rollback the transaction and then return
super.onRuntimeException(...).
You'll need to inject a Provider<EntityManager> into your
WebApplication subclass such that you can call .get() on it when you
make your custom WebRequestCycle. You'll need to configure that
provider in a Guice module somewhere, obviously. You should be able to
figure that out. ;-)
How do you inject things into your WebApplication? Well, you can use
the new GuiceWebApplicationFactory, which unfortunately didn't quite
make it into 1.3.0-rc1 but is in trunk. However, you can copy that
class from trunk and use it in your own project quite happily - it has
no extra dependencies.
If you need to integrate with your container-managed JTA transactions
you'll need something a little different (the container may well start
and commit the tx for you), but the principle is roughly the same.
Alternatively, you can take Wicket out of the picture and do stuff
using a ServletFilter (like Spring's OpenEntityManagerInView).
Regards,
Al
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]