[Interest] Close DB connection when not actively using?

Glen Mabey gmabey at swri.org
Tue Aug 21 20:44:11 CEST 2018


I suggest you do not subclass any of those Qt classes, but instead create a new class that derives from QObject with the signals, slots and methods that you need.  Those methods could even have the same names as those of QSqlQuery, for example.

One of the methods of that class could be to set the timeout for a QTimer* data member that would close the database connection.

When your "exec()" method is invoked, it first checks whether the connection is open, and remedies that situation as needed.

Glen

________________________________
From: Interest [interest-bounces+gmabey=swri.org at qt-project.org] on behalf of Israel Brewster [ibrewster at flyravn.com]
Sent: Tuesday, August 21, 2018 11:23 AM
To: interest at qt-project.org
Subject: [Interest] Close DB connection when not actively using?

I have an application that may be left running for extended periods of time. It uses a SQLite database via QSqlDatabase and various other QtSQL classes (QSQLTableModel, QSqlQuery, etc, depending on where in the program it is used). The problem is that once I call open() on the database, it remains open until I explicitly close it, which is not until the program exits. Since this means the SQLite file is held open, this causes problems with things like backup programs that don't back up open files, or potentially services like dropbox (haven't tested that one thoroughly) that try to sync files between different computers.

Ideally, what I'd have is a system where you set up the connection, and then it automatically opens/closes the connection when needed (i.e. when doing a SELECT, INSERT, UPDATE, etc). With my app, this would generally only be for a) periodic updates or b) in direct response to user input, so most of the time the connection could remain closed. To the best of my knowledge (correct me if I am wrong), this is not possible with the existing QSqlDatabase/ QSQLITE database driver classes.

So, assuming that I need to subclass something to implement such functionality:

- Which class(es) will I need to subclass - QSqlDatabase? The SQLite Driver? Both?
- Which functions specifically should I be looking at modifying?
- How do classes like QSqlTableModel interact with the underlying QSqlDatabase as far as performing queries - would they know or care about the connection being closed at any point other than when it is explicitly doing a select or update/insert/delete?

I do realize this could result in multiple open/close cycles in a rapid period of time (maybe I could reduce that by putting some sort of timer on the automatic close), but for the purposes of my application I don't see this as being an issue. Thanks!
-----------------------------------------------
Israel Brewster
Systems Analyst II
5245 Airport Industrial Rd
Fairbanks, AK 99709
(907) 450-7293
-----------------------------------------------

[cid:e84a5d6b-1779-4d69-afc9-781595d38945 at flyravn.com]



[cid:0c00c43a-c113-4a29-bfba-108a7d1636eb at flyravn.com]







-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20180821/292aa6c3/attachment.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: image001.jpg
Type: image/jpeg
Size: 2298 bytes
Desc: image001.jpg
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20180821/292aa6c3/attachment.jpg>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: image002.jpg
Type: image/jpeg
Size: 6246 bytes
Desc: image002.jpg
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20180821/292aa6c3/attachment-0001.jpg>


More information about the Interest mailing list