[英]Connect two or more applications to the same database using Hibernate
I am planning to build a desktop application that will use Hibernate and MySQL as its datasource. 我打算构建一个桌面应用程序,它将使用Hibernate和MySQL作为其数据源。 I would like to execute the desktop application on multiple machines but I want them all to read/write to the same MySQL database. 我想在多台机器上执行桌面应用程序,但我希望它们都能读/写同一个MySQL数据库。 Is this even possible? 这甚至可能吗? My concern is about concurrency issue when two applications is trying to access/modify the same information. 当两个应用程序试图访问/修改相同的信息时,我担心的是并发问题。 Is there an alternative solution for multiple ORM applications to have real-time database syncronisation (if a single database is not allowed)? 是否存在多个ORM应用程序的替代解决方案,以实现实时数据库同步(如果不允许单个数据库)?
There were a few similar question to mine on stackoverflow but their answers do not satisfy me. 在stackoverflow上有一些类似的问题,但他们的答案不满足我。
You can have multiple hibernate-based apps talking to the same database. 您可以让多个基于hibernate的应用程序与同一个数据库通信。 For the most part, this is pretty much the same as running multiple threads talking to the same database. 在大多数情况下,这与运行与同一数据库通信的多个线程几乎相同。
The most common problem you are likely to see will occur if your hibernate configuration is using a second-level cache. 如果您的hibernate配置使用二级缓存,则可能会出现最常见的问题。 When hibernate is configured with a second-level cache, it assumes that the cache is consistent with the underlying database. 当hibernate配置了二级缓存时,它假定缓存与底层数据库一致。 When you have a single application attaching to the database, the second-level cache can exist in the local RAM of the app server and everything is good. 当您将单个应用程序附加到数据库时,二级缓存可以存在于应用程序服务器的本地RAM中,一切都很好。
If you have multiple servers (whether they are running the same application or different ones), you need to have a single second-level cache that is shared by all of the servers. 如果您有多个服务器(无论它们是运行相同的应用程序还是不同的应用程序),您需要拥有一个由所有服务器共享的单个二级缓存。 Or you need to not use a second-level cache. 或者您不需要使用二级缓存。
Several distributed cache solutions for hibernate's second-level cache are available - google will help you research your various options if you decide to go this route. 可以使用几种用于hibernate二级缓存的分布式缓存解决方案 - 如果您决定采用这种方式,谷歌将帮助您研究各种选项。
Same argument applies to query cache as well. 同样的参数也适用于查询缓存。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.