Author: kfujino Date: Thu Jan 11 01:43:11 2018 New Revision: 1820819 URL: http://svn.apache.org/viewvc?rev=1820819&view=rev Log: Fix https://bz.apache.org/bugzilla/show_bug.cgi?id=61312 Prevent NullPointerException when using the statement cache of connection that has been closed.
Modified: tomcat/tc7.0.x/trunk/modules/jdbc-pool/src/main/java/org/apache/tomcat/jdbc/pool/interceptor/StatementCache.java tomcat/tc7.0.x/trunk/webapps/docs/changelog.xml Modified: tomcat/tc7.0.x/trunk/modules/jdbc-pool/src/main/java/org/apache/tomcat/jdbc/pool/interceptor/StatementCache.java URL: http://svn.apache.org/viewvc/tomcat/tc7.0.x/trunk/modules/jdbc-pool/src/main/java/org/apache/tomcat/jdbc/pool/interceptor/StatementCache.java?rev=1820819&r1=1820818&r2=1820819&view=diff ============================================================================== --- tomcat/tc7.0.x/trunk/modules/jdbc-pool/src/main/java/org/apache/tomcat/jdbc/pool/interceptor/StatementCache.java (original) +++ tomcat/tc7.0.x/trunk/modules/jdbc-pool/src/main/java/org/apache/tomcat/jdbc/pool/interceptor/StatementCache.java Thu Jan 11 01:43:11 2018 @@ -25,6 +25,8 @@ import java.util.Map; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.atomic.AtomicInteger; +import org.apache.juli.logging.Log; +import org.apache.juli.logging.LogFactory; import org.apache.tomcat.jdbc.pool.ConnectionPool; import org.apache.tomcat.jdbc.pool.PoolProperties.InterceptorProperty; import org.apache.tomcat.jdbc.pool.PooledConnection; @@ -34,6 +36,7 @@ import org.apache.tomcat.jdbc.pool.Poole * {@code CallableStatement} instances on a connection. */ public class StatementCache extends StatementDecoratorInterceptor { + private static final Log log = LogFactory.getLog(StatementCache.class); protected static final String[] ALL_TYPES = new String[] {PREPARE_STATEMENT,PREPARE_CALL}; protected static final String[] CALLABLE_TYPE = new String[] {PREPARE_CALL}; protected static final String[] PREPARED_TYPE = new String[] {PREPARE_STATEMENT}; @@ -197,16 +200,14 @@ public class StatementCache extends Stat } public CachedStatement isCached(Method method, Object[] args) { - @SuppressWarnings("unchecked") - ConcurrentHashMap<CacheKey,CachedStatement> cache = - (ConcurrentHashMap<CacheKey,CachedStatement>)pcon.getAttributes().get(STATEMENT_CACHE_ATTR); + ConcurrentHashMap<CacheKey,CachedStatement> cache = getCache(); + if (cache == null) return null; return cache.get(createCacheKey(method, args)); } public boolean cacheStatement(CachedStatement proxy) { - @SuppressWarnings("unchecked") - ConcurrentHashMap<CacheKey,CachedStatement> cache = - (ConcurrentHashMap<CacheKey,CachedStatement>)pcon.getAttributes().get(STATEMENT_CACHE_ATTR); + ConcurrentHashMap<CacheKey,CachedStatement> cache = getCache(); + if (cache == null) return false; if (proxy.getCacheKey()==null) { return false; } else if (cache.containsKey(proxy.getCacheKey())) { @@ -224,9 +225,8 @@ public class StatementCache extends Stat } public boolean removeStatement(CachedStatement proxy) { - @SuppressWarnings("unchecked") - ConcurrentHashMap<CacheKey,CachedStatement> cache = - (ConcurrentHashMap<CacheKey,CachedStatement>)pcon.getAttributes().get(STATEMENT_CACHE_ATTR); + ConcurrentHashMap<CacheKey,CachedStatement> cache = getCache(); + if (cache == null) return false; if (cache.remove(proxy.getCacheKey()) != null) { cacheSize.decrementAndGet(); return true; @@ -236,6 +236,17 @@ public class StatementCache extends Stat } /*end the actual statement cache*/ + protected ConcurrentHashMap<CacheKey,CachedStatement> getCache() { + PooledConnection pCon = this.pcon; + if (pCon == null) { + if (log.isWarnEnabled()) log.warn("Connection has already been closed or abandoned"); + return null; + } + @SuppressWarnings("unchecked") + ConcurrentHashMap<CacheKey,CachedStatement> cache = + (ConcurrentHashMap<CacheKey,CachedStatement>)pCon.getAttributes().get(STATEMENT_CACHE_ATTR); + return cache; + } protected class CachedStatement extends StatementDecoratorInterceptor.StatementProxy<Statement> { boolean cached = false; Modified: tomcat/tc7.0.x/trunk/webapps/docs/changelog.xml URL: http://svn.apache.org/viewvc/tomcat/tc7.0.x/trunk/webapps/docs/changelog.xml?rev=1820819&r1=1820818&r2=1820819&view=diff ============================================================================== --- tomcat/tc7.0.x/trunk/webapps/docs/changelog.xml (original) +++ tomcat/tc7.0.x/trunk/webapps/docs/changelog.xml Thu Jan 11 01:43:11 2018 @@ -131,6 +131,14 @@ </add> </changelog> </subsection> + <subsection name="jdbc-pool"> + <changelog> + <fix> + <bug>61312</bug>: Prevent <code>NullPointerException</code>n when using + the statement cache of connection that has been closed. (kfujino) + </fix> + </changelog> + </subsection> <subsection name="Other"> <changelog> <update> --------------------------------------------------------------------- To unsubscribe, e-mail: dev-unsubscr...@tomcat.apache.org For additional commands, e-mail: dev-h...@tomcat.apache.org