Results 1 to 7 of 7

Thread: QSqlQuery.clear() not clearing?

  1. #1
    Join Date
    Jan 2006
    Location
    Lincoln, NE USA
    Posts
    177
    Thanks
    3
    Thanked 7 Times in 6 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Question QSqlQuery.clear() not clearing?

    My first production app has one annoying problem.

    After a clerk makes about 150 record entries a/o changes the app throws an ORA-01000 error, meaning that MAX_OPEN_CURSORS has been reached. That value is 300 per user session.

    I decided early on that I wanted to 'reuse' my queries so I thought I had designed them that way.

    First, I declared the query in the header:
    Qt Code:
    1. public:
    2. QSqlQuery persQry;
    3. ...
    To copy to clipboard, switch view to plain text mode 
    then, in the class intializer code I put the same line.

    In each function which used persQry I used the following method
    Qt Code:
    1. myappclass::somefunction(){
    2. ...
    3. QString somestr = "SELECT * ......";
    4. persQry.clear();
    5. persQry.exec(somestr);
    6. ...
    7.  
    8. }
    To copy to clipboard, switch view to plain text mode 
    Our Oracle Admin watched the cursor count and said that every time "persQry.exe(somestr);" was executed the cursor count would increase and never decrease. "persQry.clear();" made no difference at all.

    The QT Assistance says that
    void QSqlQuery::clear ()
    Clears the result set and releases any resources held by the query. You should rarely if ever need to call this function.
    Apparently it's only refering to QT resources, not Oracle cursors.


    Unless there is something I overlooked I'm going to have to rewrite the code using pointers on the heap instead of automatic variables on the stack. But, how do I know that the same problem won't still exist..? The Assistant didn't distuingish between queries made on the stack or the heap. persQry.clear() should clear the result set (cursor) and "any" resources.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QSqlQuery.clear() not clearing?

    What is that "myappclass" class? How many instances it has?

  3. #3
    Join Date
    Jan 2006
    Location
    Lincoln, NE USA
    Posts
    177
    Thanks
    3
    Thanked 7 Times in 6 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QSqlQuery.clear() not clearing?

    homestead.h
    Qt Code:
    1. #ifndef HOMESTEAD_H
    2. #define HOMESTEAD_H
    3. #include <QMainWindow>
    4. class homestead : public QMainWindow
    5. .....
    6. {
    7. Q_OBJECT
    8.  
    9. public:
    10. homestead(QWidget *parent = 0);
    11. ....
    12. QSqlQuery persQry;
    13. ....
    14. ...
    15. }
    To copy to clipboard, switch view to plain text mode 
    and
    homestead.cpp
    Qt Code:
    1. /*
    2. Program: homestead.cpp
    3. */
    4. #include "homestead.h"
    5. #include "wholenamedlg.h"
    6. #include "historydialog.h"
    7. ...
    8.  
    9. homestead::homestead(QWidget *parent) : QMainWindow(parent) {
    10. ui.setupUi(this); // draw the gui interface
    11. ...
    12. // create global query objects
    13. QSqlQuery persQry;
    14. ...
    15. }
    16. void homestead::searchAll() {
    17. .....
    18. QString queryStr = "";
    19. ...
    20. if (ui.rbProprtyID->isChecked()) {
    21. // user is searching for property id
    22. requestString.append(" in Property");
    23. ui.leStatus->setText(requestString);
    24. queryStr = this->propPIDqryStr; // default property id select
    25. queryStr.append(ui.leSearch->text()); // append property id
    26. propQry.clear();
    27. propQry.exec(queryStr);
    28. if (propQry.first()){
    29. // found property record, now fetch associated persinfo record
    30. foundProp = true;
    31. queryStr = this->persPIDqryStr;
    32. queryStr.append(ui.leSearch->text());
    33. persQry.clear();
    34. persQry.exec(queryStr);
    35. if (persQry.first()) {
    36. foundPers = true;
    37. }
    38. ...
    39. }
    40. ...
    To copy to clipboard, switch view to plain text mode 
    and main.cpp
    Qt Code:
    1. .....
    2. int main( int argc, char * argv[] ) {
    3. QString strRejected = "";
    4. QApplication app(argc, argv);
    5. app.setQuitOnLastWindowClosed(false);
    6. dlgLogin dlg;
    7. if( dlg.exec() == QDialog::Accepted ){
    8. QSqlDatabase hapdb = QSqlDatabase::addDatabase(DBDRIVER);
    9. hapdb.setHostName(DBHOST);
    10. hapdb.setDatabaseName(DBNAME);
    11. hapdb.setUserName(dlg.dui.leUserName->text());
    12. hapdb.setPassword(dlg.dui.leUserPassword->text());
    13. if ( hapdb.open() ) {
    14. homestead ht;
    15. ht.RevID = dlg.dui.leUserName->text();
    16. ht.show();
    17. app.setQuitOnLastWindowClosed(true);
    18. return app.exec();
    19. } else {
    20. strRejected = QString("The Login was rejected because: %1").arg(hapdb.lastError().text()).toLatin1();
    21. QMessageBox::information(0,"Login Rejected!",strRejected,
    22. QMessageBox::Ok,QMessageBox::NoButton,QMessageBox::NoButton);
    23. return 1;
    24. }
    25. } else {
    26. strRejected = QString("User Canceled the login!").toLatin1();
    27. QMessageBox::information(0,"Login Rejected!",strRejected,
    28. QMessageBox::Ok,QMessageBox::NoButton,QMessageBox::NoButton);
    29. return 2;
    30. }
    31. }
    To copy to clipboard, switch view to plain text mode 

    A cursor is created each time "persQry.exec(queryStr): is executed but it is not released by persQry.clear*() the next time the searchAll function is called.
    The Oracle cursor count continues to increase until MAX_OPEN_CURSOR is reached.

  4. #4
    Join Date
    Jan 2006
    Location
    Ukraine,Lviv
    Posts
    454
    Thanks
    9
    Thanked 27 Times in 27 Posts
    Qt products
    Qt3
    Platforms
    Unix/X11 Windows

    Default Re: QSqlQuery.clear() not clearing?

    Why are you create in constructor global instance of QSqlQuery?
    a life without programming is like an empty bottle

  5. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QSqlQuery.clear() not clearing?

    Quote Originally Posted by GreyGeek
    homestead::homestead(QWidget *parent) : QMainWindow(parent) {
    ui.setupUi(this); // draw the gui interface
    ...
    // create global query objects
    QSqlQuery persQry;
    ...
    }
    This doesn't create a global object, but a local variable.

    Quote Originally Posted by GreyGeek
    A cursor is created each time "persQry.exec(queryStr): is executed but it is not released by persQry.clear*() the next time the searchAll function is called.
    The Oracle cursor count continues to increase until MAX_OPEN_CURSOR is reached.
    Everything looks OK, so it's probably a Qt bug.

  6. #6
    Join Date
    Jan 2006
    Location
    Lincoln, NE USA
    Posts
    177
    Thanks
    3
    Thanked 7 Times in 6 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Unhappy Re: QSqlQuery.clear() not clearing?

    This doesn't create a global object, but a local variable.
    True, but it's "global" to all the functions in the homestead class, so I don't have to pass the query object from function to function.
    Everything looks OK, so it's probably a Qt bug.
    I came to your conclusions too, but I'm a relative newbie to QT and C++, and was not absolutely sure. I value yours and Wyota's opinions highly and hoped you'd answer, which is why I posted my question. We have a support contract and I've posted the question to the QT support staff. I'll let you know what they tell me.

    Rats! I was hoping that it was my coding error., , and that it could be fixed quickly with a minor change or the addition of some "secret" paramter in the query intialization. But, it looks like it will only be fixed by a support patch or by upgrading QT 4.1 itself. That was something I was puttng off until I finished my second project, which I am deep into.

  7. #7
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QSqlQuery.clear() not clearing?

    Quote Originally Posted by GreyGeek
    True, but it's "global" to all the functions in the homestead class, so I don't have to pass the query object from function to function.
    Unfortunately it's not --- you are writing about this variable:
    Qt Code:
    1. class homestead : public QMainWindow
    2. {
    3. ...
    4. QSqlQuery persQry; // <---
    5. ...
    6. }
    To copy to clipboard, switch view to plain text mode 
    While this one:
    Qt Code:
    1. homestead::homestead(QWidget *parent) : QMainWindow(parent) {
    2. ...
    3. // create global query objects
    4. QSqlQuery persQry; // <---
    5. ...
    6. }
    To copy to clipboard, switch view to plain text mode 
    Is a local variable and it will be destroyed when the constructor ends.

    Here's a classic example of this error: http://www.qtcentre.org/forum/showthread.php?t=2078

  8. The following user says thank you to jacek for this useful post:

    GreyGeek (7th May 2006)

Similar Threads

  1. Replies: 3
    Last Post: 12th February 2008, 21:17
  2. Having trouble clearing a QTreeWidget.
    By Nyphel in forum Qt Programming
    Replies: 28
    Last Post: 10th October 2007, 15:33
  3. clearing lineEdits
    By Salazaar in forum Newbie
    Replies: 4
    Last Post: 4th June 2007, 21:02
  4. Clearing QTable
    By Lebowski in forum Newbie
    Replies: 1
    Last Post: 20th April 2006, 08:24
  5. What's the correct way of clearing a pixmap?
    By karye in forum Qt Programming
    Replies: 4
    Last Post: 19th January 2006, 17:46

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.