MySQL Forums
Forum List  »  Connector/C++

con-->close() still leaves thread connected
Posted by: Simon Allen
Date: October 17, 2011 05:13AM

* Under normal circumstances issuing con->close() disconnects the connected thread from the DB and this is clearly visible by doing: show global status where variable_name = 'Threads_connected'; from the mysql prompt.

* However it seems in the case where an exception is caught and a con->close() is issued, the thread remains connected. I am writing a daemon which will disconnect and then re-connect if a DB problem is encountered, but with this apparent bug, the DB quickly becomes flooded with connected threads.

* Anyone suggest a solution or I am doing something wrong? Below is code which illustrates the problem by running sql that will fail (select 'output' from ddual). If ddual is changed to dual, then everything runs fine and the "threads_connected" is decreased immedately. Using "ddual", the "threads_connected" is not decreased immediately.

* Thanks for your help

#include <stdlib.h>
#include <iostream>

#include "mysql_connection.h"
#include "mysql_driver.h"

//#include <cppconn/driver.h>
#include <cppconn/exception.h>
#include <cppconn/resultset.h>
#include <cppconn/statement.h>


using namespace std;
using namespace sql::mysql;


// ---------------------------------------------------------------------------
// main()
// ---------------------------------------------------------------------------
int main(int argc, char* argv[])
{
sql::Driver *driver;
sql::Connection *con;
sql::Statement *stmt;
sql::ResultSet *res;
sql::PreparedStatement *pstmt;

// --- create a connection ---
driver = sql::mysql::get_driver_instance();
con = driver->connect("localhost", "root", "root");


// --- connect to db ---
con->setSchema("code");

// --- launch sql that will fail ---
con->setSchema("code");
try
{
sql::Statement *stmt = NULL;
stmt = con->createStatement();
res = stmt->executeQuery("select 'output' from ddual;");

// --- consume any extra results sets that are present ---
while (stmt->getMoreResults());

if(stmt)
delete stmt;

}
catch (sql::SQLException &e)
{
cout << "# ERR: SQLException in " << __FILE__;
cout << "(" << __FUNCTION__ << ") on line " << __LINE__ << endl;
cout << "# ERR: " << e.what();
cout << " (MySQL error code: " << e.getErrorCode();
cout << ", SQLState: " << e.getSQLState() << " )" << endl;

/*
if(stmt)
delete stmt;
*/
}

cout << "Closing connection to MySQL..." << endl;
con->close();

if (con->isClosed())
printf("DB is closed...\n");
else
printf("DB is open...\n");

delete con;

/* --- while sleep is running: show open sessions by executing sql:
show global status where variable_name = 'Threads_connected';
*/
sleep(15);
}

Options: ReplyQuote


Subject
Views
Written By
Posted
con-->close() still leaves thread connected
3035
October 17, 2011 05:13AM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.