tomcat-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From kfuj...@apache.org
Subject svn commit: r1820818 - in /tomcat/tc8.0.x/trunk: modules/jdbc-pool/src/main/java/org/apache/tomcat/jdbc/pool/interceptor/StatementCache.java webapps/docs/changelog.xml
Date Thu, 11 Jan 2018 01:42:04 GMT
Author: kfujino
Date: Thu Jan 11 01:42:04 2018
New Revision: 1820818

URL: http://svn.apache.org/viewvc?rev=1820818&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/tc8.0.x/trunk/modules/jdbc-pool/src/main/java/org/apache/tomcat/jdbc/pool/interceptor/StatementCache.java
    tomcat/tc8.0.x/trunk/webapps/docs/changelog.xml

Modified: tomcat/tc8.0.x/trunk/modules/jdbc-pool/src/main/java/org/apache/tomcat/jdbc/pool/interceptor/StatementCache.java
URL: http://svn.apache.org/viewvc/tomcat/tc8.0.x/trunk/modules/jdbc-pool/src/main/java/org/apache/tomcat/jdbc/pool/interceptor/StatementCache.java?rev=1820818&r1=1820817&r2=1820818&view=diff
==============================================================================
--- tomcat/tc8.0.x/trunk/modules/jdbc-pool/src/main/java/org/apache/tomcat/jdbc/pool/interceptor/StatementCache.java
(original)
+++ tomcat/tc8.0.x/trunk/modules/jdbc-pool/src/main/java/org/apache/tomcat/jdbc/pool/interceptor/StatementCache.java
Thu Jan 11 01:42:04 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};
@@ -198,16 +201,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())) {
@@ -225,9 +226,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;
@@ -237,6 +237,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/tc8.0.x/trunk/webapps/docs/changelog.xml
URL: http://svn.apache.org/viewvc/tomcat/tc8.0.x/trunk/webapps/docs/changelog.xml?rev=1820818&r1=1820817&r2=1820818&view=diff
==============================================================================
--- tomcat/tc8.0.x/trunk/webapps/docs/changelog.xml (original)
+++ tomcat/tc8.0.x/trunk/webapps/docs/changelog.xml Thu Jan 11 01:42:04 2018
@@ -128,6 +128,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-unsubscribe@tomcat.apache.org
For additional commands, e-mail: dev-help@tomcat.apache.org


Mime
View raw message