简体   繁体   English

访问MySQL数据库时如何同步两个单独的Java应用程序

[英]How to synchronize two separate Java Applications when accessing a MySQL database

I want to have a Java Web App and a normal Java App that both have access to the same MySQL database using JDBC. 我想拥有一个Java Web App和一个普通的Java App,它们都可以使用JDBC访问相同的MySQL数据库。 The two are of course entirely independent so what i want is to find a way to synchronize them somehow, so that i can ensure that there will be no errors to the database access due to race conditions, but i don't know how to do that. 两者当然是完全独立的,所以我想要的是找到一种以某种方式同步它们的方法,这样我就可以确保不会由于竞争条件而导致对数据库访问的错误,但是我不知道该怎么做那。 Any suggestions? 有什么建议么?

Transactions. 交易。 Pick the best isolation level of your transactions for your needs. 为您的交易选择最佳的隔离级别。

Most ORM solutions (hibernate, jpa) can use a 'version' column which will be checked when you attempt to write through to the database. 大多数ORM解决方案(休眠,jpa)都可以使用“版本”列,当您尝试写入数据库时​​将对其进行检查。 If the version of the object you are trying to write is different than the version in the database then you are out of date and need to merge incoming changes first. 如果您要写入的对象版本与数据库中的版本不同,则您已过时,需要首先合并传入的更改。 It shouldn't be too hard to do something similar in JDBC. 在JDBC中做类似的事情应该不难。

事务(或某些DB事物)可能是最好的,但是如果在您的应用程序中无法正常工作,则可以设置一个虚拟文件并要求对其进行写访问作为您的锁。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM