使用Hibernate将两个或更多应用程序连接到同一个数据库 [英] Connect two or more applications to the same database using Hibernate

查看:152
本文介绍了使用Hibernate将两个或更多应用程序连接到同一个数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我打算构建一个桌面应用程序,它将使用Hibernate和MySQL作为其数据源。我想在多台机器上执行桌面应用程序,但是我希望它们都可以读/写同一个MySQL数据库。这甚至有可能吗?我关心的是两个应用程序试图访问/修改相同信息时的并发问题。是否有多种ORM应用程序的替代解决方案来实现实时数据库同步(如果不允许单个数据库)?



有几个类似的问题需要我stackoverflow,但他们的答案不满足我。

解决方案

您可以让多个基于hibernate的应用程序与同一个数据库交谈。在大多数情况下,这与运行多个线程与同一数据库进行交谈几乎是一样的。



如果您的休眠状态发生,您可能会看到最常见的问题配置正在使用二级缓存。当hibernate配置了二级缓存时,它假定缓存与底层数据库一致。当你有一个应用程序连接到数据库时,二级缓存可以存在于应用程序服务器的本地RAM中,并且一切都很好。



如果您有多个服务器(不管它们运行的​​是相同的应用程序还是不同的应用程序),您需要有一个由所有服务器共享的单个二级缓存。或者您不需要使用二级缓存。

几种分布式缓存解决方案可用于hibernate的二级缓存 - 谷歌将帮助您研究各种选项,如果您决定走这条路。



同样的参数也适用于查询缓存。


I am planning to build a desktop application that will use Hibernate and MySQL as its datasource. I would like to execute the desktop application on multiple machines but I want them all to read/write to the same MySQL database. 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)?

There were a few similar question to mine on stackoverflow but their answers do not satisfy me.

解决方案

You can have multiple hibernate-based apps talking to the same database. 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. When hibernate is configured with a second-level cache, it assumes that the cache is consistent with the underlying database. 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.

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.

Same argument applies to query cache as well.

这篇关于使用Hibernate将两个或更多应用程序连接到同一个数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆