Results 1 to 7 of 7

Thread: Confused with disconnecting from DB in correct way

  1. #1
    Join Date
    Sep 2010
    Location
    Poland
    Posts
    112
    Thanks
    8
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Maemo/MeeGo

    Default 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
    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
    Qt Code:
    1. void ConnectionQuery::connect(QString connName, ConnectionType cType){
    2. QSqlDatabase database = QSqlDatabase::addDatabase("QMYSQL",cD.data()->getConnectionName());
    3. if(cD->getConnectionName() == connName){
    4. qDebug() << ("ConnectionQuery::connect() - connName argument and connectionName from ConnectioData are the same");
    5. }
    6. database.setUserName(this->cD.data()->getUser());
    7. database.setPassword(this->cD.data()->getPass());
    8. database.setHostName(this->cD.data()->getHost());
    9. database.setPort(this->cD.data()->getPort());
    10. database.setDatabaseName(this->cD.data()->getConnectionName());
    11. if(!database.open()){
    12. qErrnoWarning("ConnectionQuery::connect() - connecting failed");
    13. cStatus = database.lastError().text();
    14. }else{
    15. QString tmp("Połączony z bazą danych ");
    16. tmp.append(cD.data()->getConnectionName());
    17. cStatus = codec->toUnicode(tmp.toLatin1());
    18. }
    19. }
    20.  
    21. void ConnectionQuery::disconnect(){
    22. QSqlDatabase database = QSqlDatabase::database(connectionName);
    23. database.close();
    24. QSqlDatabase::removeDatabase(this->connectionName);
    25. cStatus = codec->toUnicode("Rozłączony z bazą danych");
    26. }
    To copy to clipboard, switch view to plain text mode 

    Yet the warning is still present, or rather maybe exception is still here
    My schedule makes my cry
    My works makes my laugh
    My life makes... oh...no....

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Confused with disconnecting from DB in correct way

    I don't think this is ok. This would be better:
    Qt Code:
    1. void ConnectionQuery::disconnect(){
    2. {
    3. QSqlDatabase database = QSqlDatabase::database(connectionName);
    4. database.close();
    5. }
    6. QSqlDatabase::removeDatabase(this->connectionName);
    7. cStatus = codec->toUnicode("Rozłączony z bazą danych");
    8. }
    To copy to clipboard, switch view to plain text mode 

    But it all depends whether you're not having any member variables that operate on the database.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Sep 2010
    Location
    Poland
    Posts
    112
    Thanks
    8
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Maemo/MeeGo

    Default 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" ;-)
    Last edited by kornicameister; 30th October 2010 at 21:10.
    My schedule makes my cry
    My works makes my laugh
    My life makes... oh...no....

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default 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.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    Sep 2010
    Location
    Poland
    Posts
    112
    Thanks
    8
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Maemo/MeeGo

    Default 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 ?
    My schedule makes my cry
    My works makes my laugh
    My life makes... oh...no....

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Confused with disconnecting from DB in correct way

    Quote Originally Posted by kornicameister View Post
    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.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  7. The following user says thank you to wysota for this useful post:

    kornicameister (30th October 2010)

  8. #7
    Join Date
    Sep 2010
    Location
    Poland
    Posts
    112
    Thanks
    8
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Maemo/MeeGo

    Default 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
    My schedule makes my cry
    My works makes my laugh
    My life makes... oh...no....

Similar Threads

  1. confused about licenses ...
    By jimmybaeten in forum Installation and Deployment
    Replies: 10
    Last Post: 26th July 2009, 10:56
  2. Layouts - very confused
    By lhartzman in forum Newbie
    Replies: 3
    Last Post: 19th July 2009, 21:40
  3. Disconnecting from database after using QSqlTableModel
    By RobbieClarken in forum Qt Programming
    Replies: 6
    Last Post: 8th April 2009, 09:51
  4. confused about Qt designer 4.3
    By rishiraj in forum Qt Tools
    Replies: 1
    Last Post: 14th December 2008, 19:13
  5. Replies: 15
    Last Post: 6th April 2008, 10:06

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.