问题
I have a very essential question about a basic concept regarding synchronization of multiple clients that share a common database:
I plan to design a distributed application using Java and JPA for persistence. Assumed that I have a central database that hosts all enterprise data and multiple clients connect to this database for querying data, what would be the best approach to keep the particular clients synchronized (in means of the data in the database) ?
E.g. Client A updates the surname of a customer while Client B is already logged in to the database and has retrieved it's persistence context. How can I inform Client B about the changes of the customers properties?
Is there any possibility in JPA to test entities in persistent-context on changes? Or do I need a JMS like network service that informs all active clients about any changes in the database?
Any suggestions highly appreciated! Thanks in advance...
回答1:
It depends on whether you want the JPA provider to cache data or not. Without caching there's nothing to worry about, the data will be retrived from the database every time. With caching you will need some kind of distributed cache to ensure that clients don't have stale data. You haven't mentioned what JPA provider you are using. Eclipselink contains its own distributed cache implementation, but beware that you need to turn it on by setting a CacheSessionCustomizer. By default Eclipselink will cache the most recent 100 entities, so if you don't enable the distributed cache you're guaranteed stale data. With Hibernate you can use something like ehcache, but I haven't used that myself so can't really comment much on it.
来源:https://stackoverflow.com/questions/9621308/how-to-synchronize-multiple-clients-with-a-shared-database-jpa