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."