Mailing List
Home
Forum Home
MySQL General - General MySQL discussion
MySQL++ - Programming with the C++ API to MySQL
MaxDB - Everything about MaxDB, formerly known as SAP DB
ODBC - ODBC with the MySQL Connector/ODBC driver
MySQL on Win32 - Runing MySQL on Windows 9x/Me/NT/2000/XP
Java Help - Mostly related to the MySQL Connector/J driver
Perl - Perl support for MySQL with DBI and DBD::mysql
GUI - MySQL GUI Tools
Announcement
Subjects
mysql openssl Question
ERROR 1045: Access denied for user: 'root@localhost ' (Using
password: NO)
Update one field with more fields from another table
Getting Identity after INSERT
ERROR 2002: Can 't connect to local MySQL server through socket
mysql test 4 1 fails with the gis test
MySQL Cluster Software
Downgrade Mysql from 4 to 3 23
Mysql 4 0 Oracle Stored Procedure Trigger Conversion
Can 't access mysql after kernel upgrade
Executing MySQL Commands From Within C Program
Comparing and writing out BLOBS
Preventing Duplicate Entries
FULLTEXT query format question
Strange behavior, Table Level Permission
Does the binary log enabling affect the MySQL performances?
mysql:it 's a db not a dbms how it 's possible?!
mysql have same function mthod as Oracle decode()
 
Stay connected with DB

Stay connected with DB

2007-06-06       - By reid.madsen@(protected)

 Back
Reply:     1     2     3     4     5     6     7     8     9     10     >>  

Warren,

I'm using a connection pool to reduce the overhead associated with
opening/closing database connections.  Often, one of the connections in
the pool times out.  Here is a snippet of code from the PooledConnection
constructor that detects connections that have timed out:

  mysqlpp::Connection * conn = getPooledConnection();
  if (conn->ping()) {
    delete conn;
    conn = new mysqlpp::Connection( ... );
     }

Is this an acceptable solution?  Anything else I should consider?
Thanks


-- --Original Message-- --
From: Warren Young [mailto:mysqlpp@(protected)]
Sent: Tuesday, June 05, 2007 12:09 PM
To: MySQL++ Mailing List
Subject: Re: Stay connected with DB

Manuel Jung wrote:
>
> It stays connected for a day or so, but always after a night (where
> probably not querys where started) the connections are lost.

This is a relatively recent change in the MySQL server defaults.  (I
believe it changed sometime in the middle of the 4.1 series.)  If the
server doesn't see a query (or a ping) on a connection for 8 hours, it
drops the connection.  This is a desirable feature, because it avoids
problems with too many idle connections building up, wasting server
resources.

If you don't like this behavior, it's better to change the wait_timeout
setting in the [mysqld] section of the my.cnf file, which controls this
behavior.  The default is 28800, or 8 hours, measured in seconds.  If
you know you will get at least one query a day, you could change it to
86400.

> Im trying to use Connection::ping for possibly reconnect, before i
> start some MySQL Work, but it doesn't help.

Of course not.  The connection is already closed by that point.

If you must use pings to work around this, you need to ping separately
from the main work loop, so the pings go out regardless of what work is
happening.

If your program just sits there doing nothing while waiting for work,
just set it to ping the server every 4 hours or so.

If instead it's an event-based system, where your code doesn't get
called until there is work to do (as in a web application) there's not
much you can do about this from the client side.  One more argument in
favor of fixing this on the server side.

> I also create a new query object after pinging.
> (Query objects are just alive for a short time in my application).

That's not going to affect this.  If you recreate the _Connection_
object each time, that would fix the problem, but you'll pay a speed
penalty.  This is why I keep coming back to fixing the server policy to
suit your situation, or if that cannot work, adding a background pinger.

> What is the preferred solution to keep connections alive or reconnect
> them automaticly?

The preferred solution is to avoid getting into a situation where you
need to do keepalives or reconnects.  Those solutions attack the
symptom.  I prefer to attack the cause, where possible.

--
MySQL++ Mailing List
For list archives: http://lists.mysql.com/plusplus
To unsubscribe:
http://lists.mysql.com/plusplus?unsub=reid.madsen@(protected)


--
MySQL++ Mailing List
For list archives: http://lists.mysql.com/plusplus
To unsubscribe:    http://lists.mysql.com/plusplus?unsub=mysql@(protected)