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

前端 未结 2 1801
隐瞒了意图╮
隐瞒了意图╮ 2020-12-29 08:52

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

相关标签:
2条回答
  • 2020-12-29 09:16

    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);
    }
    
    0 讨论(0)
  • 2020-12-29 09:25

    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.

    0 讨论(0)
提交回复
热议问题