Results 1 to 2 of 2

Thread: Singleton vs. Prototype

  1. #1
    Join Date
    Aug 2004
    Location
    Herndon, Virginia
    Posts
    28

    Default Singleton vs. Prototype

    I've now successfully deployed two applications based on the Spring Framework. I am constantly amazed at this software and the significant improvements I've made in application creation.

    Now that I've gotten some things out, I'm starting to develop some usage questions. My first is in the title. The Spring document, which is very comprehensive doesn't talk much about a, IMO, very fundamental feature of Spring's bean creation: Singleton vs. Prototype. I would like an expanded understanding of the implications of using one over the other, perhaps some scenarios to help select one over the other.

    For example, we I used Mark Eagle(?) article on Wiring open source applications as a starting point for the Persistence layer we have in our applications. I have a business object that contains a PersistenceService interface that is injected by Spring. A class that implemented this interface has serveral DAOs also injected by Spring.

    We had a bug where I passed an object containing some data to the persistence layer. This data object was passed in as a BaseData object(another interface). For ease of use, the implementation class of the PersistenceInterface had a class variable that I would cast BaseData to. For example, I would cast BaseData to CostData thereby gaining access to the specific getters of CostData. Again, this was done right at the beginning of a specific method just to simplify use.

    However, we had a problem that two simultaneous calls to this PersistenceService(user1 and user2) would cause the data of user1 to appear in user2's call.

    It appear that since the same instance of PersistenceService was being returned, and this variable that held a reference to BaseData was a class instance variable, that this data was overlapping.

    Would it be safe to say that instance variables of singleton Spring returned beans are not threadsafe? I removed the casting, in essence, stopped using the class variable and stuck to using the object that was passed in as well as variables created locally in the method and the problem seemed to go away.

    Are there any nuances of Singleton and Prototype use that can be shared?

    Thanks!
    ---------------------------------
    David H. McCoy


    ----------------------------------

  2. #2
    Join Date
    Aug 2004
    Location
    Melbourne, Australia
    Posts
    335

    Default

    Would it be safe to say that instance variables of singleton Spring returned beans are not threadsafe?
    No. Whether or not access to instance variables of a singleton is thread safe is entirely a function of the design of the class and has nothing to do with whether it is managed by Spring or anything else for that matter.

    Ollie

Similar Threads

  1. Is a 'singleton' flow a bad idea?
    By akw in forum Web Flow
    Replies: 4
    Last Post: Oct 6th, 2005, 01:16 AM
  2. Replies: 4
    Last Post: Oct 5th, 2005, 11:04 AM
  3. EHCaching Hibernate
    By dencamel in forum Data
    Replies: 3
    Last Post: Sep 6th, 2005, 09:03 PM
  4. Replies: 6
    Last Post: May 25th, 2005, 01:56 AM
  5. DefaultAdvisorAutoProxyCreator skipping beans
    By youngm in forum Container
    Replies: 6
    Last Post: Apr 12th, 2005, 04:29 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •