当应用程序在多个节点上 运行 时,如何实现数据库更新的同步?

How to achieve synchronization in DB update when application is running on multiple nodes?

我是 J2EE 开发的新手,正在努力掌握一些基础知识。所以怀疑就像,假设我在数据库中的某个地方有一个 employee table 和一个增加 employee table 中的 salary 列的函数。

public synchronized IncreaseSalaryResponse increaseSalary(int empId, long raise);

在我的例子中,服务是 运行 在两个节点(所以两个 JVM)上以实现高可用性。现在,如果同时进行两次调用以增加员工的薪水,并且如果它们命中不同的主机,则最后结束的函数调用的效果将反映在最终薪水中。

DB 是否确保在其级别避免此类竞争条件?

如果没有,我的情况如何处理。

请提供想法。

这取决于您使用的 RDMS 是否跨国以及隔离级别。

根据维基百科,

Transactions in a database environment have two main purposes:

1- To provide reliable units of work that allow correct recovery from failures and keep a database consistent even in cases of system failure, when execution stops (completely or partially) and many operations upon a database remain uncompleted, with unclear status.

2- To provide isolation between programs accessing a database concurrently. If this isolation is not provided, the program's outcome are possibly erroneous.