Synchronization in helper objects


EJB programming & troubleshooting: Synchronization in helper objects

  1. Synchronization in helper objects (2 messages)

    I understand that the use of synchronization in an EJB violates the spec. Does this only apply to EJBs or does it extend to helper classes (DAO's, Value objects, etc) that an EJB might use? Can these helper classes use synchrozized methods? What I've seen w/ WebLogic 6.1 indicates that it works ok. I also suspect that its ok since InitialContext has a constructor that requires a Hashtable object and Hashtables are synchronized.

    Can anyone shed any light on this for me?

    Paul Lemley
  2. Synchronization in helper objects[ Go to top ]

    The container is responsible for synchronizing the beans, either by creating multiple instances or by serializing access to a single instance.

    However any other object that may be accessed by multiple beans concurrently, will have to be synchronized. For example, if you have a singleton that may be accessed by multiple beans concurrently, then the singleton will have to be properly synchronized.

  3. Synchronization in helper objects[ Go to top ]

    This is in the vague area of the spec and has been debated much on this site.

    Some people say NO, YOU CAN'T DO IT.

    I say that's taking it too far. What about if you use a Hashtable? All it's methods are synchronized.

    In my opinion (and I know someone is instantly going to disagree with me here) it is OK to have synchronization in your helper objects. BUT, be warned, you do take some control away from the container to manage resources when you do this, since you effectively force certain threads to sleep so the container could, in theory, end up causing a deadlock, by attempting to run a thread which was waiting on another thread which the container was not running. In practice, it's unlikely to happen.

    But, the synchronized keyword will have to come inside the helper class, you can't use that keyword anywhere in your bean or some of the EJB compilers will moan at you.