PDA

View Full Version : Properly close database handlers



ruben.rodrigues
23rd September 2010, 16:17
Hi all!

I have a database application running in a remote server that therefore works as dataserver. The client send a request and the server replies to it after getting some info from the database. The code works but must be sharpen because every 20 connections( more or less) it fails to connect to the database. Is this message related to the problem? I get it every connection but it still works:


QSqlDatabasePrivate::removeDatabase: connection 'qt_sql_default_connection' is still in use, all queries will cease to work.
QSqlDatabasePrivate::addDatabase: duplicate connection name 'qt_sql_default_connection', old connection removed.

how should I close the database properly?

Lykurg
23rd September 2010, 16:23
Before you remove a connection make sure all QSqlQuerry/QSqlDatabase/QSql*** object's are destroyed of cleared. Then you can remove the database.

ruben.rodrigues
23rd September 2010, 16:42
Before you remove a connection make sure all QSqlQuerry/QSqlDatabase/QSql*** object's are destroyed of cleared. Then you can remove the database.

Thanks for your answer!

I already tryed to clear close and delete all objects but the message keeps showing. I will post some of the code:

Header:


QSqlDatabase db;

Constructor:


db = QSqlDatabase::addDatabase("QMYSQL"); //select database type
db.setHostName("localhost"); //tell where the database is
db.setDatabaseName("qt"); //Database name
db.setUserName("user"); //username to connect to the database
db.setPassword("password"); //password from user

Function LoadProduct:


//check for errors on database connection
if (!db.open()) {
qDebug() << db.lastError();
// return 4;
}
//set table to use
QString table = "mytable";
QSqlQuery query("USE " + table);

//Query to get table info
query.exec("Select ProductName from "+ table);

//routine to read values
while (query.next()) {

QSqlRecord record = query.record();

//get ProductNames
ProductNames << query.value(record.indexOf("ProductName")).toString();
numberOfProducts++;
}


How should I close it now? I already tryed, db.close(), query.clear, db.removeDatabase("QMYSQL"), delete db, delete query, QSqlDatabase::removeDatabase("QMYSQL"), etc...
Remind you that the application works most of the times but the message keeps on comming.

Lykurg
23rd September 2010, 16:46
Simple solution: Don't use a member variable for QSqlDatabase! There is no need for it. You can always use the static QSqlDatabase::database() function to get a handle when you need it.

ruben.rodrigues
23rd September 2010, 17:00
do you mean that I don't need the db variable?
because I have the QSqlDatabase::database() but there is no function to set the username, password, database name and host..

or am I confused?

Lykurg
23rd September 2010, 17:10
You need it, but not as a member variable:
//somewhere in your definition
void Foo::bar()
{
//...
{
QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL"); //select database type
db.setHostName("localhost"); //tell where the database is
db.setDatabaseName("qt"); //Database name
db.setUserName("user"); //username to connect to the database
db.setPassword("password"); //password from user
} // here the temporary "db" gets deleted.
//...
}

void Foo::baz()
{
QSqlQuery q(QSqlDatabase::database());
// use q
} // here q gets deleted...