• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

JAX-WS web service implementation

 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 282
Eclipse IDE PHP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
today's feeble attempt to support the empire
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic