[Qt-interest] QtSql connection timeout error
Thomas Fjellstrom
tfjellstrom at shaw.ca
Thu Sep 17 23:00:23 CEST 2009
On Thu September 17 2009, Thomas Fjellstrom wrote:
> On Thu September 17 2009, Stephen Jackson wrote:
> > On Thu, Sep 17, 2009 at 7:30 PM, Thomas Fjellstrom wrote:
> > > I've created a little long running process (a server), which connects
> > > to MySQL through the QtSql api, but occasionally the mysql connection
> > > times out due to inactivity (i assume). I've tried checking
> > > connection.isOpen(),
> > > connection.isValid(), and the connection.lastError(), but none of them
> > > seem to hint that there's even been a problem. isOpen returns true,
> > > isValid returns true, and lastError().code() is QSqlError::NoError but
> > > every single query.exec() fails.
> > >
> > > I'm stumped. How can I reliably detect when the connection drops like
> > > this?
> >
> > Perhaps these two recent threads might be relevant to this problem.
> >
> > http://lists.trolltech.com/pipermail/qt-interest/2009-August/011477.html
> >
> > http://lists.trolltech.com/pipermail/qt-interest/2009-September/012347.ht
> >ml
>
> Partially valid. But lastError() should actually say there's an error if
> exec() fails, no?
>
The interesting bit, is I'm now checking prepare as well for the same bug, but
prepare will return a proper error, at least the text of the error will be
something like "server has gone away", though the type() will be
QSqlError::StatementError, which seems odd.
I've now worked around the issue, by checking for type() == NoError /or/
type() == StatementError, as well as isOpen/isValid == true, and it seems to
be able to handle this case. In case the server was actually down at the time,
I had to add a second case where it just checks isOpen, because if db.open()
fails, it will actually make isOpen return false, which is helpful.
Now at least my server application manages to stay connected. And it doesn't
just open a new connection for each request, which is nice.
--
Thomas Fjellstrom
tfjellstrom at shaw.ca
More information about the Qt-interest-old
mailing list