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

Posted on 2010-01-06
Last Modified: 2013-11-11

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
Question by:amitseth1000
    LVL 35

    Expert Comment

    by:Miguel Oz
    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.

    LVL 40

    Expert Comment

    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.

    Author Comment


    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.
    LVL 35

    Accepted Solution

    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.

    Author Closing Comment

    Thanks. I have got it working by locking shared objects .

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
    What is Waterfall Model? Waterfall model is the classic Software Development Life Cycle method practiced in software development process. As the name "waterfall" describes, this development is flowing downwards steadily like waterfall, i.e., procee…
    To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…
    Internet Business Fax to Email Made Easy - With eFax Corporate (, you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

    728 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now