#include <stdlib.h>
#include <iostream>
#include <sstream>
#include <stdexcept>
#include <string.h>
#include <pthread.h>
#include <stdio.h>	
#define NUM_THREADS     40


#include "mysql_connection.h"

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

using namespace std;

void *testBug(void *threadid)
{
	long tid;
	tid = (long)threadid;

	cout << endl;
	printf("testBug(), thread #%ld, begin\n", tid);

	try { 
		sql::Driver *driver;
		sql::Connection *con;
		sql::Statement *stmt;
		sql::PreparedStatement *pstmt;
		sql::ResultSet *res;

		/* Create a connection */
		driver = get_driver_instance();
		con = driver->connect("tcp://127.0.0.1:13000", "root", "");	
		
		stmt = con->createStatement(); 
		stmt->executeQuery("select 1");


		con->close(); 
		delete con;
		
		sleep(10);
		

	} 
	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;
	}
	printf("testBug(), thread #%ld, end\n", tid);
	pthread_exit(NULL);
}

int main()
{
	pthread_t threads[NUM_THREADS];
	int rc;
	long t;
	for(t=0; t<NUM_THREADS; t++){
		printf("In main: creating thread %ld\n", t);
		rc = pthread_create(&threads[t], NULL, testBug, (void *)t);
		if (rc){
			printf("ERROR; return code from pthread_create() is %d\n", rc);
			exit(-1);
		}
	}
	pthread_exit(NULL);
	return 0;
}

