简体   繁体   中英

JDBC Connection issue on embedded tomcat

In my spring boot application, i am creating a datasource using org.apache.tomcat.jdbc.pool.DataSource. I am using embedded tomcat. Below are some of the properties i have set

myDS.setDriverClassName(driverClassName);
myDS.setUrl(databaseURL);
myDS.setUsername(databaseUserId);
myDS.setTestWhileIdle(false);
myDS.setTestOnBorrow(true);
myDS.setValidationQuery("SELECT 1");
myDS.setTestOnReturn(false);
myDS.setValidationInterval(30000);
myDS.setTimeBetweenEvictionRunsMillis(30000);
myDS.setMaxActive(100);
myDS.setInitialSize(10);
myDS.setMaxWait(10000);
myDS.setRemoveAbandonedTimeout(60);
myDS.setMinEvictableIdleTimeMillis(30000);
myDS.setMinIdle(10);
myDS.setLogAbandoned(true);
myDS.setRemoveAbandoned(true);

I am using this ds object in two spring beans which have scope as prototype. When i use first functionality which uses first spring bean application works fine. Then i hit second funcationality which uses same ds object then i get below error

org.springframework.jdbc.CannotGetJdbcConnectionException: Could not get JDBC Connection; nested exception is java.sql.SQLException: Failed to validate a newly established connection.

Internally in my code i am using jdbcTemplate for db operations. I tried using destroy method but it did not work

@Bean(destroyMethod="") public DataSource appDS(){

Please suggest

Since i am using DB2 database, i changed the validation query as follows and it started working

SELECT current date FROM sysibm.sysdummy1

Note - Embedded Tomcat and Standalone tomcat behavior is different for same configurations. On Standalone tomcat, you do not get exception for invalid validation query. External tomcat version 7

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