How to set custom connection properties on DataSource in Spring Boot 1.3.x with default Tomcat connection pool

后端 未结 4 1474
再見小時候
再見小時候 2020-12-31 11:01

I need to set some specific Oracle JDBC connection properties in order to speed up batch INSERTs (defaultBatchValue) and mass SELECTs

4条回答
  •  粉色の甜心
    2020-12-31 11:23

    After digging around in the Tomcat code for a bit, I found that the dataSource.getPoolProperties().getDbProperties() is the Properties object that will actually get used to generate connections for the pool.

    If you use the BeanPostProcessor approach mentioned by @m-deinum, but instead use it to populate the dbProperties like so, you should be able to add the properties in a way that makes them stick and get passed to the Oracle driver.

    import java.util.Properties;
    import org.apache.tomcat.jdbc.pool.DataSource;
    import org.apache.tomcat.jdbc.pool.PoolConfiguration;
    
    @Component
    public class OracleConfigurer implements BeanPostProcessor {
        @Override
        public Object postProcessBeforeInitialization(Object bean, String name) throws BeansException {
            if (bean instanceof DataSource) {
                DataSource dataSource = (DataSource)bean;
                PoolConfiguration configuration = dataSource.getPoolProperties();
                Properties properties = configuration.getDbProperties();
                if (null == properties) properties = new Properties();
                properties.put("defaultRowPrefetch", 1000);
                properties.put("defaultBatchValue", 1000);
                configuration.setDbProperties(properties);
            }
            return bean;
        }
    
        @Override
        public Object postProcessAfterInitialization(Object bean, String name) throws BeansException {
            return bean;
        }
    }
    

提交回复
热议问题