After reading the docs it's still unclear to me what the correct usage is. Should one:
- Call
mysql_init()
andmysql_real_connect()
once at startup, before thread creation, and all threads share theMYSQL
handle returned frommysql_real_connect()
. - Call
mysql_init()
once at startup, before thread creation, and then each thread callsmysql_real_connect()
to get a thread specificMYSQL
handle. - Each thread calls
mysql_init()
andmysql_real_connect()
. - Something else?
CodePudding user response:
Sharing a connection over multiple threads is not a good idea, since you have to handle all kind of problems in your application: Transaction safety, protocol buffers etc.
mysql_init()
API function initializes a connection handle (MYSQL
) which will be used to connect to a server viamysql_real_connect()
. Trying to connect with the same handle again will result in an error (CR_ALREADY_CONNECTED=2058).Using one connection per thread is the better solution. If threads don't use the connection all the time, use a connection pool (e.g. by using Connector/C ).