Recent

Author Topic: Databaselock with SQlite table(s)  (Read 849 times)

Hansvb

  • Hero Member
  • *****
  • Posts: 909
Databaselock with SQlite table(s)
« on: April 05, 2026, 09:11:19 pm »
Hi,

I have a database lock that I can't find. I have to make a mistake somewhere in the following. I have 2 datagrids on a Form, each with its own connection, transaction and query, to be provided with data. That works, but if I then want to expand 1 of both datasets with a new record, I get a datalock message.
I have brought this back to a test project. See the attachment. This creates a SQLite database in the exe folder during startup and then creates 2 dummy tables and 1 record of data. If you then click on the btnAdd1 or btnAdd2 button, you will see the datalock message. I use express 2 connections to the database because otherwise after changing data in 1 grid the other grid went empty.

How can I put 2 datagrids on a screen, each with their own dataset (no master detail). And the dataset per Dbgrid must be editable.

LemonParty

  • Sr. Member
  • ****
  • Posts: 468
Re: Databaselock with SQlite table(s)
« Reply #1 on: April 05, 2026, 09:33:37 pm »
Quote
after changing data in 1 grid the other grid went empty
Have you consider to refresh your other dataset by closing and opening it?

Why do you use 2 connections? It seems like one is enough for your needs.
Lazarus v. 4.99. FPC v. 3.3.1. Windows 11

Sieben

  • Sr. Member
  • ****
  • Posts: 390
Re: Databaselock with SQlite table(s)
« Reply #2 on: April 05, 2026, 10:09:46 pm »
Two connections in default mode will always lock each other because each will start a transaction on opening a dataset. The reason why your other grid went empty when using only one connection after changing data is again the default mode of SQLdb, which closes all tables of a connection after commit. You can prevent this by setting the option sqoKeepOpenOnCommit on your datasets or by using CommitRetaining. You really should (re)read the documention of SQLdb.
Lazarus 2.2.0, FPC 3.2.2, .deb install on Ubuntu Xenial 32 / Gtk2 / Unity7

cdbc

  • Hero Member
  • *****
  • Posts: 2786
    • http://www.cdbc.dk
Re: Databaselock with SQlite table(s)
« Reply #3 on: April 05, 2026, 11:32:18 pm »
Hi Hans
@Sieben is right.
What I usually do is: keep both 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...
IIRC there should be something in my 'Diary' app's BOM, worth looking at...
A middle-tier is like a Presenter, to put it in words you can relate to  :D
It controls & orchestrates...
Regards Benny
« Last Edit: April 05, 2026, 11:33:56 pm by cdbc »
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE6/QT6 -> FPC Release -> Lazarus Release &  FPC Main -> Lazarus Main

Hansvb

  • Hero Member
  • *****
  • Posts: 909
Re: Databaselock with SQlite table(s)
« Reply #4 on: April 06, 2026, 09:47:21 am »
@Sieben, thanks this will help me move forward.
I now have 1 connection and 1 transaction and 2 tsqlqueries. If I now add a record to dbgrid 1, dbgrid 2 remains intact.

@benny, I know..., I downloaded your 'Diary' app a while ago and still have to sit down and take a good look at it. :-[ My "problem" is that I still want to learn/read a lot but also want to see some progress in my trying projects. In short, always a kind of lack of time :-X.

Edit: added the attachment
« Last Edit: April 06, 2026, 09:52:29 am by Hansvb »

Xenno

  • Jr. Member
  • **
  • Posts: 88
    • BS Programs
Re: Databaselock with SQlite table(s)
« Reply #5 on: April 06, 2026, 11:40:43 am »
You may want to take look on WAL for your database's journal mode. Although it does not prevent table lock, it might help for your case. I often use WAL to smoothen processing in separated connections.

https://sqlite.org/wal.html
Lazarus 4.0, Windows 10, https://www.youtube.com/@bsprograms

Zvoni

  • Hero Member
  • *****
  • Posts: 3370
Re: Databaselock with SQlite table(s)
« Reply #6 on: April 07, 2026, 08:45:58 am »
Journal-Mode = WAL and pending Transactions have already been mentioned.
I'd additionally take a look at the "OpenFlags" of the SQLite-Connection
https://sqlite.org/c3ref/open.html
"sofNoMutex" looks promising....
Quote
SQLITE_OPEN_NOMUTEX
    The new database connection will use the "multi-thread" threading mode. This means that separate threads are allowed to use SQLite at the same time, as long as each thread is using a different database connection.
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

 

TinyPortal © 2005-2018