'Oracle DB connections not releasing from connection pool in Tomcat 8

We are migrating Tomcat6, java 6 and Oracle 10g web-applications to Tomcat 8, Java 8 and Oracle 10g. Our applications working fine after migrated, but initial connections (initialSize="5") available in connection pool not released after Tomcat shut down. When second time starting tomcat, its creating 5 more initial connections to pool. I am using below resource configuration in server.xml

<Resource   name="TestAppDataSource"
            auth="Container"
            factory="org.apache.tomcat.jdbc.pool.DataSourceFactory"
            type="javax.sql.DataSource"
            driverClassName="oracle.jdbc.OracleDriver"
            initialSize="5"
            maxActive="40"
            maxIdle="40"
            minIdle="5"
            timeBetweenEvictionRunsMillis="30000"
            minEvictableIdleTimeMillis="30000"
            maxWait="10000"
            testWhileIdle="true"
            testOnBorrow="true"
            testOnReturn="false"
            validationQuery="SELECT 1 from dual"
            validationInterval="30000" 
            logAbandoned="true"
            removeAbandonedTimeout="30"
            removeAbandonedOnBorrow="true"
            removeAbandonedOnMaintenance="true"
            suspectTimeout="300"
            maxAge="60000"
            url="jdbc:oracle:thin:@//IP_ADDRESS:1521/SCHEMA_NAME"
            username="USER_NAME"
            password="PASSWORD" />

And below resource link configuration in application META_INF/context.xml

<ResourceLink
    name="APP_TEST"
    global="TestAppDataSource" 
    type="javax.sql.DataSource"
/>

I am using ojdbc7.jar for oracle driver. Please help whether i missed any configuration..



Solution 1:[1]

try with the follow option:

removeAbandoned = true 

(boolean) Flag to remove abandoned connections if they exceed the removeAbandonedTimeout. If set to true a connection is considered abandoned and eligible for removal if it has been in use longer than the removeAbandonedTimeout Setting this to true can recover db connections from applications that fail to close a connection. See also logAbandoned The default value is false.

Tomcat now use JDBC Connection Pool org.apache.tomcat.jdbc.pool that is a replacement or an alternative to the Apache Commons DBCP connection pool.

removeAbandoned is a option for JDBC Connection Pool

https://tomcat.apache.org/tomcat-8.0-doc/jdbc-pool.html

Solution 2:[2]

You have to add closeMethod="close" to your JDBC resource in context.xml. That way, Tomcat properly releases pending connections to the database.

Solution 3:[3]

Nitpick: when tomcat is shut down, the JVM is shut down, hence all of its resources are "released" too, and there is no more connection pool - what you meant is that the connections are not properly being shut down, so the DB is not being notified that they're closed, and hence the sessions there are not being ended. This is either because the pool is not getting a shutdown command, or because something else is hanging in tomcat during shutdown and hence it's not getting to the point of shutting down the pool, being force-killed by the shutdown script after a wait timeout has expired. You can take thread dumps during shutdown to see what it's waiting on, and look at catalina.out for messages about leaked threads (...has started a thread ... that's not been shut down ...). It is a common problem that webapps will launch long-running threads without daemonizing them - such webapps need to implement a ServletContextListener that will stop this thread/resource when the ServletContext is stopped.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 johnnymnmonic
Solution 2 bounce
Solution 3 volkerk