Results 1 to 5 of 5

Thread: How to detect QSqlDatabase is still open???

  1. #1
    Join Date
    Oct 2010
    Posts
    18
    Thanks
    2
    Qt products
    Qt4 Qt/Embedded Qt Jambi
    Platforms
    Unix/X11 Windows

    Default How to detect QSqlDatabase is still open???

    Hi.

    I have wrote some the thread run loop where i every iteration select some data from my database. In every iteration fisrt i check if database is open. if it open i am quering. Code looks like this.
    Qt Code:
    1. void MyThread::run()
    2. {
    3. while (m_running)
    4. {
    5. QSqlDatabase db = QSqlDatabase::database(CONNECTION_NAME);
    6.  
    7. // In this code if /etc/init.d/network stop (network closed) db.isOpen() returns true.
    8. if (db.isOpen())
    9. {
    10. QSqlQuery query1(db);
    11. // some code
    12. }
    13.  
    14. msleep(1000);
    15. }
    16. }
    To copy to clipboard, switch view to plain text mode 

    But, for testing i close the network, in linux using /etc/init.d/network stop. In this case db.isOpen() return true and in my query.exec() my program frozes in exec code.
    How can i detect in this case that connection is stil open or network works or databse in remote server is not stopped?

    Thanks.
    Last edited by hashimov; 25th January 2011 at 07:27.

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: How to detect QSqlDatabase is still open???

    Please use [code] tags around code.

    What is the error type from QSqlDatabase::lastError() the first time you execute your query after the connection is lost?

  3. #3
    Join Date
    Oct 2010
    Posts
    18
    Thanks
    2
    Qt products
    Qt4 Qt/Embedded Qt Jambi
    Platforms
    Unix/X11 Windows

    Default Re: How to detect QSqlDatabase is still open???

    db.isOpen() returns true even it db.open() is not called.

  4. #4
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: How to detect QSqlDatabase is still open???

    QSqlDatabase::database() will open the database when called unless Qt thinks it is already open, or you tell it not to with the optional second parameter.

    Qt has no way of knowing the connection is lost due to some external event until you try to use the connection, hence my question: What is the error type from QSqlDatabase::lastError() the first time you execute your query after the connection is lost? I suspect you will receive a QSqlError of type QSqlError::ConnectionError and should close() the database at that point so that Qt knows it is no longer good. You should also check QSqlDatabase::isOpenError() if the server disappearing is a likely event.

  5. #5
    Join Date
    Oct 2010
    Posts
    18
    Thanks
    2
    Qt products
    Qt4 Qt/Embedded Qt Jambi
    Platforms
    Unix/X11 Windows

    Default Re: How to detect QSqlDatabase is still open???

    There is not any output of QSqlDatabase::lastError(). QSqlDatabase::lastError() - outputs QSqlDatabasePrivate::database: unable to open database: "ORA-12157: TNS:internal network communication error when network is closed and in closing network i run my application. But when i run application, and after running i close network there is not any output and db.isOpen() returns true.


    Added after 7 minutes:


    Quote Originally Posted by ChrisW67 View Post
    QSqlDatabase::database() will open the database when called unless Qt thinks it is already open, or you tell it not to with the optional second parameter.

    Qt has no way of knowing the connection is lost due to some external event until you try to use the connection, hence my question: What is the error type from QSqlDatabase::lastError() the first time you execute your query after the connection is lost? I suspect you will receive a QSqlError of type QSqlError::ConnectionError and should close() the database at that point so that Qt knows it is no longer good. You should also check QSqlDatabase::isOpenError() if the server disappearing is a likely event.
    There is not any output of QSqlDatabase::lastError(). QSqlDatabase::lastError() - outputs QSqlDatabasePrivate::database: unable to open database: "ORA-12157: TNS:internal network communication error when network is closed and in closing network i run my application. But when i run application, and after running i close network there is not any output and db.isOpen() returns true.
    Last edited by hashimov; 25th January 2011 at 08:17.

Similar Threads

  1. Replies: 3
    Last Post: 25th August 2010, 12:39
  2. QSqlDatabase::open Segfaults
    By rich.remer in forum Qt Programming
    Replies: 3
    Last Post: 30th June 2010, 16:32
  3. QSqlDatabase::open() fails with QODBC
    By grzywacz in forum Qt Programming
    Replies: 4
    Last Post: 7th April 2010, 03:27
  4. Why QSqlDatabase::open() returns open?
    By gboelter in forum Newbie
    Replies: 7
    Last Post: 27th August 2009, 18:52
  5. Detect which OS?
    By December in forum Qt Programming
    Replies: 6
    Last Post: 24th May 2007, 23:01

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.