users@jersey.java.net

Re: [Jersey] Does JPA-injection in Jersey really work?

From: Paul Sandoz <Paul.Sandoz_at_Sun.COM>
Date: Tue, 06 Jan 2009 11:17:14 +0100

On Jan 5, 2009, at 6:22 PM, saturon wrote:

>
> Hi Paul,
>
> As mentioned I use jersey 1.0.1.
>

The NB project is using Jersey 1.0 because that is what NBs uses.

For 1.0 the ServletAdapter is:

   com.sun.jersey.impl.container.servlet.ServletAdaptor

For 1.0.1 the ServletAdaptor is:

   com.sun.jersey.server.impl.container.servlet.ServletAdaptor

I modified the web.xml of your project to use the ServletAdaptor for
1.0 and it worked. See below for the web.xml.

Paul.

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<web-app>
     <servlet>
         <servlet-name>Jersey Web Application</servlet-name>
         <servlet-
class>com.sun.jersey.impl.container.servlet.ServletAdaptor</servlet-
class>
         <init-param>
             <param-name>com.sun.jersey.config.feature.Redirect</param-
name>
             <param-value>true</param-value>
         </init-param>
         <init-param>
             <param-name>unit:JerseyInjectionTestPU</param-name>
             <param-value>persistence/injectionTest</param-value>
         </init-param>
         <load-on-startup>1</load-on-startup>
     </servlet>
     <servlet-mapping>
         <servlet-name>Jersey Web Application</servlet-name>
         <url-pattern>/resources/*</url-pattern>
     </servlet-mapping>
     <session-config>
         <session-timeout>30</session-timeout>
     </session-config>
     <welcome-file-list>
         <welcome-file>
             index.jsp
         </welcome-file>
     </welcome-file-list>
     <persistence-unit-ref>
         <persistence-unit-ref-name>persistence/injectionTest</
persistence-unit-ref-name>
         <persistence-unit-name>JerseyInjectionTestPU</persistence-
unit-name>
     </persistence-unit-ref>
     <resource-ref>
         <res-ref-name>UserTransaction</res-ref-name>
         <res-type>javax.transaction.UserTransaction</res-type>
         <res-auth>Container</res-auth>
     </resource-ref>

     <persistence-context-ref>
         <persistence-context-ref-name>persistence/
JerseyInjectionTestPU</persistence-context-ref-name>
         <persistence-unit-name>JerseyInjectionTestPU</persistence-
unit-name>
     </persistence-context-ref>
</web-app>


> I now just do jndi lookup of the entitymanager and forgot about
> injection, since this seems to me an open point with the EE
> integration.
>
> Attached is a nb project as zip file to reproduce the issue.
>
> Rgds
>
> -----Original Message-----
> From: Paul.Sandoz_at_Sun.COM [mailto:Paul.Sandoz_at_Sun.COM]
> Sent: Montag, 5. Januar 2009 11:52
> To: users_at_jersey.dev.java.net
> Subject: Re: [Jersey] Does JPA-injection in Jersey really work?
>
>
> Hi,
>
> What version of Jersey are you using?
>
> Did you make any progress? did you get the bookmark example working?
>
> It is hard to know what exactly is going on. If you could send a
> simple reproducible example as a zip file we could have a look at it.
>
> Paul.
>
> On Dec 24, 2008, at 6:08 AM, saturon wrote:
>
>> Hello,
>>
>> Well I just cannot get jersey to inject entitymanagerfactory (JNDI
>> lookup works though...).
>>
>> - I use glassfishv2ur2, jersey 1.0.1, toplinkessentials
>> - I have the exact same configuration as in the bookmark example
>> EXCEPT:
>>
>> Using:com.sun.jersey.server.impl.container.servlet.ServletAdaptor in
>> web.xml gives me: ...
>> Weaver processing class [foo.model.ent.NlUser].
>> end predeploying Persistence Unit NightlifeServer2PU; state
>> Predeployed;
>> factoryCount 1
>> PWC1412: WebModule[/NightlifeServer2]-ServletContext.log():PWC1409:
>> Servlet ServletAdaptor wird als nicht verfügbar markiert
>> WebModule[/MyServer2]Error loading WebappClassLoader
>> delegate: true
>> repositories:
>> /WEB-INF/classes/
>> ----------> Parent Classloader:
>> EJBClassLoader :
>> urlSet = []
>> doneCalled = false
>> Parent -> java.net.URLClassLoader_at_e5f46e
>> com.sun.jersey.server.impl.container.servlet.ServletAdaptor
>> java.lang.ClassNotFoundException:
>> com.sun.jersey.server.impl.container.servlet.ServletAdaptor
>>
>> Using the com.sun.jersey.spi.container.servlet.ServletContainer in
>> web.xml and my EntityManagerFactory is null. I use
>>
>> @PersistenceUnit(unitName = "MyPU")
>> EntityManagerFactory emf;
>>
>> And then im my get of the resource (exactyl like in the bookmark
>> example):
>> final EntityManager em = emf.createEntityManager();
>> TransactionManager.manage(new Transactional(em) { public void
>> transact() {
>> em.merge(userConfig);
>> }});
>>
>> So I am not seeing what im doing wrong here.
>>
>> -----Original Message-----
>> From: Florian Hars [mailto:hars_at_bik-gmbh.de]
>> Sent: Montag, 22. Dezember 2008 12:13
>> To: users_at_jersey.dev.java.net
>> Subject: Re: [Jersey] Jersey and JPA best practices?
>>
>>
>> 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? Would it even be useful to
>> inject
>> an EntityManager or would you run into concurrency issues?
>>
>>> 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?)
>>
>> The answers are all implicit in the bookmarks example, but it might
>> help
>> people if they are written down with some short explanations.
>>
>> - 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
>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: users-unsubscribe_at_jersey.dev.java.net
>> For additional commands, e-mail: users-help_at_jersey.dev.java.net
>>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe_at_jersey.dev.java.net
> For additional commands, e-mail: users-help_at_jersey.dev.java.net
> <
> JerseyInjectionTest
> .zip
> >---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe_at_jersey.dev.java.net
> For additional commands, e-mail: users-help_at_jersey.dev.java.net