Page 1 of 2 12 LastLast
Results 1 to 10 of 11

Thread: Are Spring business Service Components Thread safe?

  1. #1
    Join Date
    Jan 2005
    Location
    Bangladesh
    Posts
    1

    Default Are Spring business Service Components Thread safe?

    Hello all,

    Currently I am using Spring as the core framework for a architecture of a large scale Hospital Management System. In designing the architecture I am thinking whether the business operations would be thread safe. Its a service oriented architecture.....And the Service provider classes(Which provide the business service to the clients or external world) uses Business Service classes through a Common gateway....This central gateway(A Facade essentially) takes the appropiate service object from the Spring Context and executes the operation.....In the db layer I am planning to use both hibernate as well as pure JDBC....through Spring Template..offcourse.... Now my question is do I need to make the common business operation of the Gateway synchronized? How much it puts burden in the System?

    Regards,

    Abdul Ahad
    Lead Design Engineer
    Sikraft Solution Ltd.

  2. #2
    Join Date
    Oct 2004
    Location
    Antwerp, Belgium
    Posts
    96

    Default

    Now my question is do I need to make the common business operation of the Gateway synchronized?
    If you use Spring's middle tier components for transaction management and your service classes do not hold state other than persistence artifacts (datasource, Hibernate session factory, ...) your classes are perfectly thread-safe. The Spring application context is thread-safe, taking care of synchronization if and where required.

    You can read more on thread-safety in Spring in this article.

  3. #3
    Join Date
    Aug 2004
    Location
    San Mateo, CA
    Posts
    1,265

    Default

    Generally there are no thread safety issues in the service layer. For example, look at typical SLSBs: they are given pooling by the container, but in reality don't need it, as a typical SLSB has no read-write instance variables after it's configured.

    In the rare cases where you strike something that isn't naturally threadsafe in the service layer, you can use synchronization. Or you can use Spring's own pooling capability (see the AOP chapter in the Reference Manual).

    What you're describing with data access will be completely threadsafe as far as Spring code is concerned.
    Rod Johnson - GM, SpringSource Division, VMware
    http://www.springsource.com
    Spring From the Source

  4. #4
    Join Date
    Sep 2004
    Location
    France
    Posts
    44

    Default

    Just a question about synchronization.

    It seems difficult for me to use method level synchronization in the service layer because of the programming to interfaces design choice.
    I cannot add the synchronized keyword to the method in the implementation class of my service. If I do so, my service is not fully synchronized because transaction commit will occur outside synchronization.
    As I don't want to let synchronization demarcation to the client side, the only solution I can find is using a synchronization keyword in my service declaration in my applicationContext.xml
    Does this makes sense ? Do you have any other solution to my problem.

    Here is a bit of code to explain my problem
    Code:
    public class Action
    {
      public void process()
      {
        // I want this call to be synchronized but don't want to declare synchronization here
        getMyService().doSomething();
      }
    }
    public interface MyService
    {
      void doSomething();
    }
    public class MyServiceImpl implements MyService
    {
      // Synchronization here is not truly effective as transaction commit 
      // occurs outside the method
      public synchronized void doSomething()
      {
        // do
      }
    }

  5. #5
    Join Date
    Nov 2004
    Location
    Hilversum - The Netherlands
    Posts
    1,054

    Default

    What is the reason you want to use synchronisation? If there is no shared state, there is no need for synchronisation. Btw shared state in de db doesn`t count because the db-transaction will protect you for concurrency problems...

    So... do you have shared state? If not.. there is no need for synchronisation.

    [edit]
    If you make all service methods synchronized there are going to be a few problems:
    1) deadlock problems (if services use each other)
    2) performance problems. If you block a complete service because someone is using it -> every other user has to wait.. so.. your application will be extremely slow.

  6. #6
    Join Date
    Sep 2004
    Location
    France
    Posts
    44

    Default

    Maybe it is quite an unusual problem.
    I have some kind of State object in the database. A lot of different threads call a service that change the properties of this State object (mainly a date). I don't want to throw exception if there is concurrent update to my object. I want my threads to change my State object one after the other.
    The synchronisation is the only way I found to achieve that but there is maybe a better solution

    Seb

  7. #7
    Join Date
    Nov 2004
    Location
    Hilversum - The Netherlands
    Posts
    1,054

    Default

    Quote Originally Posted by scesbron
    Maybe it is quite an unusual problem.
    I have some kind of State object in the database. A lot of different threads call a service that change the properties of this State object (mainly a date).
    You don`t have too. If 2 users need to access the same object, the second should block untill the first has finished. This is part of your transaction.

    The synchronisation is the only way I found to achieve that but there is maybe a better solution
    Seb
    Yes... there is: transactions.

  8. #8
    Join Date
    Sep 2004
    Location
    France
    Posts
    44

    Default

    Sorry but for me this is true if you do pessimistic locking.

    With optimistic locking, you can have two concurrent users that modify the same object. You can have two behavior in this case :
    - The second one override what the first one did.
    - You have a version check or something like that and the second one fails because of the modification made by the first user

    Seb

  9. #9
    Join Date
    Nov 2004
    Location
    Hilversum - The Netherlands
    Posts
    1,054

    Default

    Quote Originally Posted by scesbron
    Sorry but for me this is true if you do pessimistic locking.
    With optimistic locking, you can have two concurrent users that modify the same object. You can have two behavior in this case :
    - The second one override what the first one did.
    - You have a version check or something like that and the second one fails because of the modification made by the first user
    That is correct. But if you know the solutions, why are you trying to synchronize a complete service (in essence you are blocking a complete table...and not a single record).

  10. #10
    Join Date
    Oct 2004
    Location
    Herndon, VA, US
    Posts
    648

    Default

    Quote Originally Posted by scesbron
    With optimistic locking, you can have two concurrent users that modify the same object. You can have two behavior in this case :
    - The second one override what the first one did.
    - You have a version check or something like that and the second one fails because of the modification made by the first user
    With optimistic locking, an exception is thrown when the version check fails, but it doesn't mean you have to let the exception propagate all the way up. You could catch it, resynchronize the in-memory state with what's in db, and possibly try again.

    Besides in-memory synchronization won't in a clustered envoronment anyway.
    --Jing Xue

Similar Threads

  1. Spring-based architectural approach
    By diegum in forum Architecture
    Replies: 9
    Last Post: May 10th, 2007, 04:09 PM
  2. Replies: 2
    Last Post: Oct 10th, 2005, 05:12 PM
  3. Spring container fails with no exception
    By naor in forum Container
    Replies: 9
    Last Post: Oct 1st, 2005, 03:39 PM
  4. Spring code remarks
    By Alarmnummer in forum Architecture
    Replies: 18
    Last Post: Apr 7th, 2005, 07:17 AM
  5. Sessions closing after commit
    By bendg25 in forum Data
    Replies: 0
    Last Post: Mar 21st, 2005, 04:38 AM

Posting Permissions

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