[Interest] Close DB connection when not actively using?

Israel Brewster ibrewster at flyravn.com
Tue Aug 21 22:54:42 CEST 2018


Ok, maybe you can give me a pointer on what I'm doing wrong then. My code is structured like the following:

bool openDatabase(){
QSqlDatabase db=QSqlDatabase::addDatabase('QSQLITE');
db.setDatabaseName(<path_to_my_database_file>);
return db.open();
}

 int main(int argc, char *argv[])
{
QApplication a(argc, argv);
...
<various initialization code>
...
bool dbOpen=openDatabase();
...
<more initilization>
return a.exec();
}

And if I put a breakpoint at any point after the call to OpenDatabase, the database is open - it does not close, even though that QSqlDatabase object went out of scope. At no point in my application do I keep a copy of QSqlDatabase around in any of my classes - every call to QSqlDatabase is as a function local member, and as such goes out of scope (thereby theoretically calling the destructor) as soon as the function exits.

So what am I missing here?

-----------------------------------------------
Israel Brewster
Systems Analyst II
5245 Airport Industrial Rd
Fairbanks, AK 99709
(907) 450-7293
-----------------------------------------------

[cid:8f9ccd5b-5a65-4794-b193-af4863e7f7aa at flyravn.com]



[cid:a01d4aae-c628-436a-a1af-b0b73bd6c7bd at flyravn.com]







On Aug 21, 2018, at 12:31 PM, Roland Hughes <roland at logikalsolutions.com<mailto:roland at logikalsolutions.com>> wrote:


Why are you creating yet another class instead of properly structuring your application?

Take a gander at

http://www.logikalsolutions.com/wordpress/information-technology/qt-and-usb-pt-4/

for some ideas. You probably also want to find a copy of this book

http://www.theminimumyouneedtoknow.com/qt_book.html

Most importantly you need to read the fine manual.

http://doc.qt.io/qt-5/qsqldatabase.html#dtor.QSqlDatabase

QSqlDatabase::~QSqlDatabase()

Destroys the object and frees any allocated resources.

Note: When the last connection is destroyed, the destructor implicitly calls close<http://doc.qt.io/qt-5/qsqldatabase.html#close>() to release the database connection.

See also close<http://doc.qt.io/qt-5/qsqldatabase.html#close>().

If your database is open for the life of the application then the application has a failed architecture. Database connections aren't supposed to have actual life spans.

On 08/21/2018 03:08 PM, Israel Brewster wrote:

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.


--
Roland Hughes, President
Logikal Solutions
(630) 205-1593

http://www.theminimumyouneedtoknow.com<http://www.theminimumyouneedtoknow.com/>
http://www.infiniteexposure.net<http://www.infiniteexposure.net/>
http://www.johnsmith-book.com<http://www.johnsmith-book.com/>
http://www.logikalblog.com<http://www.logikalblog.com/>
http://www.interestingauthors.com/blog
http://lesedi.us<http://lesedi.us/>

_______________________________________________
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/55678c1e/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/55678c1e/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/55678c1e/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/55678c1e/attachment.bin>


More information about the Interest mailing list