PDA

View Full Version : QSqlDatabasePrivate::removeDatabase: connection 'menu_settings' is still in use, all



rianquinn
5th March 2006, 00:06
When I close my application I get the following message

QSqlDatabasePrivate::removeDatabase: connection 'menu_settings' is still in use, all queries will cease to work.

Not sure what this is. I have tried closing all the databases I make, and I have tried clearing all of the queries I have made and nothing seems to get rid of this message. The only thing that I know does work is if I don't use the database in a plugin, I don't get this message.

Thanks

jacek
5th March 2006, 00:51
Maybe that plugin uses one of the QSql*Model classes?

rianquinn
5th March 2006, 13:08
the plugin does use QSqlModel classes, but if I comment them out, I still get this bizzar error. I think I am going to try to find out where in the Qt code its coming from.

jacek
5th March 2006, 17:12
How do you create that "menu_settings" connection?

rianquinn
5th March 2006, 17:17
in the plugin's constructor. I call:

QSqlDatabase db = QSqlDatabase::addDatabase ("QSQLITE", "menu_settings");

Is there another way to do this?

jacek
5th March 2006, 17:29
in the plugin's constructor. I call:

QSqlDatabase db = QSqlDatabase::addDatabase ("QSQLITE", "menu_settings");
Does this error occur if you add similar statement in the application?


Is there another way to do this?
Probably not.

rianquinn
5th March 2006, 18:03
The SQLITE database is being used by each plugin and the main application. This error only occurs when its added in the plugins. Meaning, if I disable all of the plugins, even though I am calling this line of code in the main app, I do not get the error.

jacek
5th March 2006, 18:53
Do you destroy the objects retrieved from plugins when your application closes?

rianquinn
5th March 2006, 19:13
no, the QSqlDatabase is being defined by value not by reference. The weird thing is, if when the plugin is unloaded, I call QSqlDatabase::removeDatabase, I still get the error. But if I run QSqlDatabase::removeDatabase in the main app, no error occurs. In fact, no matter where I call QSqlDatabase::removeDatabase in the plugin, I get the error, and the error occurs when this function is called. So I guess, if you don't call this function it is called for you.

Basically, I have figured out that the function call QSqlDatabase::removeDatabase, is what is causing the error, but how do you prevent this error from occuring when you call this function from a plugin

jacek
5th March 2006, 19:35
The weird thing is, if when the plugin is unloaded, I call QSqlDatabase::removeDatabase, I still get the error.
The only thing that comes to my mind is that QApplication instance might be destroyed before the plugin (or rather the object that was created by the plugin).

Did you check Trolltech task tracker? This might be also some bug.

rianquinn
6th March 2006, 16:08
Ok..... here is the solution. When making a plugin, you have to make a class that inherits your Plugin Interface. In the private section of this class, I defined the database that I would be using. Turns out that the main application closes all database connections BEFORE closing plugins. I didn't see this because I put the QSqlDatabase::removeDatabase function in the same function as QSqlDatabase::addDatabase, which Qt doesn't like. Instead, you have to destroy the QSqlDatabase object FIRST, then call QSqlDatabase::removeDatabase. So how does this apply to plugins, well, because I put the definition of the QSqlDatabase::addDatabase at the class scope, this object was getting destroyed AFTER the QSqlDatabase::removeDatabase function was being called, (because this function is called, then the plugins are destroyed.) To fix this problem, you have to destroy the database object, then unload the plugins. The only way I found that this can be done is by leaving the definition of the QSqlDatabase in the functions that they are needed, not at the class level. So instead of this:

class
{
Public
Func ()
Private
QSqlDatabase
}

do this:

class
{
Public
Func ()
}

Func ()
{
QSqlDatabase
}

If more info is needed, just let me know. I hope this helps people in the future!

:)