简体   繁体   中英

Setting an embedded tomcat property in spring boot, bean usage?

I have read and done this https://www.mkyong.com/spring-boot/spring-boot-configure-maxswallowsize-in-embedded-tomcat/ by including it in one of my classes but I have no idea how to ensure it is supposed to be executed. I even tried running the function in my code but I keep getting ERR_CONNECTION_RESET being returned to my web app.

I haven't changed anything else. Is there something I am missing?

I faced this problem before and find a solutions like this; setting these parameters on application.properties :

spring.datasource.test-on-borrow=true
spring.datasource.test-while-idle=true
spring.datasource.validation-query=SELECT 1

But It did not worked for me with my spring-boot version. Then I've checked the tomcat jdbc datasource factory fields and noticed that the fields of the dataSourceFactory are not set successfully by application.properties configurations.

Finally I resolved it by setting these fields after application initialization, and my connection reset problem resolved.:

@Component
public class PoolConfiguration implements BeanPostProcessor {

    @Value("${spring.datasource.test-on-borrow:true}")
    private boolean isTestOnBorrow;

    @Value("${spring.datasource.test-while-idle:true}")
    private boolean isTestWhileIdle;

    @Value("${spring.datasource.validation-query:SELECT 1}")
    private String validationQuery;

    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        return bean;
    }

    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        if (bean instanceof TomcatJdbcDataSourceFactory) {
            TomcatJdbcDataSourceFactory tomcatJdbcDataSourceFactory = (TomcatJdbcDataSourceFactory) bean;
            tomcatJdbcDataSourceFactory.setTestOnBorrow(isTestOnBorrow);
            tomcatJdbcDataSourceFactory.setTestWhileIdle(isTestWhileIdle);
            tomcatJdbcDataSourceFactory.setValidationQuery(validationQuery);
        }
        return bean;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM