我需要设置一些特定的 Oracle JDBC 连接属性以加快批处理速度 INSERT
s ( defaultBatchValue
) 和质量 SELECT
s ( defaultRowPrefetch
)。
我收到了 suggestions如何使用 DBCP 实现这一点(感谢 M. Deinum),但我想:
我正在考虑支持功能请求
spring.datasource.custom_connection_properties
或类似的 future ,因此试图假装这已经是可能的。我通过在创建数据源时传递相关信息来做到这一点,并像这样操作数据源的创建:@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;
}
在日志中,我可以看到我正在创建正确的数据源:
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}
执行器显示我的代码替换了数据源:
但是设置未激活,我可以在分析应用程序时看到。
defaultRowPrefetch
还在10
这导致我的 SELECT
s 比 1000
时慢得多被激活。
最佳答案
设置池 connectionProperties应该工作。这些将传递给 JDBC 驱动程序。将此添加到 application.properties:
spring.datasource.connectionProperties: defaultRowPrefetch=1000;defaultBatchValue=1000
编辑(一些背景信息):
Note also that you can configure any of the DataSource implementation specific properties via spring.datasource.*: refer to the documentation of the connection pool implementation you are using for more details.
来源:spring-boot documentation
关于spring-boot - 如何使用默认的 Tomcat 连接池在 Spring Boot 1.3.x 中的 DataSource 上设置自定义连接属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34856811/