Mar*_*ged 9 connection-pooling jdbctemplate ojdbc spring-boot tomcat-jdbc
我需要设置一些特定的Oracle JDBC连接属性,以加快批处理INSERTs(defaultBatchValue)和质量SELECTs(defaultRowPrefetch).我得到了如何用DBCP实现这一目标的建议(感谢M. Deinum),但我想:
我正在考虑spring.datasource.custom_connection_properties将来支持或类似的功能请求,并且由于这种尝试,这已经成为可能.我这样做是通过在创建DataSource时传递相关信息并操纵DataSource的创建,如下所示:
@Bean
public DataSource dataSource() {
DataSource ds = null;
try {
Field props = DataSourceBuilder.class.getDeclaredField("properties");
props.setAccessible(true);
DataSourceBuilder builder = DataSourceBuilder.create();
Map<String, String> properties = (Map<String, String>) props.get(builder);
properties.put("defaultRowPrefetch", "1000");
properties.put("defaultBatchValue", "1000");
ds = builder.url( "jdbc:oracle:thin:@xyz:1521:abc" ).username( "ihave" ).password( "wonttell" ).build();
properties = (Map<String, String>) props.get(builder);
log.debug("properties after: {}", properties);
} ... leaving out the catches ...
}
log.debug("We are using this datasource: {}", ds);
return ds;
}
Run Code Online (Sandbox Code Playgroud)
在日志中我可以看到我正在创建正确的DataSource:
2016-01-18 14:40:32.924 DEBUG 31204 --- [ main] d.a.e.a.c.config.DatabaseConfiguration : We are using this datasource: org.apache.tomcat.jdbc.pool.DataSource@19f040ba{ConnectionPool[defaultAutoCommit=null; ...
2016-01-18 14:40:32.919 DEBUG 31204 --- [ main] d.a.e.a.c.config.DatabaseConfiguration : properties after: {password=wonttell, driverClassName=oracle.jdbc.OracleDriver, defaultRowPrefetch=1000, defaultBatchValue=1000, url=jdbc:oracle:thin:@xyz:1521:abc, username=ihave}
Run Code Online (Sandbox Code Playgroud)
执行器向我显示我的代码替换了数据源:
但是没有激活设置,我可以在分析应用程序时看到这些设置.在defaultRowPrefetch仍处于10这将导致我SELECT使他能远远低于他们是,如果1000被激活.
设置池connectionProperties应该工作.这些将被传递给JDBC驱动程序.将其添加到application.properties:
spring.datasource.connectionProperties: defaultRowPrefetch=1000;defaultBatchValue=1000
Run Code Online (Sandbox Code Playgroud)
编辑(一些背景信息):
另请注意,您可以通过spring.datasource配置任何DataSource实现特定属性.*:有关更多详细信息,请参阅您正在使用的连接池实现的文档.
由于 Spring Boot 已停产很长一段时间,我切换到 Spring Boot 2.1 及其新的默认连接池 Hikari。这里的解决方案更加简单,可以在 application.properties 或(如此处所示)application.yml 中完成:
spring:
datasource:
hikari:
data-source-properties:
defaultRowPrefetch: 1000
Run Code Online (Sandbox Code Playgroud)
(在现实生活中的配置中,还会有其他几个配置项,但由于它们对所提出的问题不感兴趣,所以我只是在示例中将它们排除在外)
| 归档时间: |
|
| 查看次数: |
25614 次 |
| 最近记录: |