users@jersey.java.net

Re: [Jersey] Jersey and JPA best practices?

From: Paul Sandoz <Paul.Sandoz_at_Sun.COM>
Date: Mon, 05 Jan 2009 11:48:03 +0100

On Dec 22, 2008, at 12:12 PM, Florian Hars wrote:

> Paul Sandoz schrieb:
>> Complete injection support equivalent to that on Servlet is not
>> currently implemented.
>
> That might explain why it didn't work :-). Anyway, what are the thread
> safety properties of jersey resources?

It depends on the life-cycle of the resource. By default resource
classes are per-request so you could inject something that is not
thread safe in such cases.


> Would it even be useful to inject
> an EntityManager or would you run into concurrency issues?
>

It could work for per-request resources.


>> What servlet class are you using for your web application?
>
> Ah, that is the secret sauce, you have to use
> com.sun.jersey.server.impl.container.servlet.ServletAdaptor
> instead of
> com.sun.jersey.spi.container.servlet.ServletContainer
> if you want it to give you an EntityManagerFactory.
>
> Maybe somebody should add an item to the FAQ that explains the
> following
> points:
>
> 1. You must use the mentioned ServletAdaptor class.
> 2. You must specify the persistence unit as an init-param
> to the servlet.
> 3. You can only inject via @PersistenceUnit EntityManagerFactory emf
> = ...
> 4. How to use EntityManagers created by that Factory to avoid
> threading problems in your jersey resources
> 5. How to use transactions in jersey resouces.
> (have I missed anything?)
>

I think that is it.


> The answers are all implicit in the bookmarks example, but it might
> help
> people if they are written down with some short explanations.
>

I admit to being a little vague about this because it uses some
annoying workarounds in the web.xml and what we really need for
developers is proper EE integration.

Paul.

> - Florian.
>
> PS: Koan for today:
> After making the change, the glassfish first gave me:
>
> java.lang.ClassCastException: com.example.jpa.User cannot be cast to
> com.example.jpa.User
>
> After playing around and finally redeploying the same code,
> everything works. Classloader madness?
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe_at_jersey.dev.java.net
> For additional commands, e-mail: users-help_at_jersey.dev.java.net
>