如何在Spring @Configuration类中使用HikariCP配置数据源? [英] How to configure datasource with HikariCP in Spring @Configuration class?

查看:239
本文介绍了如何在Spring @Configuration类中使用HikariCP配置数据源?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

我在互联网上搜索后发现,HikariCP数据源需要使用构造函数进行配置.我已经尝试了这种方法(在他们的github网页中提到了它的方式),但是仍然无法正常工作.请帮助我解决这个问题.

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

推荐答案

您可以在以下Wiki中查看我们的示例:

You can check out our example in the wiki here:

https://github.com/brettwooldridge/HikariCP/wiki/带有注释的Spring-Hibernate

如本文所述:

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

上面提供的代码不正确.您正在尝试将 MySQL DataSource属性用于 Oracle DataSource.现在,您将基于Driver的配置与基于DataSource的配置混合在一起.简化它:

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:

驱动程序:

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

数据源:

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

此外,对于您来说, 100 连接对于Oracle来说是一个大方法,除非您每秒运行20K事务, 10-20 更合理.

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

这篇关于如何在Spring @Configuration类中使用HikariCP配置数据源?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆