0 down vote favorite
I have multithreading application client to MySQL and I use MySQL C-client (libmysqlclient_r). I have db connections pool, where I open connection before create thread workers (pthread_create).
The each worker gets only single connection from connections pool before starting the work and puts it to the pool after finishing work. The each worker use it's unique connection.
But, database server is very overload, and MySQL client have errors: MySQL " Lost connection to MySQL server during query" or " MySQL server has gone away". My application make reconnect in the worker thread:
my_bool res = mysql_ping(c->mysql);
if (res) {
mysql_close(c->mysql);
mysql_thread_end();
c->mysql = mysql_init(NULL);
mysql_thread_init();
struct conn_desc *cd = &c->db->cds[c->num];
syslog(LOG_ERR, "reconnect :[%s:%d]\t%s\tnew MySQL=%X tid=%X\n", cd->host, cd->port, c->db->default_db_name, c->mysql, pthread_self());
res = mysql_real_connect(c->mysql, cd->host, cd->login, cd->passwd, c->db->default_db_name, cd->port, NULL, 0);
if (res == NULL) {
syslog(LOG_ERR, "[restart ] reconnect Error\n");
exit(1);
}
}
Sometime, I have sigmentation fault into mysql_ping() or mysql_real_connect(). Why? I use the separate mysql-connections between workers threads. What is wrong? How is making the Right?