List:MySQL++« Previous MessageNext Message »
From:Jim Wallace Date:February 17 2009 7:11pm
Subject:RE: Newbie connection question
View as plain text  
By permanent, you mean forever connected?  Even if you connect, you're likely to be
disconnected by your server if you remain idle.  You must always be prepared to retry on
the event of a lost connection.

Our app connects and stays connected, but handles reconnects with some simple macros and
using the ReconnectOption(true) on the connection object.  For example:

BEGIN_TRY_SQL

	...do a bunch of MySQL++

END_TRY_SQL("doingSomething")


The macros are actually for loops that will retry on connection loss.  Here's the macros I
use which make some assumptions about access to m_conn and a logger, but you get the idea.



#define RETRY_LIMIT 2
#define BEGIN_TRY_SQL int __sqlTries__ = 0; for ( ; __sqlTries__ < RETRY_LIMIT;
__sqlTries__ ++ ) try {

#define END_TRY_SQL( functionName )\
		break; \
	}\
	catch (const mysqlpp::BadQuery& er) \
	{\
		int sqlErr = m_conn->errnum();\
		if ( sqlErr != CR_SERVER_LOST ) {\
			kstring s = formatBadQuery(  functionName, er.what(), er.query_string(),
m_conn->server(), m_conn->port(), m_conn->db(), m_conn->user()  ); \
			LOG4CPLUS_WARN( alert_logger, s ); \
			__sqlTries__ = 100; \
			throw new KEPException( s.c_str(), er.errnum() );\
		} else {\
			LOG4CPLUS_INFO( alert_logger, "Lost connection in " << functionName << "
retrying since got " << sqlErr << " " << er.what() ); }\
	}\
	catch (const mysqlpp::BadConversion& er) \
	{\
		kstring s = loadStrPrintf( IDS_DB_CONVERSION_ERROR, functionName, er.what(),
er.retrieved, er.actual_size ); \
		LOG4CPLUS_WARN( alert_logger, s ); \
		__sqlTries__ = 100; \
		throw new KEPException( s.c_str(), m_conn->errnum() != 0 ? m_conn->errnum() :
E_INVALIDARG );\
	}\
	catch (const mysqlpp::Exception& er) \
	{\
		kstring s = loadStrPrintf( IDS_DB_ERROR, functionName, er.what() ); \
		LOG4CPLUS_WARN( alert_logger, s ); \
		throw new KEPException( s.c_str(), m_conn->errnum() != 0 ? m_conn->errnum() :
E_INVALIDARG );\
	}\
	if ( __sqlTries__ >= RETRY_LIMIT ) {\
		LOG4CPLUS_INFO( alert_logger, "Retry limit hit for " << functionName ); \
	}
-----Original Message-----
From: Sebastián Salazar Molina. [mailto:ssalazar@stripped] 
Sent: Tuesday, February 17, 2009 1:52 PM
To: plusplus@stripped
Subject: Newbie connection question

Hi All.

I'd Would like to know how to create a permanent connection using mysql++, I 
searched in google and have not found anything satisfactory.

Thanks

-- 
MySQL++ Mailing List
For list archives: http://lists.mysql.com/plusplus


Thread
Newbie connection questionSebastián Salazar Molina.17 Feb
  • RE: Newbie connection questionJim Wallace17 Feb
    • Re: Newbie connection questionSebastián Salazar Molina.17 Feb
      • Re: Newbie connection questionRick Gutleber17 Feb
      • Re: Newbie connection questionWarren Young17 Feb
        • Re: Newbie connection questionSebastián Salazar Molina.18 Feb