Recent

Author Topic: Changing the same record in DB  (Read 756 times)

LemonParty

  • Sr. Member
  • ****
  • Posts: 438
Changing the same record in DB
« on: April 07, 2026, 07:13:45 pm »
Hello.

Let's say we have 2 connections to DB. The first user opens table X and changes record Y in it, at the same time another user opens this same table and changes record Y. Then the first user commit transaction and then the second user commit transaction too. There will be an error or maybe exception? What will happen?
Lazarus v. 4.99. FPC v. 3.3.1. Windows 11

Zvoni

  • Hero Member
  • *****
  • Posts: 3349
Re: Changing the same record in DB
« Reply #1 on: April 08, 2026, 08:20:16 am »
Hello.

Let's say we have 2 connections to DB. The first user opens table X and changes record Y in it, at the same time another user opens this same table and changes record Y. Then the first user commit transaction and then the second user commit transaction too. There will be an error or maybe exception? What will happen?
This is about Isolation Levels.
Read here: https://en.wikipedia.org/wiki/Isolation_(database_systems)

As to what's happening?
Depends on the Isolation-Level and the DB in use
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

egsuh

  • Hero Member
  • *****
  • Posts: 1773
Re: Changing the same record in DB
« Reply #2 on: April 08, 2026, 09:15:29 am »
Zvoni's answer would be precise, but probably the lastly committed change will remain stored, in general cases.

Thaddy

  • Hero Member
  • *****
  • Posts: 18920
  • Glad to be alive.
Re: Changing the same record in DB
« Reply #3 on: April 08, 2026, 09:42:24 am »
The theory behind it is ACID.
https://en.wikipedia.org/wiki/ACID

Not a long read and a solid explanation.

Most distributed databases have out-of-the-box support for that.

Some databases need something like a trigger, though, keeping track of multiple read access and generating a warning on commit that the record is being edited elsewhere.

I once wrote something like that for Sqlite ( a server-side user defined function that keeps a reference count in a separate table) which lacks that, because Sqlite is by design not a multi-user database.
It may be that mORMot already has something similar available, though?

In its simplest way, egsuh is right: simply all commits are accepted, but that is not how distributed databases usually work, since the commits can be conflicting, so care needs to be taken, otherwise the database may be permanently in a conflicting state for that record and that is not good database management. You need write locks and a trigger, usually this is built-in in some form.
« Last Edit: April 08, 2026, 10:06:27 am by Thaddy »
Recovered from removal of tumor in tongue following tongue reconstruction with a part from my leg.

Zvoni

  • Hero Member
  • *****
  • Posts: 3349
Re: Changing the same record in DB
« Reply #4 on: April 08, 2026, 10:48:48 am »
As to SQLite:
"Default" is, if you have a "pending" Transaction (Not commited or rolled back), trying to commit (or is it "starting" the Transaction? Don't remember) your second Transaction results in an Exception "Database is locked"
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

cdbc

  • Hero Member
  • *****
  • Posts: 2716
    • http://www.cdbc.dk
Re: Changing the same record in DB
« Reply #5 on: April 08, 2026, 11:55:02 am »
Hi
With SQLite3:
What I usually do is: keep datasets in memory (like a ClientDataset), e.g.: TBufDataset, maybe :) then I write a middle-tier that funnels / sequences the changing of data to database one at the time, so _no_ locks...
Additionally one could use a lock-file with the 'tablename-recID' currently being in transaction, so that the user gets a warning, if he tries to change that specific record, this ofc. if we're talking 2 different processes, otherwise one could do it in memory...
Just my 2 cent's worth
Regards Benny
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE6/QT6 -> FPC Release -> Lazarus Release &  FPC Main -> Lazarus Main

jcmontherock

  • Sr. Member
  • ****
  • Posts: 347
Re: Changing the same record in DB
« Reply #6 on: April 08, 2026, 12:00:57 pm »
DB contained in one file are not done for multiple, several and simultaneous access to the same data.
Windows 11 UTF8-64 - Lazarus 4.6-64 - FPC 3.2.2

cdbc

  • Hero Member
  • *****
  • Posts: 2716
    • http://www.cdbc.dk
Re: Changing the same record in DB
« Reply #7 on: April 08, 2026, 12:04:13 pm »
Hi
I agree, it's usually not worth 'the mountain of fiddling' needed, when you've got several good multi-user-databases available...
Regards Benny
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE6/QT6 -> FPC Release -> Lazarus Release &  FPC Main -> Lazarus Main

LemonParty

  • Sr. Member
  • ****
  • Posts: 438
Re: Changing the same record in DB
« Reply #8 on: April 08, 2026, 03:42:51 pm »
Thank you for answers. This two articles https://en.wikipedia.org/wiki/Isolation_(database_systems), https://en.wikipedia.org/wiki/ACID have an explanation about such situations.
Lazarus v. 4.99. FPC v. 3.3.1. Windows 11

 

TinyPortal © 2005-2018