Can't create more than max_prepared_stmt_count statements

使用mysql connector的时候,如果报这个错误

Can't create more than max_prepared_stmt_count statements (current value: 16382)

是因为下面的写法有一定问题,如果catch了错误,那么stmt就不会释放,最好是把stmt放到外面,在最后的时候delete,但是一般不会出问题,关键是如果写了bug,导致不断运行catch错误,就会导致preparedstatement大量不会释放,超出默认的16382的个数

try
{
	sql::PreparedStatement * stmt = con->prepareStatement(sqlstr);

	stmt->setInt(1, id1);
	stmt->setInt(2, id2);
	stmt->execute();
	delete stmt;
}
catch (sql::SQLException & e)
{
	bsqlcon = false;
	Log_Text_Format(LOGLEVEL_ERROR, "Err: SQLException in %s %s %d ERR[%s] ErrCode[%d] SQLState[%s] sql[%s]", __FILE__, __FUNCTION__, __LINE__, e.what(), e.getErrorCode(), e.getSQLState().c_str(), sqlstr.c_str());
}

 可以通过下面的语句查看目前申请了几个,使用了几个,释放了几个,正常情况申请的和释放的应该是很接近在几十或是上百个以内,并且固定不变不会逐渐增加。不过申请释放的这个值,如果直接关闭程序,是不会改变的,还是维持最后一次的记录

show global status like 'com_stmt%';

Com_stmt_execute    563281
Com_stmt_close    563280
Com_stmt_fetch    0
Com_stmt_prepare    563359
Com_stmt_reset    0
Com_stmt_send_long_data    0
Com_stmt_reprepare    0

猜你喜欢

转载自www.cnblogs.com/studywithallofyou/p/12244638.html