Reputation: 11
I'm trying to create a global connection pool for tomcat with HikariCP, but somehow Hikari creates new Pool for every webapp and even when redeploying applications. Furthermore this leads in many not closed connection pools.
Here are my configurations on Tomcat:
tomcat/conf/server.xml
<GlobalNamingResources>
<Resource name="jdbc/Pool" global="jdbc/Pool" auth="Container"
factory="com.zaxxer.hikari.HikariJNDIFactory"
type="javax.sql.DataSource"
singleton="true"
minimumIdle="5"
maximumPoolSize="30"
connectionTimeout="300000"
dataSourceClassName="oracle.jdbc.pool.OracleDataSource"
dataSource.implicitCachingEnabled="true"
dataSource.user="user"
dataSource.password="pw"
dataSource.url="url" />
</GlobalNamingResources>
tomcat/conf/context.xml
<ResourceLink name="jdbc/Pool"
global="jdbc/Pool"
auth="Container"
type="javax.sql.DataSource" />
In my java code I'm getting the DataSource like this:
HikariConfig config = new HikariConfig();
Context initContext = new InitialContext();
DataSource dataSource = (DataSource)
initContext.lookup("java:comp/env/jdbc/Pool");
config.setDataSource(dataSource);
config.setRegisterMbeans(true);
HikariDataSource ds = new HikariDataSource(config);
I'm actually not getting any errors or stuff, but inside jconsole I can see, that Hikari is creating connection pool for every webapp.
Anybody knows, what could be possibly wrong here?
Thanks in advance!
Upvotes: 1
Views: 1568
Reputation: 58822
You need to define singleton=true in factory definition
If you want to share a pool across WARs, from my reading of the Tomcat docs, what you need is the singleton="true" attribute.
factory="com.zaxxer.hikari.HikariJNDIFactory"
singleton="true"
Upvotes: 1