Bug #54817 #error 1040 Too many |Can not disconnect mysql connection manual use Connect/c++
Submitted: 25 Jun 2010 20:53 Modified: 26 Jun 2010 8:00
Reporter: sonly strong Email Updates:
Status: Not a Bug Impact on me:
None 
Category:Connector / C++ Severity:S3 (Non-critical)
Version: OS:Windows (VS2008)
Assigned to: CPU Architecture:Any

[25 Jun 2010 20:53] sonly strong
Description:
I want to Insert mass data into Mysql by multi-thread in Windows. 
It shows #error 1040, Too many connections 

I set the 
max_connections = 1000 in my.ini 

after run 1000 thead, insert 1000 data into mysql, I cannot insert more. 
and I use "netstat -an" check the port stats. 
show a lots of 
TCP 127.0.0.1:3306 127.0.0.1:1160 ESTABLISHED 
TCP 127.0.0.1:3306 127.0.0.1:1163 ESTABLISHED 
TCP 127.0.0.1:3306 127.0.0.1:1166 ESTABLISHED 
TCP 127.0.0.1:3306 127.0.0.1:1169 ESTABLISHED 
TCP 127.0.0.1:3306 127.0.0.1:1172 ESTABLISHED 
TCP 127.0.0.1:3306 127.0.0.1:1175 ESTABLISHED 
TCP 127.0.0.1:3306 127.0.0.1:1178 ESTABLISHED 
TCP 127.0.0.1:3306 127.0.0.1:1181 ESTABLISHED 
TCP 127.0.0.1:3306 127.0.0.1:1184 ESTABLISHED 

and 

TCP 127.0.0.1:4879 127.0.0.1:3306 TIME_WAIT 
TCP 127.0.0.1:4882 127.0.0.1:3306 TIME_WAIT 
TCP 127.0.0.1:4887 127.0.0.1:3306 TIME_WAIT 
TCP 127.0.0.1:4890 127.0.0.1:3306 TIME_WAIT 
TCP 127.0.0.1:4893 127.0.0.1:3306 TIME_WAIT 
TCP 127.0.0.1:4898 127.0.0.1:3306 TIME_WAIT 
TCP 127.0.0.1:4901 127.0.0.1:3306 TIME_WAIT 
TCP 127.0.0.1:4905 127.0.0.1:3306 TIME_WAIT 

I need to disconnect immediately when I finished the thead. How can I do? 
If I use the c API, Can use the mysql_close(&mysql); 
But I dont know how to use the Connect/c++ to close the connection 

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

/* Create a connection */ 
driver = get_driver_instance(); 
con = driver->connect("tcp://127.0.0.1:3306", "root", "root"); 
/* Connect to the MySQL test database */ 
con->setSchema("test"); 

stmt = con->createStatement(); 
res = stmt->executeQuery("insert data sql here............."); 
} 
delete res; 
delete stmt; 
con->close(); 
delete con; 

} 
catch (sql::SQLException &e) 
{ 
; 
}

How to repeat:
try 
{ 
sql::Driver *driver; 
sql::Connection *con; 
sql::Statement *stmt; 
sql::ResultSet *res; 

/* Create a connection */ 
driver = get_driver_instance(); 
con = driver->connect("tcp://127.0.0.1:3306", "root", "root"); 
/* Connect to the MySQL test database */ 
con->setSchema("test"); 

stmt = con->createStatement(); 
res = stmt->executeQuery("insert data sql here............."); 
} 
delete res; 
delete stmt; 
con->close(); 
delete con; 

} 
catch (sql::SQLException &e) 
{ 
; 
}

run the code above in multi-thread
[26 Jun 2010 8:00] Sveta Smirnova
Thank you for the report.

I can not repeat described behavior in multiple-threaded program. So this either something wrong in your program or Windows just does not free resources as quickly as you need. Search for TIME_WAIT to see explanation why.