[Qt-interest] QtSql connection timeout error

Peter qt at bienert-rh.de
Fri Sep 18 11:33:26 CEST 2009


I had the same problem and solved it with a simple timer, all 2 minutes 
I fire the timer for each open connection to have a simple select 
count(*) from existing_table and I have no drops (except network errors 
or server break downs).. In my former api (before using SQL Plugins) I 
had a automatic mysql_ping established...
Regards,
Peter


Thomas Fjellstrom schrieb:
> On Fri September 18 2009, Raul Metsma wrote:
>> For Mysql reconnect we are using following code
>>
>> QVariant v = db.driver()->handle();
>> if( qstrcmp( v.typeName(), "MYSQL*" ) == 0 )
>> {
>>      MYSQL *handle = *static_cast<MYSQL **>( v.data() );
>>      if( handle != 0 )
>>      {
>> 	my_bool reconnect = 1;
>> 	mysql_options( handle, MYSQL_OPT_RECONNECT, &reconnect );
>>      }
>> }
>>
>> It works just fine
> 
> That's not bad. But If I ever want to change databases, that code becomes dead 
> weight. I might use something similar, maybe the ping method, or some command 
> that lets me check to see if the socket is closed or half closed.
> 
> Thanks :)
> 
>> Raul Metsma
>>
>> Mikhail Veygman wrote:
>>> Not having QT code or documentation makes this a little harder but ...
>>>
>>> When mysql server drops a connection from the client the client does
>>> not detect this until the connection is used again. Which is precisely
>>> the reason that you are getting an error. However mysql C API provides
>>> a mysql_ping function which if the connection is dropped by the server
>>> it will reestablish the connection to the server without having to
>>> recreate the mysql connection handle. So 2 things you can check:
>>>
>>> 1. Check if QMySQL driver calls this function prior to sending a query
>>>
>>> 2. If there is a DB ping method that can be called to achieve the same
>>> result
>>>
>>> Regards,
>>>
>>> Mikhail Veygman
>>>
>>> Sep 17, 2009, в 3:00 PM, Thomas Fjellstrom <tfjellstrom at shaw.ca>
>>>
>>> написал(а):
>>>> 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.htm
>>>>> l
>>>>>
>>>>> http://lists.trolltech.com/pipermail/qt-interest/2009-September/012347.
>>>>> html
>>>> Partially valid. But lastError() should actually say there's an
>>>> error if
>>>> exec() fails, no?
>>> _______________________________________________
>>> Qt-interest mailing list
>>> Qt-interest at trolltech.com
>>> http://lists.trolltech.com/mailman/listinfo/qt-interest
>> _______________________________________________
>> Qt-interest mailing list
>> Qt-interest at trolltech.com
>> http://lists.trolltech.com/mailman/listinfo/qt-interest
>>
> 
> 



More information about the Qt-interest-old mailing list