spring - 使用 Spring Boots application.properties 优化 JDBC 获取大小

标签 spring oracle jdbc spring-boot jdbctemplate

我的基于 Spring Boot 1.3.1 的应用程序依赖于 Oracle 11.2 数据库,我想调整 SELECT 语句结果的获取。

JdbcTemplate 提供 public void setFetchSize(int fetchSize) 来调整获取大小,对于 Oracle,驱动程序将其预设为 10:

Set the fetch size for this JdbcTemplate. This is important for processing large result sets: Setting this higher than the default value will increase processing speed at the cost of memory consumption; setting this lower can avoid transferring row data that will never be read by the application. Default is -1, indicating to use the JDBC driver's default (i.e. to not pass a specific fetch size setting on the driver).

Oracle JDBC 驱动程序(我使用 ojdbc7.jar 因为它向下兼容)提供了一个 defaultRowPrefetch 参数来增加完整数据库连接的获取大小。

根据the docs该参数可以这样设置:

java.util.Properties info = new java.util.Properties();
info.put ("user", "scott");
info.put ("password","tiger");
info.put ("defaultRowPrefetch","15");
getConnection ("jdbc:oracle:oci8:@",info);

但是我的应用程序是使用 application.yml 配置的:

datasource:
    url: jdbc:oracle:thin:@xyz:1521:abc
    username: ${name}
    password: ${password}
    driver-class-name: oracle.jdbc.driver.OracleDriver
    ...

即使我想更改该配置以使用 spring.datasource.url=jdbc:... ,也无法根据 this post 全局设置获取大小。 .

是否有更“Spring Boot 风格”的方法或者我需要手动配置每个模板?

最佳答案

一个BeanPostProcessor将处理 ApplicationContext 中的所有 bean,这样您就可以添加其他配置或完全替换它(如果您愿意)。

您可以创建一个BeanPostProcessor,它将属性添加到配置的DataSource。下面的示例假定使用 commons-dbcp 1 或 2(如果您使用不同的 DataSource 进行相应修改)。

public class DataSourceConfiguringBeanPostProcessor implements BeanPostProcessor {
    private final Map<String,String> properties = new HashMap<>;

    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        if (bean instance BasicDataSource ) { 
            for (Map.Entry<String, String> prop : properties.entrySet()) {
                ((BasicDataSource) bean).addConnectionProperty(prop.getKey(), prop.getValue());
            }
        }
        return bean;
    }

    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        return bean;
    }

    public void setProperties(Map<String, String> properties) {
        this.properties.putAll(properties);
    }
}

现在您可以将其添加到您的配置中,它会将属性添加到 DataSource beans。

@Bean
public BeanPostProcessor dataSourcePostProcessor() {
    DataSourceConfiguringBeanPostProcessor processor = new DataSourceConfiguringBeanPostProcessor();
    Map<String, String> properties = new HashMap<>();
    properties.put("defaultRowPrefetch", "15");
    properties.put("defaultBatchValue", "25");
    processor.setProperties(properties);
    return processor;
}

这应该可以解决配置数据源的问题。

关于spring - 使用 Spring Boots application.properties 优化 JDBC 获取大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34789220/

相关文章:

Oracle - 此范围内不存在名称为 X 的函数

mysql - sql : combining 2 query to give 1 resultset

java - 间歇性 JDBC 管道损坏和链接失败

java - 如何从数据库中获取自动生成的id

java - 在java/jdbc中,如果在db2中包含 "for fetch only with ur",那么在执行选择后是否必须调用commit?

java - 我可以从另一个使用 @Autowired 的项目调用方法吗?

java - 当页面没有元素时返回空的 "content"数组

Java + spring - 将历史事件持久保存到数据库日志的中心方式

java - Spring 安全 : Username is sent empty for login after upgrade to Spring-Security 4. 1

java - 具有等级和分区的 SQL