Confused with disconnecting from DB in correct way
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
Quote:
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
Code:
void ConnectionQuery
::connect(QString connName, ConnectionType cType
){ 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(){
database.close();
cStatus = codec->toUnicode("Rozłączony z bazą danych");
}
Yet the warning is still present, or rather maybe exception is still here :confused:
Re: Confused with disconnecting from DB in correct way
I don't think this is ok. This would be better:
Code:
void ConnectionQuery::disconnect(){
{
database.close();
}
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.
Re: Confused with disconnecting from DB in correct way
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" ;-)
Re: Confused with disconnecting from DB in correct way
I mean some persistent objects that are either QSqlQuery or QSqlQueryModel or derived from any of them.
Re: Confused with disconnecting from DB in correct way
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 ?
Re: Confused with disconnecting from DB in correct way
Quote:
Originally Posted by
kornicameister
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.
Re: Confused with disconnecting from DB in correct way
and now I can with my conscious clear say, that I know what is all about
thank You for Your help