[Logo] Terracotta Discussion Forums
  [Search] Search   [Recent Topics] Recent Topics   [Members]  Member Listing   [Groups] Back to home page 
[Register] Register / 
[Login] Login 
[Expert]
Problems with CacheManager.create() in servlet  XML
Forum Index -> Ehcache
Author Message
hsn

neo

Joined: 03/12/2012 05:26:00
Messages: 4
Offline

I used to have CacheManager.create() in servlet init handler and CacheManager.getInstance().getCache() in rest of my servlet code.

every time i called getInstance() following warning was logged.

11:02:50,848 WARN [net.sf.ehcache.config.ConfigurationFactory] (ajp--127.0.0.1-8009-12) No configuration found. Configuring ehcache from ehcache-failsafe.xml
found in the classpath: vfs:/content/hlodac-0.0.1-SNAPSHOT.war/WEB-INF/lib/ehcache-core-2.5.1.jar/ehcache-failsafe.xml
11:02:50,861 WARN [net.sf.ehcache.config.ConfigurationFactory] (ajp--127.0.0.1-8009-13) No configuration found. Configuring ehcache from ehcache-failsafe.xml
found in the classpath: vfs:/content/hlodac-0.0.1-SNAPSHOT.war/WEB-INF/lib/ehcache-core-2.5.1.jar/ehcache-failsafe.xml

also my servlet application didnt worked right, often empty page was retuned with no error message in jboss log files. I had to rewrite code to store instances in servlet ctx to get app working again.

CacheManager manager = CacheManager.create();
manager.removalAll();
Cache documents = new Cache("documents", 1, false, false, 10*60, 10*60, false, 600);
manager.addCache(documents);
Cache lastsearch = new Cache("lastsearch", 20, MemoryStoreEvictionPolicy.FIFO, false, null, false, 24*60, 24*60, false, 600, null);
manager.addCache(lastsearch);
ServletContext ctx = cte.getServletContext();
ctx.setAttribute("documents", manager.getCache("documents"));
ctx.setAttribute("lastsearch", manager.getCache("lastsearch"));
ctx.setAttribute("ehcache", manager);

I think you should look at this problem and/or write some guidelines how to use ehcache in servlets properly.
rajoshi

seraphim

Joined: 07/04/2011 04:36:10
Messages: 1491
Offline

Hi ,

Using static CacheManager.create() methods, will always return the named (or default unnamed) CacheManager if it already exists in that JVM. So here also the same instance is being returned when you are storing the instance in servlet ctx. Can you please share the piece of code in which you are facing the issue for clarity. Here is the link to the documentation about using Cachemanagers :

http://ehcache.org/documentation/code-samples#Using-the-CacheManager

Rakesh Joshi
Senior Consultant
Terracotta.
hsn

neo

Joined: 03/12/2012 05:26:00
Messages: 4
Offline

I am looking at ehcache source code and seems to be right in sync with provided documentation

public static CacheManager create() throws CacheException {
if (singleton != null) {
LOG.debug("Attempting to create an existing singleton. Existing singleton returned.");
return singleton;
}

but it didnt worked for me that way. Maybe it has something to do with servlet engine, one thread do not see static variable in other thread.

give me email i will send you 2 code snapshots. Using this:

CacheManager manager = CacheManager.getInstance();
Cache cache = manager.getCache("documents");
Element element;
element = cache.get("documents");

didnt worked. currently using this - storing Cache instances into application context

public static String asString(ServletContext ctx) throws MalformedURLException, NamingException, SolrServerException {
Cache cache = (Cache) ctx.getAttribute("documents");
Element element;
element = cache.get("documents");
if (element != null )
return (String)element.getObjectValue();

hsn

neo

Joined: 03/12/2012 05:26:00
Messages: 4
Offline

I guess that problem is caused by class garbage collection, it resets static variables to their defaults.
 
Forum Index -> Ehcache
Go to:   
Powered by JForum 2.1.7 © JForum Team