0

Multiple servers use the same database,when they access the same data by optimistic locks.Sever A,server B get the data of version 1,and they commit at the same time with the version 2. Do optimistic locks work for multiple applications?

jr1990
  • 1
  • 1

1 Answers1

0

If you're actually using optimistic locks, of course they work for multiple applications. That's the whole point of optimistic locks - you don't really need them in a single application.

But they need to be optimistic locks, not just "let's hope it doesn't break". That means that an update of the row must always result in a new version number. When A and B try to do the update, the first that actually goes through will have the old version incremented, which means the other will no longer have a matching version, and will get an optimistic concurrency fail.

Luaan
  • 62,244
  • 7
  • 97
  • 116
  • database will not refuse committing when A and B commit at the same time because it's optimistic .The ability of refusing committing is controlled by application itself. To the multiple applications,A application can't stop B committing . – jr1990 Apr 04 '17 at 18:58
  • @jr1990 You really should include your code in the question. It sounds like you really aren't implementing optimistic concurrency correctly. The point isn't that the DB will accept both commits - that would make it rather useless. Updates to the same row must be serialized, and the one that comes later will fail (usually by simply not doing any update, since the row version changed). The application needs to *resolve* the conflict, but the DB mustn't allow the conflicting store to succeed. – Luaan Apr 05 '17 at 10:29