14

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;
} 
brettw
  • 10,664
  • 2
  • 42
  • 59
Abhinab Kanrar
  • 1,532
  • 2
  • 20
  • 46
  • No code, no links to what you have tried. Please add sufficient information. – M. Deinum May 13 '14 at 11:39
  • 1
    By the way, ``HikariDataSource`` does not need to be configured with a constructor. ``HikariDataSource`` extends ``HikariConfig``, so you can just construct a ``HikariDataSource`` and configure the properties on it directly. – brettw May 14 '14 at 07:24

2 Answers2

28

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.

brettw
  • 10,664
  • 2
  • 42
  • 59
  • Thanks for your suggestion. But its not working.It's quite strange that it's not even throwing any exception when the database is accessed.I even tried to put try-catch block around the datasource configuration code portion. Still no exception is thrown. My driver is "oracle.jdbc.driver.OracleDriver" and url is "jdbc:oracle:thin:@localhost:1521:XE". Please help me in solving the problem – Abhinab Kanrar May 13 '14 at 16:10
  • When you say it is not working, what exactly is not working? You say that "it's not even throwing any exception when the database is accessed" -- that makes it sound like it *is* working. You can stop using DataSource-based configuration completely, and use ``config.setDriverClassName("oracle.jdbc.driver.OracleDriver")`` and ``config.setJdbcUrl("jdbc:oracle:thin:@localhost:1521:XE")``. But it sounds like the issue is somewhere else in your configuration. – brettw May 14 '14 at 04:00
  • 1
    But the same configuration is working fine when I'm using BoneCP or C3P0 or Spring DriverManagerDataSource. Every jar is there which are meeded. So it's strange that it's not working with only HikariCP. If anything goes wrong, then it should throw at least some exception/s. But it's not even throwing any exception. I'm eager to use HikariCP in this project. Please help me. – Abhinab Kanrar May 14 '14 at 05:31
  • You still haven't pasted any code, so we really don't know what your configuration attempt looks like. Can you update your question with some code? There are thousands of users using Spring+HikariCP, so it must be something with how you are configuring things. – brettw May 14 '14 at 07:01
  • 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; } – Abhinab Kanrar May 14 '14 at 07:07
  • Thanks a lot brettw. Finally it works. All along the time, I did do this stupid mistake :P Again thanks a ton brettw. – Abhinab Kanrar May 14 '14 at 15:26
  • What was the mistake? – gkiko Sep 26 '14 at 14:33
  • Configuration problem: @Bean method 'dataSource' must not be private or final; change the method's modifiers to continue – jarosik Nov 29 '16 at 11:42
  • Depending on your ojdbc you will just have to switch property "port" to "portNumber", I'm preety sure that above 11 it's required – Pedro Caires Oct 27 '21 at 21:45
5

Something like the following should fit your needs:

@Bean
public DataSource dataSource() {
     HikariConfig config = new HikariConfig();
     config.setMaximumPoolSize(100);
     config.setDataSourceClassName("oracle.jdbc.pool.OracleDataSource");
     config.addDataSourceProperty("serverName", "localhost");
     config.addDataSourceProperty("port", "1521");
     config.addDataSourceProperty("databaseName", "XE");
     config.addDataSourceProperty("user", "yourUser");
     config.addDataSourceProperty("password", "yourPassword");

     return new HikariDataSource(config);
}
geoand
  • 60,071
  • 24
  • 172
  • 190