spring-boot - 如何使用默认的 Tomcat 连接池在 Spring Boot 1.3.x 中的 DataSource 上设置自定义连接属性

标签 spring-boot connection-pooling jdbctemplate ojdbc tomcat-jdbc

我需要设置一些特定的 Oracle JDBC 连接属性以加快批处理速度 INSERT s ( defaultBatchValue ) 和质量 SELECT s ( defaultRowPrefetch )。
我收到了 suggestions如何使用 DBCP 实现这一点(感谢 M. Deinum),但我想:

  • 保留默认的Tomcat jdbc 连接池
  • 保留 application.yml 用于配置

  • 我正在考虑支持功能请求 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}
    

    执行器显示我的代码替换了数据源:

    enter image description here

    但是设置未激活,我可以在分析应用程序时看到。 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/

    相关文章:

    spring - 使用开放 API 配置设置全局参数?

    oracle - Hikaricp Oracle 连接问题

    java - 1 个 ItemReader、2 个 SQL 查询、jdbcTemplate?

    java - 如何拦截自己创建的JdbcTemplate实例

    mongodb - 在 Spring 数据的 RepositoryRestResource 中急切加载 MongoDB @DBRef

    spring-boot - 使用spring嵌入式ldap模拟事件目录进行集成测试

    database - 用于 Java Web 应用程序的 Oracle 驱动程序

    java - 如何使用 JdbcTemplate 从 Spring 创建存储函数?

    java - Spring引导加载以数字为键的嵌套属性

    hibernate - Hibernate 中的 session 和连接有什么区别?