cayenne-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ntimof...@apache.org
Subject [cayenne] branch master updated: CAY-2642 EhCache memory leak due to misconfiguration - add a warning when creating new cache entry with default config
Date Mon, 30 Dec 2019 06:56:13 GMT
This is an automated email from the ASF dual-hosted git repository.

ntimofeev pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/cayenne.git


The following commit(s) were added to refs/heads/master by this push:
     new 4720d65  CAY-2642 EhCache memory leak due to misconfiguration   - add a warning when
creating new cache entry with default config
     new c5225e0  Merge pull request #410 from stariy95/4.2-CAY-2642-jcache-memory-leak
4720d65 is described below

commit 4720d65914fc474c2359c18d4e8414fe68022412
Author: Nikita Timofeev <stariy95@gmail.com>
AuthorDate: Thu Dec 26 11:46:33 2019 +0300

    CAY-2642 EhCache memory leak due to misconfiguration
      - add a warning when creating new cache entry with default config
---
 cayenne-jcache/pom.xml                             | 26 +++++++++++++++++-----
 .../apache/cayenne/jcache/JCacheQueryCache.java    |  7 ++++++
 .../jcache/unit/CacheServerRuntimeProvider.java    | 12 ++++++++++
 cayenne-jcache/src/test/resources/eh-cache.xml     | 10 +++++++++
 4 files changed, 50 insertions(+), 5 deletions(-)

diff --git a/cayenne-jcache/pom.xml b/cayenne-jcache/pom.xml
index 63fa742..ad67f29 100644
--- a/cayenne-jcache/pom.xml
+++ b/cayenne-jcache/pom.xml
@@ -31,8 +31,9 @@
     <packaging>jar</packaging>
 
     <properties>
-        <ehcache-version>3.3.1</ehcache-version>
         <jcache-version>1.0.0</jcache-version>
+        <ehcache-version>3.8.1</ehcache-version>
+        <jaxb-api-version>2.3.1</jaxb-api-version>
     </properties>
 
     <dependencyManagement>
@@ -47,6 +48,16 @@
                 <artifactId>cache-api</artifactId>
                 <version>${jcache-version}</version>
             </dependency>
+            <dependency>
+                <groupId>javax.xml.bind</groupId>
+                <artifactId>jaxb-api</artifactId>
+                <version>${jaxb-api-version}</version>
+            </dependency>
+            <dependency>
+                <groupId>com.sun.xml.bind</groupId>
+                <artifactId>jaxb-impl</artifactId>
+                <version>${jaxb-api-version}</version>
+            </dependency>
         </dependencies>
     </dependencyManagement>
 
@@ -89,13 +100,18 @@
             <scope>test</scope>
         </dependency>
         <dependency>
-            <groupId>org.ehcache</groupId>
-            <artifactId>ehcache</artifactId>
+            <groupId>javax.xml.bind</groupId>
+            <artifactId>jaxb-api</artifactId>
             <scope>test</scope>
         </dependency>
         <dependency>
-            <groupId>org.slf4j</groupId>
-            <artifactId>slf4j-api</artifactId>
+            <groupId>com.sun.xml.bind</groupId>
+            <artifactId>jaxb-impl</artifactId>
+            <scope>test</scope>
+        </dependency>
+        <dependency>
+            <groupId>org.ehcache</groupId>
+            <artifactId>ehcache</artifactId>
             <scope>test</scope>
         </dependency>
         <dependency>
diff --git a/cayenne-jcache/src/main/java/org/apache/cayenne/jcache/JCacheQueryCache.java
b/cayenne-jcache/src/main/java/org/apache/cayenne/jcache/JCacheQueryCache.java
index ceb7c27..2973153 100644
--- a/cayenne-jcache/src/main/java/org/apache/cayenne/jcache/JCacheQueryCache.java
+++ b/cayenne-jcache/src/main/java/org/apache/cayenne/jcache/JCacheQueryCache.java
@@ -24,6 +24,8 @@ import org.apache.cayenne.cache.QueryCacheEntryFactory;
 import org.apache.cayenne.di.BeforeScopeEnd;
 import org.apache.cayenne.di.Inject;
 import org.apache.cayenne.query.QueryMetadata;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 import javax.cache.Cache;
 import javax.cache.CacheException;
@@ -39,6 +41,8 @@ import java.util.concurrent.ConcurrentHashMap;
  */
 public class JCacheQueryCache implements QueryCache {
 
+    private static final Logger LOGGER = LoggerFactory.getLogger(QueryCache.class);
+
     @Inject
     protected CacheManager cacheManager;
 
@@ -139,6 +143,9 @@ public class JCacheQueryCache implements QueryCache {
     }
 
     protected Cache createCache(String cacheName) {
+        // Cache creation here can lead to a memory leak, see CAY-2642 for details.
+        LOGGER.warn("Creating a new JCache entry '{}'. It will be unlimited by default, and
that can lead to greater memory usage or even leak. " +
+                "This entry could be configured by JCache provider-specific configuration.",
cacheName);
         return cacheManager.createCache(cacheName, configurationFactory.create(cacheName));
     }
 
diff --git a/cayenne-jcache/src/test/java/org/apache/cayenne/jcache/unit/CacheServerRuntimeProvider.java
b/cayenne-jcache/src/test/java/org/apache/cayenne/jcache/unit/CacheServerRuntimeProvider.java
index 120832f..df9da48 100644
--- a/cayenne-jcache/src/test/java/org/apache/cayenne/jcache/unit/CacheServerRuntimeProvider.java
+++ b/cayenne-jcache/src/test/java/org/apache/cayenne/jcache/unit/CacheServerRuntimeProvider.java
@@ -19,9 +19,11 @@
 
 package org.apache.cayenne.jcache.unit;
 
+import java.net.URISyntaxException;
 import java.util.ArrayList;
 import java.util.Collection;
 
+import org.apache.cayenne.CayenneRuntimeException;
 import org.apache.cayenne.dba.DbAdapter;
 import org.apache.cayenne.di.Inject;
 import org.apache.cayenne.di.Module;
@@ -45,6 +47,16 @@ public class CacheServerRuntimeProvider extends ServerRuntimeProvider {
     protected Collection<? extends Module> getExtraModules() {
         Collection<Module> modules = new ArrayList<>(super.getExtraModules());
         modules.add(new JCacheModule());
+
+        String configURI;
+        try {
+            configURI = getClass().getResource("/eh-cache.xml").toURI().toString();
+        } catch (URISyntaxException e) {
+            throw new CayenneRuntimeException("Unable to resolve ehcache config resource
URI.");
+        }
+
+        modules.add(binder -> JCacheModule
+                .contributeJCacheProviderConfig(binder, configURI));
         return modules;
     }
 }
diff --git a/cayenne-jcache/src/test/resources/eh-cache.xml b/cayenne-jcache/src/test/resources/eh-cache.xml
new file mode 100644
index 0000000..60f4455
--- /dev/null
+++ b/cayenne-jcache/src/test/resources/eh-cache.xml
@@ -0,0 +1,10 @@
+<config xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'
+        xmlns='http://www.ehcache.org/v3'
+        xsi:schemaLocation="http://www.ehcache.org/v3 http://www.ehcache.org/schema/ehcache-core-3.0.xsd">
+
+    <cache alias="g1">
+        <heap unit="entries">100</heap>
+    </cache>
+
+</config>
+


Mime
View raw message