5 Replies Latest reply: Nov 15, 2012 6:05 AM by 726437 RSS

    Webservice initialized on every method-call.

    schrotti12
      Hello!

      I have written two webservices. One containing a EJB and a SOAP-Webservice and one with just a SOAP-Webservice.
      If I call the EJB-Version the webservice gets inizialized by glassfish on the first call and then stays running (Constructor is not called any more) but if I call the SOAP-only service constructor is called on every method-call.
      Therefore I have a deployment-problem with the EJB-Version: I use SQL-Server-JDBC-Libs in my Netbeans-Project. The get deployed to the server but are not used by the application. Instead I have to copy them to the servers lib-directory. If I deploy them with my Soap-only-service they work...
        • 1. Re: Webservice initialized on every method-call.
          gimbal2
          but if I call the SOAP-only service, the constructor is called on every method-call.
          (fixed that a bit for some clarity)

          And HOW do you call that SOAP-only service? Show some example code. Post it between \
           tags.                                                                                                                                                                                                                                                                                                                                                                                                                                                                
          • 2. Re: Webservice initialized on every method-call.
            schrotti12
            For the Webservice-only I create a web-application in Netbeans (File - New Project - Java Web - Web Application) and run this code:
            package com.service;
            
            import javax.jws.WebMethod;
            import javax.jws.WebParam;
            import javax.jws.WebService;
            
            @WebService(serviceName = "Service")
            public class Service {
                
                public Service() {
                    System.out.println("Constructor called!");
                }
            
                @WebMethod(operationName = "hello")
                public String hello(@WebParam(name = "name") String txt) {
                    return "Hello " + txt + " !";
                }
            }
            Everytime i call the hello-method through the Tester (http://ip:port/URI?Tester) the webservice get initialized ("Constructor called").

            On the other hand I create a Java EE - EJB-Module in Netbeans and add the webservice.
            I remove the EJB-Annotations from the module and use it as a POJO for the code.

            EJB:
            package com.slan.ejb;
            
            import javax.ejb.Stateless;
            
            @Stateless
            public class EJBSlan {
                
                public EJBSlan(){
                    System.out.println("Constructor of EJB called!");
                }
                
                public String getHello(String name) {
                    return "Hello " + name;
                }
            }
            Service:
            package com.slan.service;
            
            import com.slan.ejb.EJBSlan;
            import java.sql.Connection;
            import java.sql.DriverManager;
            import java.sql.ResultSet;
            import java.sql.SQLException;
            import java.sql.Statement;
            import javax.ejb.Stateless;
            import javax.jws.WebMethod;
            import javax.jws.WebParam;
            import javax.jws.WebService;
            
            @WebService(serviceName = "EJBService")
            @Stateless()
            public class EJBService {
                
                private EJBSlan ejbRef;
                
                public EJBService(){
                    ejbRef = new EJBSlan();
                    System.out.println("Constructor of Service called!");
                }
                
                @WebMethod(operationName = "getHello")
                public String getHello(@WebParam(name = "name") String name) {
                    return ejbRef.getHello(name);
                }
                
                @WebMethod(operationName = "getMachines")
                public String getMachines() {
                    
                    String machines = "";
                    
                    try {
                        
                        DriverManager.registerDriver(new com.mysql.jdbc.Driver());
                        Connection c = DriverManager.getConnection("jdbc:mysql://ip/cm","user","password");
                        Statement s = c.createStatement();
                        ResultSet r = s.executeQuery("select  from machines");
                        
                        while(r.next()){
                            machines += r.getString("comment");
                        }
                        
                        
                        s.close();
                        c.close();
                    } catch (SQLException e) {
                        System.out.println(e.toString());
                    }
                    
                    return machines;
                }
            }
            As you can see I already tried to add the sql-code to query the database. I added the mysql.jar to the project and deployed it. Using the tester the Service- and POJO-Object gets initialized only once but I cant use the mysql-driver cause it is not used on the server. But it gets deployed to the server... I think it's just not included in the class-path...
            • 3. Re: Webservice initialized on every method-call.
              gimbal2
              I give such a short reply and still you manage to miss/ignore my comment on using \
               tags.                                                                                                                                                                                                
              • 4. Re: Webservice initialized on every method-call.
                schrotti12
                I'm not familiar with this board-software. had to find out first how to use ist...
                But I changed it.

                One solution would be how to add a jar-file (i.e. a mysql-library) to a EJB-Project in Netbeans 7 so that the ejb-file gets deployed to the server and included into the classpath on server-side...

                Edited by: user12092354 on 05-Nov-2012 06:21
                • 5. Re: Webservice initialized on every method-call.
                  726437
                  Why cant you use a normal webservice, and then just inject the EJBs you need? And since you're in a container, why dont you use the container's datasource?
                  @WebService(serviceName = "EJBService")
                  public class EJBService {
                  
                      @EJB
                      private EJBSlan ejbRef;
                      @Resource(name="myDs")
                      private DataSource ds;
                      
                      @WebMethod(operationName = "getHello")
                      public String getHello(@WebParam(name = "name") String name) {
                          return ejbRef.getHello(name);
                      }
                  
                      @WebMethod(operationName = "getMachines")
                      public String getMachines() {
                          
                          String machines = "";
                          
                          try {
                              
                              Connection c = ds.getConnection();
                              Statement s = c.createStatement();
                              ResultSet r = s.executeQuery("select  from machines");
                              
                              while(r.next()){
                                  machines += r.getString("comment");
                              }
                              
                              
                              s.close();
                              c.close();
                          } catch (SQLException e) {
                              System.out.println(e.toString());
                          }
                          
                          return machines;
                      }
                  }