1 2 Previous Next 22 Replies Latest reply on Apr 13, 2009 5:52 AM by sannegrinovero Go to original post
      • 15. Re: Why is injection not working in AS 4.0 and 4.2 (servlets
        zyborg

        hi all!

        my application is packed into a .ear file and gets deployed into a jboss 5 beta2.
        i am trying to inject a EntityManager from a servlet without success ...
        should my scenario work or is there something i misunderstood?
        maybe its just a deployment failure - so i packaged my ear file note correctly?
        i ran out of ideas ... please help!

        the ear file structure looks like:

        META-INF/
         |-- jboss-app.xml
         |-- application.xml
        beans.jar
         |--META-INF/
         |--persistence.xml
         |--com/
         |--myapp/
         |--...
        web.war
         |--WEB-INF/
         |-- classes/
         |-- lib/
         |--web.xml
        


        jboss-app.xml:
        <!DOCTYPE jboss-app
         PUBLIC "-//JBoss//DTD J2EE Application 1.4//EN"
         "http://www.jboss.org/j2ee/dtd/jboss-app_4_0.dtd">
        <jboss-app>
         <loader-repository>
         com.myapp:loader=MyApp.ear
         </loader-repository>
        </jboss-app>
        


        application.xml:
        <?xml version="1.0" encoding="UTF-8"?>
        <application>
         <display-name>My Application Title</display-name>
         <description>My Application Title Description</description>
        
         <module>
         <ejb>beans.jar</ejb>
         </module>
        
         <module>
         <web>
         <web-uri>web.war</web-uri>
         <context-root>MyApp</context-root>
         </web>
         </module>
        </application>
        


        the injection code in the servlet looks like:
         @PersistenceContext(unitName="MyUnit")
         private EntityManager em;
        



        On deployment i get following error:
        java.lang.RuntimeException: @PersistenceContext(name='env/com.myapp.server.MyServiceImpl/em',unitName='MyUnit') on EJB: web.war failed to inject on field private javax.persistence.EntityManager com.myapp.server.MyServiceImpl.em Unable to find persistence unit: MyUnit for deployment: web.war
         at org.jboss.injection.PcEncInjector.inject(PcEncInjector.java:85)
         at org.jboss.web.tomcat.service.TomcatInjectionContainer.populateEnc(TomcatInjectionContainer.java:213)
         at org.jboss.web.tomcat.service.deployers.TomcatDeployment$EncListener.lifecycleEvent(TomcatDeployment.java:471)
         at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:117)
         at org.apache.catalina.core.StandardContext.start(StandardContext.java:4356)
         at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:761)
         at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:741)
         at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:553)
         at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
         at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
         at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
         at java.lang.reflect.Method.invoke(Method.java:585)
         at org.apache.tomcat.util.modeler.BaseModelMBean.invoke(BaseModelMBean.java:297)
         at org.jboss.mx.server.RawDynamicInvoker.invoke(RawDynamicInvoker.java:164)
         at org.jboss.mx.server.MBeanServerImpl.invoke(MBeanServerImpl.java:668)
         at org.apache.catalina.core.StandardContext.init(StandardContext.java:5312)
         at org.jboss.web.tomcat.service.deployers.TomcatDeployment.performDeployInternal(TomcatDeployment.java:298)
         at org.jboss.web.tomcat.service.deployers.TomcatDeployment.performDeploy(TomcatDeployment.java:138)
         at org.jboss.web.deployers.AbstractWarDeployment.start(AbstractWarDeployment.java:367)
         at org.jboss.web.deployers.WebModule.startModule(WebModule.java:104)
         at org.jboss.web.deployers.WebModule.start(WebModule.java:80)
         at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
         at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
         at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
         at java.lang.reflect.Method.invoke(Method.java:585)
         at org.jboss.mx.interceptor.ReflectedDispatcher.invoke(ReflectedDispatcher.java:157)
         at org.jboss.mx.server.Invocation.dispatch(Invocation.java:96)
         at org.jboss.mx.server.Invocation.invoke(Invocation.java:88)
         at org.jboss.mx.server.AbstractMBeanInvoker.invoke(AbstractMBeanInvoker.java:264)
         at org.jboss.mx.server.MBeanServerImpl.invoke(MBeanServerImpl.java:668)
         at org.jboss.system.microcontainer.ServiceProxy.invoke(ServiceProxy.java:184)
         at $Proxy0.start(Unknown Source)
        


        • 16. Re: Why is injection not working in AS 4.0 and 4.2 (servlets
          wolfgangknauf

          Hi Zyborg,

          I think persistence.xml has a module scope. If you need it in the WAR file, you have to add one. You cannot access the one of the EJB jar.

          Hope this helps

          Wolfgang

          • 17. Re: Why is injection not working in AS 4.0 and 4.2 (servlets
            zyborg

            Thanks for your hint Wolfgang!

            I tried to put the persitence.xml everywhere in the war file without success.
            WEB-INF/persistence.xml
            WEB-INF/META-INF/persistence.xml
            WEB-INF/classes/persistence.xml
            WEB-INF/classes/META-INF/persistence.xml

            Maybe i also should have posted my persistence.xml:

            <persistence>
             <persistence-unit name="MyUnit">
             <jta-data-source>java:/MySqlDS</jta-data-source>
             <properties>
             <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect"/>
             <property name="hibernate.hbm2ddl.auto" value="create-drop"/>
             <property name="jboss.entity.manager.jndi.name" value="java:/MyUnit"/>
             <property name="jboss.entity.manager.factory.jndi.name" value="java:/MyUnitFactory"/>
             </properties>
             </persistence-unit>
            </persistence>
            


            tried it with and without the jndi properties. :/

            • 18. Re: Why is injection not working in AS 4.0 and 4.2 (servlets
              wolfgangknauf
              • 19. Re: Why is injection not working in AS 4.0 and 4.2 (servlets
                yilmaz_

                to get EntityManager you have to use EntityManager em=(EntityManager)Component.getInstance("entitymanagername")

                but to access EM you have to configure components.xml first.

                Until you got JEE5 app server anotations may not work.

                I dont have config files. I will post my configuration files couple hours later.

                • 20. Re: Why is injection not working in AS 4.0 and 4.2 (servlets
                  yilmaz_

                  if you are using Seam Add this line to your component.xml

                  <?xml version="1.0" encoding="UTF-8"?>
                  <components xmlns="http://jboss.com/products/seam/components"
                  xmlns:core="http://jboss.com/products/seam/core"
                  xmlns:drools="http://jboss.com/products/seam/drools"
                  xmlns:security="http://jboss.com/products/seam/security"
                  xmlns:mail="http://jboss.com/products/seam/mail"
                  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                  xmlns:web="http://jboss.com/products/seam/web"
                  xsi:schemaLocation=
                  "http://jboss.com/products/seam/core http://jboss.com/products/seam/core-1.2.xsd
                  http://jboss.com/products/seam/drools http://jboss.com/products/seam/drools-1.2.xsd
                  http://jboss.com/products/seam/security http://jboss.com/products/seam/security-1.2.xsd
                  http://jboss.com/products/seam/mail http://jboss.com/products/seam/mail-1.2.xsd
                  http://jboss.com/products/seam/components http://jboss.com/products/seam/components-1.2.xsd
                  http://jboss.com/products/seam/web http://jboss.com/products/seam/web-1.2.xsd
                  ">
                  <core:init debug="true" jndi-pattern="@jndiPattern@"/>
                  <web:context-filter url-pattern="/yourservleturl/*"/>
                  <core:manager concurrent-request-timeout="500"
                  conversation-timeout="120000"
                  conversation-id-parameter="cid"
                  conversation-is-long-running-parameter="clr"/>

                  • 21. Re: Why is injection not working in AS 4.0 and 4.2 (servlets
                    yilmaz_

                    TestInterface testBean= (TestInterface) Component.getInstance(TestBean.class);
                    or
                    EntityManager entitiyManager=(EntityManager)Component.getInstance("entityManager");

                    • 22. Re: Why is injection not working in AS 4.0 and 4.2 (servlets
                      sannegrinovero

                      Hi, this sticky thread got a bit messy: Seam is not relevant in the discussion.

                      Injecting an EntityManager in a servlet is just wrong: servlets get concurrent requests and you really don't want an instance level EntityManager; Inject the EntityManagerFactory instead and use it to create a new entitymanager in method scope.
                      IMHO not supporting this in JBoss4 was a good idea.

                      1 2 Previous Next