List:MySQL++« Previous MessageNext Message »
From:Jim Wallace Date:June 5 2007 11:05am
Subject:RE: Stay connected with DB
View as plain text  
Here are the macros I use.  If they word-wrapped too badly, let me know
and I'll send as an attachment.

I first started using CHECK_CONNECTION which does a ping.  I think this
will help keep alive, but if you are inactive for too long the server
may disconnect you, depending on the server settings.

To get around the disconnect issue, the BEGIN_TRY_SQL has a little for
loop the tries 2 times in case of failure.  This fixed our problem when
the connection was dropped by the server.  I was already using the
macros for exception handling, to I only had to make macro changes to
apply the loop.

HTH,
jmw

// helper macros
#define CHECK_CONNECTION \
	if ( m_conn == 0 || !m_conn->connected() )\
	{\
		if( m_conn ) \
			m_conn->ping(); \
		if ( m_conn == 0 || !m_conn->connected() )\
			throw new KEPException( loadStr(
IDS_DB_NOT_CONNECTED ).c_str(), m_conn->errnum()  );\
	}

// added retry logic
#define BEGIN_TRY_SQL for ( int sqlTries = 0; sqlTries < 2; sqlTries ++
) try {

#define END_TRY_SQL( functionName )\
		break; \
	}\
	catch (const mysqlpp::BadQuery& er) \
	{\
		int sqlErr = m_conn->errnum();\
		if ( sqlErr != CR_SERVER_LOST ) {\
			throw new KEPException( loadStrPrintf(
IDS_DB_QUERY_ERROR, functionName, er.what() ).c_str(), m_conn->errnum()
);\
		} else {\
			LOG4CPLUS_INFO( m_logger, "Lost connection in "
<< functionName << " retrying" ); }\
	}\
	catch (const mysqlpp::BadConversion& er) \
	{\
		throw new KEPException( loadStrPrintf(
IDS_DB_CONVERSION_ERROR, functionName, er.what(), er.retrieved,
er.actual_size ).c_str(), m_conn->errnum() );\
	}\
	catch (const mysqlpp::Exception& er) \
	{\
		throw new KEPException( loadStrPrintf( IDS_DB_ERROR,
functionName, er.what() ).c_str(), m_conn->errnum() );\
	} 

-----Original Message-----
From: Manuel Jung [mailto:gzahl@stripped] 
Sent: Tuesday, June 05, 2007 6:53 AM
To: plusplus@stripped
Subject: Stay connected with DB

Hi,

My application should stay connected with the Mysql Server or
reconnected on diconnect. It stays connected for a day or so, but always
after a night (where probably not querys where started) the connections
are lost. 
Im trying to use Connection::ping for possibly reconnect, before i start
some MySQL Work, but it doesn't help. I also create a new query object
after pinging. (Query objects are just alive for a short time in my
application).

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

Kind Regards
Manuel Jung

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

Thread
Stay connected with DBManuel Jung5 Jun
  • RE: Stay connected with DBJim Wallace5 Jun
    • RE: Stay connected with DBgary clark5 Jun
      • Re: Stay connected with DBWarren Young5 Jun
        • Re: Stay connected with DBgary clark5 Jun
  • RE: Stay connected with DBManuel Jung5 Jun
  • Re: Stay connected with DBWarren Young5 Jun
    • RE: Stay connected with DBreid.madsen5 Jun
      • Re: Stay connected with DBWarren Young5 Jun
        • Re: Stay connected with DBWarren Young5 Jun
        • RE: Stay connected with DBreid.madsen5 Jun
  • RE: Stay connected with DBManuel Jung6 Jun