如何在 Spring @Configuration 类中使用 HikariCP 配置数据源?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23630123/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 06:44:07  来源:igfitidea点击:

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

springspring-mvcjdbchikaricp

提问by Abhinab Kanrar

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

我正在尝试在 Spring @Configuration 类中配置 HikariCP 数据源[数据库是 oracle]。但它不起作用。

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.

上网查了一下,发现HikariCP数据源需要配置constructor。我已经尝试过[在他们的 github 网页中提到的方式],但它仍然无法正常工作。请帮我解决这个问题。

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;
} 

回答by brettw

You can check out our example in the wiki here:

您可以在此处的 wiki 中查看我们的示例:

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

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

As covered by this article:

如本文所述:

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

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

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

编辑:上面提供的代码不正确。您正在尝试将MySQLDataSource属性用于OracleDataSource。现在您将Driver基于 - 的配置与DataSource基于 -的配置混合在一起。简化一下:

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;
}

ORDataSource:

数据源:

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, 100connection is way to big for Oracle unless you are running 20K transactions per-second, 10-20is more reasonable.

此外,除非您每秒运行 20K 事务,否则100 个连接对 Oracle 来说太大了,10-20 个更合理。

回答by geoand

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);
}