tags:

views:

179

answers:

2

I am using the JAX-WS "WebService" annotation on a class to expose its "WebMethod"s as a web service. The class is denoted as the servlet class handling calls to "/MyService".

As essentially a servlet, I would expect an instance of this class to be created once and to basically be a singleton. I have code in the constructor of this class to create an EntityManagerFactory for assignment to a member variable. What I'm seeing is that the constructor is being called for every client request to the web service. This is not good.

Does anyone know what's going on here? Does anyone understand what I'm trying to ask? :)

Thanks.

A: 

Your topic mentions a SLSB, which I assume is "Stateless Session Bean". In JEE 5 you can create web services either from a Stateless Session Bean, or you can annotate a class and the runtime will publish it as a webservice when deployed in a compliant web container.

In either case, neither of these are Servlets per se, and don't follow a Servlet lifecycle.

Will Hartung
Interesting. Thank you very much. How do you know this, by the way? :) I cannot find good documentation in this regard... Does this basically follow from info in the JSR?
Rintoul
I know this because nothing I've seen has told me that they were Servlets. Some implementations may well use a Servlet to implement them, but that's an implementation detail, and not part of the spec.
Will Hartung
Just weird that you wire it up in the web deployment descriptor as though it were a servlet (right down to the "servlet-class" element containing the name of the @WebService class)...
Rintoul
That's the actual dispatcher that the web container uses to fire the WebService, but it doesn't make the WebService itself an actual Servlet. And, in the Stateless Session Bean case, there is no "dispatcher" wired up, the container does that for you. Now, clearly, in a common web container, a servlet is likely involved SOMEWHERE, it's just silly not to use one, but that does not make the WebService itself an actual Servlet, or even really related to one. Of course, in J2SE, there's no container or servlet. Just a HTTP Listener and a dispatcher. I have zero characters available in this message.
Will Hartung
A: 

As essentially a servlet, I would expect an instance of this class to be created once and to basically be a singleton.

It's up to container. You cannot rely on it.

Create a real singleton -- a simple Java class -- which does all the heavy lifting, and call it from the servlet.

Vladimir Dyuzhev