Hikaricp: Getting error while including HikariCP as a JNDI DataSource Factory on Tomcat.

Created on 7 Jun 2016  路  5Comments  路  Source: brettwooldridge/HikariCP

I am trying to setup Hikari as a JNDI DataSource Factory on my tomcat 7. But Not sure that i have to add Hikari.jar inside tomcat lib or simply adding as maven dependency for using it as a JNDI
DataSource.

Anyway, Here are following steps which i did so far.

  1. Added Maven dependency into my POM.xml

<dependency> <groupId>com.zaxxer</groupId> <artifactId>HikariCP</artifactId> <version>2.4.6</version> </dependency>

  1. Added following resource inside context.xml
    factory="com.zaxxer.hikari.HikariJNDIFactory"
    type="javax.sql.DataSource"
    minimumIdle="5"
    maximumPoolSize="10"
    connectionTimeout="300000"
    dataSourceClassName="com.mysql.jdbc.jdbc2.optional.MysqlDataSource"
    dataSource.user="root"
    dataSource.password=""
    dataSource.url="jdbc:mysql://localhost:3306/coesi?useUnicode=true&characterEncoding=utf-8"/>
  2. Added a Bean inside my persistance Config

@Bean
public DataSource dataSource() {
Context ctx;
try {
ctx = new InitialContext();
return (DataSource) ctx.lookup("java:comp/env/jdbc/MysqlHikari");
} catch (NamingException e) {
LOGGER.info("Error loockup to the database : \"java:comp/env/jdbc/MysqlHikari\".");
}
return null;
}

I am getting this following error "DataSource must not be null".

Full Stack

Caused by: org.springframework.beans.factory.BeanDefinitionStoreException: Factory method [public org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean com.ftprod.fappi.gc.config.PersistanceConfig.entityManagerFactoryBean()] threw exception; nested exception is java.lang.IllegalArgumentException: DataSource must not be null
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:188)
at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:586)
... 83 more
Caused by: java.lang.IllegalArgumentException: DataSource must not be null
at org.springframework.util.Assert.notNull(Assert.java:112)
at org.springframework.jdbc.datasource.lookup.SingleDataSourceLookup.(SingleDataSourceLookup.java:40)
at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.setDataSource(LocalContainerEntityManagerFactoryBean.java:231)
at com.ftprod.fappi.gc.config.PersistanceConfig.entityManagerFactoryBean(PersistanceConfig.java:59)
at com.ftprod.fappi.gc.config.PersistanceConfig$$EnhancerBySpringCGLIB$$238d1829.CGLIB$entityManagerFactoryBean$1()
at com.ftprod.fappi.gc.config.PersistanceConfig$$EnhancerBySpringCGLIB$$238d1829$$FastClassBySpringCGLIB$$f799cb05.invoke()
at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:228)
at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:312)
at com.ftprod.fappi.gc.config.PersistanceConfig$$EnhancerBySpringCGLIB$$238d1829.entityManagerFactoryBean()
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:166)
... 84 more

not-a-bug question

All 5 comments

error message is very clear. you are passing 'driver' class name instead of 'dataSource' class name ! change it to: dataSourceClassName=com.mysql.jdbc.jdbc2.optional.MysqlDataSource

I already added this line dataSourceClassName="com.mysql.jdbc.jdbc2.optional.MysqlDataSource" but still getting this "DataSource must not be null" error.

I have it like this and it's working. Make sure Hikari jar file is in tomcat lib folder.

<Resource name="jdbc/datasource" 
        auth="Container" 
        factory="com.zaxxer.hikari.HikariJNDIFactory" 
        type="javax.sql.DataSource" 
        maximumPoolSize="10"
        connectionTimeout="300000" 
        dataSourceClassName="com.mysql.jdbc.jdbc2.optional.MysqlDataSource"
        dataSource.url="jdbc:mysql://server/database"
        dataSource.user="user" 
        dataSource.password="pwd" 
/>

@wesoos : your suggested code doesnt work to me..

@coexia It is 'specific' configuration issue, you may get answer on stack overflow instead.

Was this page helpful?
0 / 5 - 0 ratings