11 Replies Latest reply on Mar 24, 2008 4:33 AM by kanumalla

    EntityManager injection in Webservice

    nudelaug

      I want an EntityManager to be injected into my WebService but that does not work:

      @WebService
      @SOAPBinding(style = SOAPBinding.Style.RPC)
      @HandlerChain(file = "handler.xml")
      public class DocumentSign
      {
       @PersistenceContext(name = "EF")
       EntityManager em;
      
       @WebResult(name = "signedDocument")
       public byte[] signDocument(@WebParam(name = "document")byte[] data)
       {
       System.out.println("em = " + em);
       //TODO implement signing
       return null;
       }
      }
      


      my web.xml:
      <web-app>
       <servlet>
       <servlet-name>DocumentSignService</servlet-name>
       <servlet-class>foo.DocumentSign</servlet-class>
       </servlet>
       <servlet-mapping>
       <servlet-name>DocumentSignService</servlet-name>
       <url-pattern>/sign/*</url-pattern>
       </servlet-mapping>
      </web-app>
      


      my persistence.xml in WEB-INF/META-INF:
      <persistence-unit name="EF" transaction-type="JTA">
       <jta-data-source>java:/EfDb.tst</jta-data-source>
       <class>at.oekb.wsportal.sapexportfinance.data.EFImportData</class>
       <properties>
       <property name="hibernate.archive.autodetection" value="class"/>
       <property name="hibernate.session_factory_name" value="java:/hibernate/EFSessionFactory"/>
       <property name="hibernate.show_sql" value="true"/>
       <property name="hibernate.format_sql" value="true"/>
       <property name="use_sql_comments" value="true"/>
       <property name="hibernate.dialect" value="org.hibernate.dialect.Oracle10gDialect"/>
       <property name="hibernate.cache.provider_class" value="org.hibernate.cache.HashtableCacheProvider"/>
       <property name="com.intellij.javaee.persistence.datasource" value="Datasource"/>
       </properties>
       </persistence-unit>
      


      I use jboss-4.2.1.GA.
      The EntityManager is not injected.
      Can anyone help me with this problem?

      Best regards,
      Wolf-Dietrich Rettensteiner

        • 1. Re: EntityManager injection in Webservice
          asoldano

          AFAIK the injection requires your class to be an EJB.

          • 2. Re: EntityManager injection in Webservice
            heiko.braun

            Right, injection only works on EJB endpoints.

            • 3. Re: EntityManager injection in Webservice
              froden

              I have a similar problem.

              The interface for the EJB:

              package services;
              
              import javax.ejb.Local;
              
              @Local
              public interface TestServiceInterface {
               public String getString();
              }


              The EJB itself:
              package services;
              
              import javax.ejb.Stateless;
              import javax.jws.WebMethod;
              import javax.jws.WebService;
              import javax.jws.soap.SOAPBinding;
              import javax.persistence.EntityManager;
              import javax.persistence.PersistenceContext;
              
              @Stateless
              @WebService
              @SOAPBinding(style=SOAPBinding.Style.RPC)
              public class TestService implements TestServiceInterface {
              
               @PersistenceContext
               private EntityManager em;
              
               @WebMethod
               public String getString() {
               System.out.println("em: " + em);
               return "Quite frankly, it gives me the heebie jeebies.";
               }
              }


              The web service is defined in my web.xml like this:

              <servlet>
               <servlet-name>TestService</servlet-name>
               <servlet-class>services.TestService</servlet-class>
               </servlet>
               <servlet-mapping>
               <servlet-name>TestService</servlet-name>
               <url-pattern>/TestService.ws</url-pattern>
               </servlet-mapping>
              </servlet>


              The web service works like it should, but the EntityManager is not injected. System.out.println("em: " + em); in the webmethod always prints a null.

              Any clues?

              • 4. Re: EntityManager injection in Webservice
                froden

                Sorry - forgot to mention that I use JBoss AS 4.2.0 with JBossWS 2.0.0.

                • 5. Re: EntityManager injection in Webservice
                  ropalka

                  Remove the web.xml file. It's not necessary because your webservice is EJB3 not POJO. You can use @WebContext annotation to specify/customize the request binding path

                  • 6. Re: EntityManager injection in Webservice
                    froden

                    Err, it actually turns out that I'm using JBossWS 1.2.1, not 2.0.0..

                    Regardless, I removed the and <servlet-mapping/> and added the annotation @WebContext(contextRoot="/ws") to the EJB.

                    The webservice is now deployed at localhost/TestServiceService/TestService. Shouldn't it be localhost/ws/TestService?

                    • 7. Re: EntityManager injection in Webservice
                      froden

                      Please disregard the last post, the angle brackets were messing up.

                      Err, it actually turns out that I'm using JBossWS 1.2.1, not 2.0.0..

                      Regardless, I removed the servlet and servlet-mapping from web.xml and added the annotation @WebContext(contextRoot="/ws") to the EJB.

                      The webservice is now deployed at localhost/TestServiceService/TestService. Shouldn't it be localhost/ws/TestService?

                      • 8. Re: EntityManager injection in Webservice
                        froden

                        I guess I should also add that after I had removed the servlet and servlet mapping, and added the WebContext annotation, calling the web service would cause the following exception:

                        19:05:06,212 ERROR [SOAPFaultHelperJAXWS] SOAP request exception
                        org.jboss.ws.WSException: Cannot obtain container from Dispatcher: jboss.j2ee:ear=Community.ear,jar=Community-1.0.jar,name=MyService,service=EJB3
                         at org.jboss.ws.integration.jboss42.ServiceEndpointInvokerEJB3.invokeServiceEndpointInstance(ServiceEndpointInvokerEJB3.java:109)
                         at org.jboss.ws.core.server.AbstractServiceEndpointInvoker.invoke(AbstractServiceEndpointInvoker.java:207)
                         at org.jboss.ws.core.server.ServiceEndpoint.processRequest(ServiceEndpoint.java:212)
                         at org.jboss.ws.core.server.ServiceEndpointManager.processRequest(ServiceEndpointManager.java:448)
                         at org.jboss.ws.core.server.AbstractServiceEndpointServlet.doPost(AbstractServiceEndpointServlet.java:114)
                         at javax.servlet.http.HttpServlet.service(HttpServlet.java:710)
                         at org.jboss.ws.core.server.AbstractServiceEndpointServlet.service(AbstractServiceEndpointServlet.java:75)
                         at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
                         at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
                         at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
                         at org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:96)
                         at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
                         at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
                         at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:230)
                         at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
                         at org.jboss.web.tomcat.security.SecurityAssociationValve.invoke(SecurityAssociationValve.java:179)
                         at org.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContextValve.java:84)
                         at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
                         at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:104)
                         at org.jboss.web.tomcat.service.jca.CachedConnectionValve.invoke(CachedConnectionValve.java:157)
                         at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
                         at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:241)
                         at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:844)
                         at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:580)
                         at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
                         at java.lang.Thread.run(Thread.java:595)
                        19:05:06,221 ERROR [SOAPFaultHelperJAXWS] SOAP request exception
                        org.jboss.ws.WSException: Cannot obtain container from Dispatcher: jboss.j2ee:ear=Community.ear,jar=Community-1.0.jar,name=MyService,service=EJB3
                         at org.jboss.ws.integration.jboss42.ServiceEndpointInvokerEJB3.invokeServiceEndpointInstance(ServiceEndpointInvokerEJB3.java:109)
                         at org.jboss.ws.core.server.AbstractServiceEndpointInvoker.invoke(AbstractServiceEndpointInvoker.java:207)
                         at org.jboss.ws.core.server.ServiceEndpoint.processRequest(ServiceEndpoint.java:212)
                         at org.jboss.ws.core.server.ServiceEndpointManager.processRequest(ServiceEndpointManager.java:448)
                         at org.jboss.ws.core.server.AbstractServiceEndpointServlet.doPost(AbstractServiceEndpointServlet.java:114)
                         at javax.servlet.http.HttpServlet.service(HttpServlet.java:710)
                         at org.jboss.ws.core.server.AbstractServiceEndpointServlet.service(AbstractServiceEndpointServlet.java:75)
                         at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
                         at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
                         at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
                         at org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:96)
                         at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
                         at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
                         at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:230)
                         at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
                         at org.jboss.web.tomcat.security.SecurityAssociationValve.invoke(SecurityAssociationValve.java:179)
                         at org.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContextValve.java:84)
                         at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
                         at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:104)
                         at org.jboss.web.tomcat.service.jca.CachedConnectionValve.invoke(CachedConnectionValve.java:157)
                         at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
                         at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:241)
                         at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:844)
                         at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:580)
                         at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
                         at java.lang.Thread.run(Thread.java:595)


                        Opening the wsdl however, works fine.

                        • 9. Re: EntityManager injection in Webservice
                          froden

                          Are any of the following features limited to JBossWS >=2.0.0?
                          -The @WebContext annotation
                          -Using an EJB as a web service class
                          -Injection of the EntityManager using the @PersistenceContext

                          When I'm using the @WebContext parameter instead of the servlet and servlet-mapping in web.xml, I get exceptions when calling the web service method. If I instead use the servlet and servlet mapping, injection of the EntityManager wont work.

                          • 10. Re: EntityManager injection in Webservice
                            froden

                            Having stopped being an idiot, I guess I can answer my own questions above.

                            - Is the @WebContext annotation JBossWS >= 2.0.0 only? No. My problem was that I had imported org.jboss.wsf.spi.annotation.WebContext instead of org.jboss.ws.annotation.WebContext.

                            - Is using an EJB as a web service class possible in version >=2.0.0 only? No.
                            - Is injection of EntityManager with @PersistenceContext limited to version >=2.0.0? No

                            Perhaps this can be helpful for someone.

                            • 11. Re: EntityManager injection in Webservice
                              kanumalla

                              wtf!