How to set up datasource with Spring for HikariCP?

匿名 (未验证) 提交于 2019-12-03 02:47:02

问题:

Hi I'm trying to use HikariCP with Spring for connection pool. I'm using jdbcTempLate and JdbcdaoSupport.
This is my spring configuration file for datasource:

But unfortunately the following error message is generating:

Cannot resolve reference to bean 'dataSource' while setting bean property 'dataSource'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in ServletContext resource [/WEB-INF/dispatcher-servlet.xml]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [com.zaxxer.hikari.HikariDataSource]: No default constructor found; nested exception is java.lang.NoSuchMethodException: com.zaxxer.hikari.HikariDataSource.() 

Can anyone please tell me how to solve this issue?

回答1:

you need to write this structure on your bean configuration (this is your datasource):

${dataSource.url}${dataSource.username}${dataSource.password}

This is my example and it is working. You just need to put your properties on hibernate.properties and set it before:

classpath:hibernate.properties

Obs.: the versions are
log4j: 1.2.16
springframework: 3.1.4.RELEASE
HikariCP: 1.4.0

Properties file (hibernate.properties):

hibernate.dataSourceClassName=oracle.jdbc.pool.OracleDataSource hibernate.hikari.maximumPoolSize=10 hibernate.hikari.idleTimeout=30000 dataSource.url=jdbc:oracle:thin:@localhost:1521:xe dataSource.username=admin dataSource.password= 


回答2:

my test java config (for MySql)

@Bean(destroyMethod = "close") public DataSource dataSource(){     HikariConfig hikariConfig = new HikariConfig();     hikariConfig.setDriverClassName("com.mysql.jdbc.Driver");     hikariConfig.setJdbcUrl("jdbc:mysql://localhost:3306/spring-test");      hikariConfig.setUsername("root");     hikariConfig.setPassword("admin");      hikariConfig.setMaximumPoolSize(5);     hikariConfig.setConnectionTestQuery("SELECT 1");     hikariConfig.setPoolName("springHikariCP");      hikariConfig.addDataSourceProperty("dataSource.cachePrepStmts", "true");     hikariConfig.addDataSourceProperty("dataSource.prepStmtCacheSize", "250");     hikariConfig.addDataSourceProperty("dataSource.prepStmtCacheSqlLimit", "2048");     hikariConfig.addDataSourceProperty("dataSource.useServerPrepStmts", "true");      HikariDataSource dataSource = new HikariDataSource(hikariConfig);      return dataSource; } 


回答3:

Using XML configuration, your data source should look something like this:

    jdbc:oracle:thin:@localhost:1521:XEusernamepassword

Or you could skip the HikariConfig bean altogether and use an approach like the one mentioned here



回答4:

This last error is caused by the library SLF4J not being found. HikariCP has two dependencies: slf4j and javassist. BTW, HikariDataSource does have a default constructor and does not need HikariConfig, see this link. So that was never the problem.



回答5:

You can create a datasource bean in servlet context as:

 jdbc:mysql://localhost:3306/exampledbroot2502048truetrue

You can find a complete CRUD example written in Spring 4, Hibernate JPA and HikariCP here in this link: http://frameworkonly.com/hikaricp-connection-pooling-in-spring-hibernate-jpa/



回答6:

I have recently migrated from C3P0 to HikariCP in a Spring and Hibernate based project and it was not as easy as I had imagined and here I am sharing my findings.

For Spring Boot see my answer here

I have the following setup

  • Spring 4.3.8+
  • Hiberante 4.3.8+
  • Gradle 2.x
  • PostgreSQL 9.5

Some of the below configs are similar to some of the answers above but, there are differences.

Gradle stuff

In order to pull in the right jars, I needed to pull in the following jars

//latest driver because *brettw* see https://github.com/pgjdbc/pgjdbc/pull/849 compile 'org.postgresql:postgresql:42.2.0' compile('com.zaxxer:HikariCP:2.7.6') {     //they are pulled in separately elsewhere     exclude group: 'org.hibernate', module: 'hibernate-core' }  // Recommended to use HikariCPConnectionProvider by Hibernate in 4.3.6+     compile('org.hibernate:hibernate-hikaricp:4.3.8.Final') {         //they are pulled in separately elsewhere, to avoid version conflicts         exclude group: 'org.hibernate', module: 'hibernate-core'         exclude group: 'com.zaxxer', module: 'HikariCP' }  // Needed for HikariCP logging if you use log4j compile('org.slf4j:slf4j-simple:1.7.25')   compile('org.slf4j:slf4j-log4j12:1.7.25') {     //log4j pulled in separately, exclude to avoid version conflict     exclude group: 'log4j', module: 'log4j' } 

Spring/Hibernate based configs

In order to get Spring & Hibernate to make use of Hikari Connection pool, you need to define the HikariDataSource and feed it into sessionFactory bean as shown below.

 localhost5432dbnamedbuserdbpasswordorg.hibernate.hikaricp.internal.HikariCPConnectionProvider

Once the above are setup then, you need to add an entry to your log4j or logback and set the level to DEBUG to see Hikari Connection Pool start up.

Log4j1.2

 

Logback

Via application.properties in Spring Boot

debug=true logging.level.com.zaxxer.hikari.HikariConfig=DEBUG  

Using logback.xml

With the above you should be all good to go! Obviously you need to customize the HikariCP pool configs in order to get the performance that it promises.



回答7:

I found it in http://www.baeldung.com/hikaricp and it works.

Your pom.xml

com.zaxxerHikariCP2.6.3

Your data.xml

Your jdbc.properties

jdbc.driverClassName=org.postgresql.Driver jdbc.dialect=org.hibernate.dialect.PostgreSQL94Dialect jdbc.databaseurl=jdbc:postgresql://localhost:5432/dev_db jdbc.username=dev jdbc.password=dev 


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!