How to synchronize multiple clients with a shared database (JPA)?

徘徊边缘 提交于 2020-01-03 02:36:07

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!