Discussions

General J2EE: how to use synchronize method in more than 1 application server

  1. I want to use synchronize method to lock some data only for one user to use at same time, but we use 2 application servers. This means we have 2 jvm, how to solve this problem?

    thanks

    Threaded Messages (5)

  2. Reply[ Go to top ]

    You can do that via common JNDI flag. In your method, you should retreive JNDI variable and check its status. If its true, you should wait until it becomes false. Otherwise, set the flag to true, bind it to JNDI and proceed with the method execution. At the end of the execution, bind a false value to JNDI.

    The same effect could be achieved through common Stateful Session bean (which can be replicated between your appservers) or by using common database record.

    But I reckon JNDI solution will be the universal one.

    Alex
  3. Hi Alex,

      Thank you for your help. But I'm not familiar for the JNDI variable, can you give me the answer?

    junyang
  4. Use the DB for this[ Go to top ]

    Java and/or J2EE don't provide generic solutions for this. You have to write something yourselve, by using a DB (write a lock entry or something) for this.

    Regards
    Barri
  5. Reply[ Go to top ]

    Junyang,

    You can consider JNDI as a database replacement. When you get an InitialContext, you can easily bind any objects you want to it. For example, you can bind a flag this way:

    Context ctx=new InitialContext();
    ctx.bind ("my_boolean_flag",new Boolean(true));

    If you have a central JNDI which is accessible by both application servers, you can easily retreive the value from any of this servers.

    Alex
  6. Hi junyang Bi,

    If you would like to cluster a large (or small ;-) amount of data across multiple appserver nodes take a look at Tangosol's Coherence product. Coherence supports cluster-wide locking and event-notification while providing in-memory speed access to your data.

    Later,
    Rob Misek
    Tangosol Coherence: Cluster your Work. Work your Cluster.
    Coherence Forums.