[Interest] Close DB connection when not actively using?

Israel Brewster ibrewster at flyravn.com
Tue Aug 21 22:08:06 CEST 2018


Ok, yeah, that could work. Then I'd simply have a member that is the original class, and call it for the data processing once I have verified connection state. Sounds like a plan, I'll give it a shot!
-----------------------------------------------
Israel Brewster
Systems Analyst II
5245 Airport Industrial Rd
Fairbanks, AK 99709
(907) 450-7293
-----------------------------------------------

[cid:ea49bbc5-e539-4aee-9f26-34bcd6e3dea2 at flyravn.com]



[cid:dea9973e-0608-4e10-bc95-ef7ca5e5e89a at flyravn.com]







On Aug 21, 2018, at 10:44 AM, Glen Mabey <gmabey at swri.org<mailto:gmabey at swri.org>> wrote:


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<mailto:interest-bounces+gmabey=swri.org at qt-project.org>] on behalf of Israel Brewster [ibrewster at flyravn.com<mailto:ibrewster at flyravn.com>]
Sent: Tuesday, August 21, 2018 11:23 AM
To: interest at qt-project.org<mailto: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
-----------------------------------------------

<image001.jpg>



<image002.jpg>







_______________________________________________
Interest mailing list
Interest at qt-project.org<mailto:Interest at qt-project.org>
http://lists.qt-project.org/mailman/listinfo/interest

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20180821/c65c2b6a/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/c65c2b6a/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/c65c2b6a/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/c65c2b6a/attachment.bin>


More information about the Interest mailing list