How do I configure Tomcat 9 to use its default connection pool? It is slightly unclear from the documentation what the difference is between the Tomcat DBCP 2 pool and the other Tomcat JDBC pool. How to configure them? And what’s the difference? And which to use?
Both of the following approaches will use DB connection pools:
type="javax.sql.DataSource"
.factory="org.apache.tomcat.jdbc.pool.DataSourceFactory"
and type="org.apache.tomcat.jdbc.pool.DataSource"
.I don’t know enough about the differences to give any strong recommendations re. using one over the other.
To make use of the default Tomcat DB connection pool, via a JNDI resource, you should follow the instructions provided on this Tomcat documentation page: JNDI Datasource How-To
Specifically, you need to use type="javax.sql.DataSource"
.
Here is a very basic resource configuration, based on MySQL - not suitable for production, but useful to illustrate the approach:
|
|
A Java connection would then follow the usual approach for using JNDI:
|
|
Or, if preferred, you can use the @Resource
annotation from javax.annotation.Resource
, applied to a field:
|
|
When using initialSize="5"
, I saw the 5 expected connections on the DB server.
In the JVM, a connection pool was created - as shown in this VisualVM heap dump:
Here we can see that the pool is created, it has 5 connection objects, and it is using Tomcat DBCP
- which, in turn, is a fork of Apache Commons DBCP 2
.
DBCP 2 provides support for JDBC 4.1 (it is a pure-Java JDBC driver).
If you look at the Tomcat documentation on this other page - The Tomcat JDBC Connection Pool - you will see different guidance.
Specifically it states that you need both of the following in your <Resource>
:
|
|
If you use this, you will also be using a DB connection pool - but it will be based on the older Tomcat JDBC pool package:
[![enter image description here][4]][4]
In this example, I used initialSize="3"
.
In the Tomcat documentation this is presented as “a new connection pool” (it was a replacement for the previously used Commons DBCP 1.x
). It’s not as new as the Tomcat default DBCP 2 solution.
I think the documentation wording is out-of-date, now. And somewhat confusing, because of that.
Notes from a member of the Tomcat commit team (see here):
Tomcat JDBC is Tomcat’s “home grown” database connection pooling and does not use poolPreparedStatements
Tomcat DBCP is Tomcat’s package renamed fork of Apache Commons DBCP 2.
Tomcat DBCP is used by default.
You can always choose to implement a pool directly in your code, without using a JNDI <Resource>
annotation. There are many available options. I have used Hikari a few times without any issues.