PDA

View Full Version : Confused with disconnecting from DB in correct way



kornicameister
29th October 2010, 23:11
After I've read something like 40 threads in this forum and the docs I failed to find correctly-working solution for my problem with disconnection from DB
I constantly have this output when disconnection takes place

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

here is my code which I assume is written like wysota proposed in one of thread I'h read

void ConnectionQuery::connect(QString connName, ConnectionType cType){
QSqlDatabase database = QSqlDatabase::addDatabase("QMYSQL",cD.data()->getConnectionName());
if(cD->getConnectionName() == connName){
qDebug() << ("ConnectionQuery::connect() - connName argument and connectionName from ConnectioData are the same");
}
database.setUserName(this->cD.data()->getUser());
database.setPassword(this->cD.data()->getPass());
database.setHostName(this->cD.data()->getHost());
database.setPort(this->cD.data()->getPort());
database.setDatabaseName(this->cD.data()->getConnectionName());
if(!database.open()){
qErrnoWarning("ConnectionQuery::connect() - connecting failed");
cStatus = database.lastError().text();
}else{
QString tmp("Połączony z bazą danych ");
tmp.append(cD.data()->getConnectionName());
cStatus = codec->toUnicode(tmp.toLatin1());
}
}

void ConnectionQuery::disconnect(){
QSqlDatabase database = QSqlDatabase::database(connectionName);
database.close();
QSqlDatabase::removeDatabase(this->connectionName);
cStatus = codec->toUnicode("Rozłączony z bazą danych");
}


Yet the warning is still present, or rather maybe exception is still here :confused:

wysota
29th October 2010, 23:56
I don't think this is ok. This would be better:

void ConnectionQuery::disconnect(){
{
QSqlDatabase database = QSqlDatabase::database(connectionName);
database.close();
}
QSqlDatabase::removeDatabase(this->connectionName);
cStatus = codec->toUnicode("Rozłączony z bazą danych");
}

But it all depends whether you're not having any member variables that operate on the database.

kornicameister
30th October 2010, 21:02
well if I understood You correctly, you are asking whether or not there are other classess which I use to operate on database
to be honest there are several, each one of them specifies how to handle common tasks like inserting or deleting and each one of them "connects" to database using connection name, only here presented ConnectionQuery establishes sommething I call "live-connection" and it should also kill it, but it does not doing it properly

yet, I feel like I misunderstood You at the point "member variables" ;-)

wysota
30th October 2010, 21:55
I mean some persistent objects that are either QSqlQuery or QSqlQueryModel or derived from any of them.

kornicameister
30th October 2010, 22:18
no, not at the moment

by the way, it is quite interesting that putting the same code in the autonomic block affects with no warning and when the code is like free, the warning is generated

was it mentioned in docs ? maybe I have an amnesia or something ?

wysota
30th October 2010, 22:43
by the way, it is quite interesting that putting the same code in the autonomic block affects with no warning and when the code is like free, the warning is generated
This is very simple though not obvious. The warning is printed whenever there exists any object that references a given connection name (regardless if the connection is active or not). If you look at the first code you will notice that there is a QSqlDatabase object related to the connection name. The anonymous block causes the object to go out of scope so it is no longer there when removeConnection() is called.

kornicameister
30th October 2010, 23:37
and now I can with my conscious clear say, that I know what is all about

thank You for Your help