General J2EE: how to use synchronize method in more than 1 application server
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?
- Posted by: Junyang Bi
- Posted on: August 11 2003 23:19 EDT
- Reply by Alex Pisarev on August 12 2003 03:17 EDT
- how to use synchronize method in more than 1 application server by Rob Misek on August 12 2003 11:35 EDT
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.
Thank you for your help. But I'm not familiar for the JNDI variable, can you give me the answer?
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.
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.
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.
Tangosol Coherence: Cluster your Work. Work your Cluster.