Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access MySQL from multiple threads concurrently

Tags:

We're doing a small benchmark of MySQL where we want to see how it performs for our data.

Part of that test is to see how it works when multiple concurrent threads hammers the server with various queries.

The MySQL documentation (5.0) isn't really clear about multi threaded clients. I should point out that I do link against the thread safe library (libmysqlclient_r.so)

I'm using prepared statements and do both read (SELECT) and write (UPDATE, INSERT, DELETE).

  • Should I open one connection per thread? And if so: how do I even do this.. it seems mysql_real_connect() returns the original DB handle which I got when I called mysql_init())
  • If not: how do I make sure results and methods such as mysql_affected_rows returns the correct value instead of colliding with other thread's calls (mutex/locks could work, but it feels wrong)
like image 729
Isak Savo Avatar asked Sep 21 '09 15:09

Isak Savo


People also ask

Can MySQL handle concurrent requests?

You may use one buffer table (Temporary table) in the sense of concurrency control with the help of LOCK'ing mechanism of MySQL. So while one request's to the server on the priority base you can set remains request as in queue or in the sense restrict the table.

Can multiple threads use the same DB connection?

No. Of course not. Each thread needs its own connection. "Allocation of one database connection per thread will cause a large overhead" probably a lot less overhead than you think, and not as much troubles as trying to share a connection concurrently.

How many concurrent connections can MySQL handle?

Simultaneous MySQL connection limits Each database user is limited to 38 simultaneous MySQL connections. This limitation helps to prevent overloading the MySQL server to the detriment of other sites hosted on the server.

Does MySQL support multi threading?

MySQL is fully multithreaded, and makes use of all CPUs made available to it. Not all CPUs may be available; modern operating systems should be able to utilize all underlying CPUs, but also make it possible to restrict a process to a specific CPU or sets of CPUs.


2 Answers

As maintainer of a fairly large C application that makes MySQL calls from multiple threads, I can say I've had no problems with simply making a new connection in each thread. Some caveats that I've come across:

  • Edit: it seems this bullet only applies to versions < 5.5; see this page for your appropriate version: Like you say you're already doing, link against libmysqlclient_r.
  • Call mysql_library_init() (once, from main()). Read the docs about use in multithreaded environments to see why it's necessary.
  • Make a new MYSQL structure using mysql_init() in each thread. This has the side effect of calling mysql_thread_init() for you. mysql_real_connect() as usual inside each thread, with its thread-specific MYSQL struct.
  • If you're creating/destroying lots of threads, you'll want to use mysql_thread_end() at the end of each thread (and mysql_library_end() at the end of main()). It's good practice anyway.

Basically, don't share MYSQL structs or anything created specific to that struct (i.e. MYSQL_STMTs) and it'll work as you expect.

This seems like less work than making a connection pool to me.

like image 122
chazomaticus Avatar answered Oct 20 '22 19:10

chazomaticus


You could create a connection pool. Each thread that needs a connection could request a free one from the pool. If there's no connection available then you either block, or grow the pool by adding a new connection to it.

There's an article here describing the pro's and cons of a connection pool (though it is java based)

Edit: Here's a SO question / answer about connection pools in C

Edit2: Here's a link to a sample Connection Pool for MySQL written in C++. (you should probably ignore the goto statements when you implement your own.)

like image 42
Glen Avatar answered Oct 20 '22 17:10

Glen