[Interest] Close DB connection when not actively using?

Israel Brewster ibrewster at flyravn.com
Tue Aug 21 18:23:57 CEST 2018


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/25c4e06b/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/25c4e06b/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/25c4e06b/attachment-0001.jpg>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: Israel Brewster.vcf
Type: text/directory
Size: 417 bytes
Desc: Israel Brewster.vcf
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20180821/25c4e06b/attachment.bin>


More information about the Interest mailing list