Discussions

General J2EE: Synchronization of application scope objects when clustering

  1. Dear all, I'm running into a rather conceptual issue that I'm trying to understand. I'll explain it by giving the following example. Suppose I need to design a web application using JSP where different users should have read/write access to the same object. The simplest thing would be to add my object (a POJO) to the application scope, so that it can be shared by all users. However, according to the specs, the objects created with application scope (as well as session scope) are not thread-safe, so it's the developer's responsibility to synchronize access to them. So, to make it 'clean', I can define a static object member in my POJO and use that object with synchronized() blocks in my POJO methods. In this case, everything goes fine IF the application scope is not replicated on a different JVM/machine. Even if the application scope is replicated by the web container on the same JVM, it still goes fine, since I synchronize on a static object. But what happens in the case of clustering, load balancing or any other cross-JVM/cross-machine replication scenario ? So if my application scope is replicated on different JVMs, then the synchronization mechanism described above won't work. Or maybe the web container keeps these application objects in sync transparently for the developer, so that the developer can still use the synchronization mechanism described so far. Any thoughts on that ? Note that in .NET the issue is similar. Checking the .NET documentation I notice that in case of clustering (web farm, etc.) the synchronization between the application objects is NOT ensured: "Application state is not shared among multiple servers serving the same application, as in a Web farm, or among multiple worker processes serving the same application on the same server, as in a Web garden. Your application therefore cannot rely on application state containing the same data for application state across different servers or processes. If your application will run in multi-processor or multi-server environments, consider using a more scalable option, such as a database, for data that must preserve fidelity across the application." Thank you, Greetings, Sorin
  2. Hi, The application scope should be transparently replicated by the server, but I dont know whether you can count on that for synchronization. I am referring to the replication delay here. Otherwise, I could think of the following approaches: 1. Use DB for locking. 2. Write an RMI class that takes care of accessing and operating on the shared data and register it in the rmi regsitry. Your application code should make RMI calls to invoke the operations. This has several drawbacks - the remote class need to know all the possible operations on the shared data, it will only take care of the mutex access to the data, I am not sure abt the waiting part. 3. Use a clustered cache implementation like terracota or oracle coherence. Please do post here if you get to an elegant solution to this. 3.
  3. interesting[ Go to top ]

    Thank you for your answers. I was also thinking about esoteric solutions, but I haven't got any elegant one .... Though the problem sounds simple, the solution lies in the area of distributed locking and that's no trivial issue. I'll keep digging. Greetings, Sorin