Andre
Andre

Reputation: 3181

MySQL JDBC connection pool maximum size

I'm building a socket server with Java. Each new socket connection launches a new thread which will need a connection to a MySQL server.

Currently I have a new connection for every thread but would like to change to a connection pool so that I can more effectively recycle connections.

However, the default size of this pool seems to be of 5 connections. I'm currently handling more than 100 clients and 5 connections won't be enough. All of the documentation I've found mention the different servers (Tomcat, JBoss, GlassFish, etc) and how to place the value in their XML configuration files.

Since I'm not using any of these, I can't find how to set the max pool size value. I would also like to avoid third party libraries for this project (such as Apache Commons).

An exception to that would be the MySQL connector for Java (found here: http://dev.mysql.com/usingmysql/java/)

The only possible reference I've found is env.put(JNDIPooledSource.MAX_POOL_SIZE, 500); but I don't believe that's correct.

Thanks for your help.

Upvotes: 2

Views: 5207

Answers (2)

Kyle Zhang
Kyle Zhang

Reputation: 51

For choosing connection pool library, my recommendation is

hikari > druid > UCP > c3p0 > DBCP

It's based on what I have tested, in my local test environment(4GB mac/mysql in docker/pool minSize=1, maxSize=8), hikari can serve 1024 threads x 1024 times to get connections, average time for each thread to finish is 1 or 2 million seconds, while c3p0 can only serve 256 threads x 1024 times and average time for each thread is already 21 million seconds. (512 threads failed).

Upvotes: 0

Matt
Matt

Reputation: 11805

MySQL connector/J does not come with a Pooled DataSource implementation. You will need to either use 3rd party software, or use what comes with your container. I believe tomcat uses commons-dbcp out of the box.

c3p0 is another option which has a directly constructable datasource which has pooling built in.

Upvotes: 1

Related Questions