• Post Reply Bookmark Topic Watch Topic
  • New Topic

JAX-WS web service implementation

 
Murali Pen
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a JAX-WS web service implementation class with several operations in it. All these operations (with @WebMethod annotation) make use of a Dao implementation class to query the database and return some results. Should I instantiate this Dao class and store it in a class level variable or should I instantiate it inside every web service method? With the first option, the Dao instance will be shared across all web service invocation. With the second option, a separate Dao instance will be created for each web service invocation. Any suggestions?

Also, I believe a single instance of web service implementation class will serve all invocations. Is this correct? I don't know how WSServlet internally handles the invocation of web services.
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13078
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why is this even a question? Consider what would happen with two "simultaneous" requests.

Assuming you are using a database connection pool, creating a simple DAO object which only needs to live through one request/response cycle is very cheap.

Bill
 
Michael Angstadt
Ranch Hand
Posts: 277
Eclipse IDE Java PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, just like servlets, a single instance of the service implementation bean is used to handle all requests. So it just boils down to whether or not your DAO is thread safe. If it is thread-safe, then storing the DAO in a class-level variable would be fine. But if it is not thread-safe, then you could still use a class-level variable, but you would have to make sure to synchronize on it so that only one thread can access the DAO at a time. You could also just create a new instance of the DAO in each of the web methods.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!