2
votes

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.

2
Quite obviously you are talking about java.sql.Connection objects, right? What kind of pooling mechanism or library are you using at the moment?Arne
Yes, I was attempting to use the Context and DataSource classes in javax.naming and javax.sql respectively to create a pool but can't seem to set the size of it.Andre
I'm confused: If your not using some of the servers mentioned above, where does this DataSource come from? It has to be configured there.Arne

2 Answers

1
votes

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.

0
votes

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).