I need to set some specific JDBC connection properties to speed up the INSERT batch ( defaultBatchValue ) and the SELECT mass ( defaultRowPrefetch ). I received suggestions on how to do this using DBCP (thanks to M. Deinum), but I would like to:
- save Tomcat jdbc default connection pool
- save application.yml for configuration
I was thinking about requesting the support function spring.datasource.custom_connection_properties or the like in the future, and because of this I tried to pretend that it was already possible. I did this by passing the relevant information when creating the DataSource and manipulating the creation of the DataSource as follows:
@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; }
In the logs, I see that I am creating the correct DataSource:
2016-01-18 14:40:32.924 DEBUG 31204 --- [ main] daeacconfig.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] daeacconfig.DatabaseConfiguration : properties after: {password=wonttell, driverClassName=oracle.jdbc.OracleDriver, defaultRowPrefetch=1000, defaultBatchValue=1000, url=jdbc:oracle:thin:@xyz:1521:abc, username=ihave}
The drive shows that my code replaced the data source:

But the settings are not activated, which I see when profiling the application. defaultRowPrefetch is still at 10 , which causes my SELECT be much slower than if it had been activated by 1000 .
source share