MySQL – Persistent connection vs connection pooling

Having persistent connections does not imply that all threads use the same connection. It just “says” that you keep the connection open (in contradiction to open a connection each time you need one). Opening a connection is an expensive operation, so – in general – you try to avoid opening connections more often than necessary.

This is the reason why multithreaded applications often use connection pools. The pool takes care of opening and closing connections and every thread that needs a connection requests one from the pool. It is important to take care that the thread returns the connection as soon as possible to the pool, so that another thread can use it.

If your application has only a few long running threads that need connections you can also open a connection for each thread and keep this open.

Using just one connection (as you described it) is equal to a connection pool with the maximum size one. This will be sooner or later your bottleneck as all threads will have to wait for the connection. This could be an option to serialize the database operations (perform them in a certain order), although there are better options to ensure serialisation.

Leave a Comment