[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 637
  • Last Modified:

Shared objects of Singelton class for different webmethod calls of a webservice

Hi,

I am running a webservice in production and this webservice uses a library and all classes in that library are singleton. I am not using locking mechanism there in singelton calls.

When I try to call a webmethod from multiple instances of an application i face concurency issues as the dataset seems to be shared for different calls.

I thought that by definition webservices are using HTTP and thus they are stateless and the objects will not be shared among different calls from different applications. But it doesnt work as expected. Can you please guide me if there is any setting (pooling) which can be turned off so that objects are not shared among different calls to same web methos.

Thanks in advance
0
amitseth1000
Asked:
amitseth1000
  • 2
  • 2
1 Solution
 
Miguel OzSoftware EngineerCommented:
YOu need to change your objects from singleton to web method local variable. (It exists only for the lifetime of your web method).
If you do require tho share the you need to use "lock" or other synchronization objects because all your code needs to be thread-save.

Internally IIS creates an exe for the application pool servicing your web service. thus your singleton objects will be alive until you do an iisreset. Every time you make a request a new thread is crated to service your web request.

0
 
Gurvinder Pal SinghCommented:
You should not put any data in the web-service POJO method.
There should be a business layer after the POJO layer in web-service based application which will do the session management, transaction management, connection pooling etc for you such EJB, spring or hibernate.
Once there is session and transaction management happening in the next layer, you don't need to worry about the concurrency issues.
0
 
amitseth1000Author Commented:
Hi,

I forgot to mention that my web service is developed in .Net 2.0. Also, I have business rules in a separate class library which is singelton in nature.

I guess locking objects may resolve the issue but it will not allow same instance of an object to be shared among different threads which may turn into permormance issues for multiple concurrent users.
0
 
Miguel OzSoftware EngineerCommented:
If you go for singleton, you need to take in account how state will affect your web service methods.
If you do need state, you can use the instance of your business class as a local variable, you only need to lock shared resources (eg. a common file) for the code to be thread-safe.
0
 
amitseth1000Author Commented:
Thanks. I have got it working by locking shared objects .
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now