Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to configure datasource with HikariCP in Spring @Configuration class?

I'm trying to configure HikariCP datasource in Spring @Configuration class[Database being oracle]. But it's not working.

I searched in the internet and found that HikariCP datasource needs to be configured with constructor. I have tried this [the way it's mentioned in their github webpage], but it still not working. Please help me in solving this problem.

private HikariDataSource dataSource() {
    final HikariDataSource ds = new HikariDataSource();
    ds.setMaximumPoolSize(100); 
    ds.setDataSourceClassName("oracle.jdbc.driver.OracleDriver"); 
    ds.addDataSourceProperty("url", "jdbc:oracle:thin:@localhost:1521:XE"); 
    ds.addDataSourceProperty("user", "username");
    ds.addDataSourceProperty("password", "password");
    ds.addDataSourceProperty("cachePrepStmts", true); 
    ds.addDataSourceProperty("prepStmtCacheSize", 250); 
    ds.addDataSourceProperty("prepStmtCacheSqlLimit", 2048); 
    ds.addDataSourceProperty("useServerPrepStmts", true);
    return ds;
} 
like image 240
Abhinab Kanrar Avatar asked May 13 '14 11:05

Abhinab Kanrar


People also ask

How do you configure a DataSource in spring?

To configure your own DataSource , define a @Bean of that type in your configuration. Spring Boot reuses your DataSource anywhere one is required, including database initialization. If you need to externalize some settings, you can bind your DataSource to the environment (see “Section 25.8.

What is spring DataSource Hikari Max lifetime?

spring.datasource.hikari.max-lifetime=1800000. Controls the maximum lifetime of a connection in the pool. An in-use connection will never be retired, only when it is closed it will be removed.


1 Answers

You can check out our example in the wiki here:

https://github.com/brettwooldridge/HikariCP/wiki/Spring-Hibernate-with-Annotations

As covered by this article:

http://www.3riverdev.com/blog/tutorial-spring-hibernate-hikaricp/

EDIT: The code provided above is incorrect. You are trying to use MySQL DataSource properties for an Oracle DataSource. And now you're mixing up a Driver-based configuration with a DataSource-based one. Simplify it:

Driver:

private HikariDataSource dataSource() {
   final HikariDataSource ds = new HikariDataSource();
   ds.setMaximumPoolSize(100);
   ds.setDriverClassName("oracle.jdbc.driver.OracleDriver"); 
   ds.setJdbcUrl("jdbc:oracle:thin:@localhost:1521:XE"); ;
   ds.setUsername("username");
   ds.setPassword("password");
   return ds;
}

OR DataSource:

private HikariDataSource dataSource() {
   final HikariDataSource ds = new HikariDataSource();
   ds.setMaximumPoolSize(100);
   ds.setDataSourceClassName("oracle.jdbc.pool.OracleDataSource");
   ds.addDataSourceProperty("serverName", "yourServer");
   ds.addDataSourceProperty("port", "1521");
   ds.addDataSourceProperty("databaseName", "XE");
   ds.addDataSourceProperty("user", "username");
   ds.addDataSourceProperty("password", "password");
   return ds;
}

Also, 100 connection is way to big for Oracle unless you are running 20K transactions per-second, 10-20 is more reasonable.

like image 59
brettw Avatar answered Sep 22 '22 21:09

brettw