5 Replies Latest reply on Feb 25, 2013 9:49 AM by Jonathan.Knight

    SafeNamedCache was explicitly released error

      Hi there, we are using Coherence within Weblogic

      We have some Java code deployed on the server:

      public Session create(User user) {
      NamedCache sessionCache = CacheFactory.getCache(SESSION_CACHE_NAME);
      Session session = new Session(user);
      sessionCache.put(session.getSessionId(), session, getConfig().getSessionTTL());
      return session;

      Every now and again it fails on the sessionCache.put method (line 71) with SafeNamedCache was explicitly released.

      Error stack:
      SafeNamedCache was explicitly released</message> <ns2:stackTrace>
      <ns2:frame class="com.tangosol.coherence.component.util.SafeNamedCache" file="SafeNamedCache.CDB" line="23" method="ensureRunningNamedCache"/>
      <ns2:frame class="com.tangosol.coherence.component.util.SafeNamedCache" file="SafeNamedCache.CDB" line="1" method="getRunningNamedCache"/>
      <ns2:frame class="com.tangosol.coherence.component.util.SafeNamedCache" file="SafeNamedCache.CDB" line="1" method="put"/>
      <ns2:frame class="mywes.security.sessions.impl.SessionDao" file="SessionDao.java" line="71" method="create"/>

      Any ideas why this would be happening? And what can be done to stop this from happening?

        • 1. Re: SafeNamedCache was explicitly released error
          manish k
          You are explicitly releasing the cache


          So the error you are getting is expected.

          • 2. Re: SafeNamedCache was explicitly released error

            I assume your code is running inside some sort of Web Application inside WebLogic and is therefore multi-threaded - that is, multiple threads could call this code at the same time. In that case it may be that something like this happens...

            Thread 1: NamedCache sessionCache = CacheFactory.getCache(SESSION_CACHE_NAME);
            Thread 2: NamedCache sessionCache = CacheFactory.getCache(SESSION_CACHE_NAME);

            Thread 1: Session session = new Session(user);
            Thread 2: Session session = new Session(user);

            Thread 2: sessionCache.put(session.getSessionId(), session, getConfig().getSessionTTL());
            Thread 2: CacheFactory.releaseCache(sessionCache);

            Thread 1: sessionCache.put(session.getSessionId(), session, getConfig().getSessionTTL());

            As the cache factory and cache are singletons inside Coherence if Thread 2 has called releaseCache before Thread 1 does the put then the put will fail as the cache has been released.

            • 3. Re: SafeNamedCache was explicitly released error

              The error is not expected necessarily. As far as I know, if you called releaseCache then next time you call getCache a new local instance of the cache would be created.

              • 4. Re: SafeNamedCache was explicitly released error
                Hi Jonathan,

                That does make sense, so now how to solve that issue.

                I guess I could remove the call to releaseCache() - although I am not sure of the consequences of not calling this.

                Alternatively, I could put some retry logic in, have a catch on SafeNamedCache explicity released, then retry getCache(), put(), releaseCache().

                Any other options?

                Thanks for the help.

                Regards Rob
                • 5. Re: SafeNamedCache was explicitly released error

                  I was curious why you were calling releaseCache as it isn't something I would do. Reading the JavaDocs I can see that it makes sense to release the caches in an application that runs in an App Server so that the class loader can be released and an application re-deployed. It is a long time since I wrote a Coherence web-app but if I remember correctly we added some sort of application life cycle listener that was triggered when the application was shut down which called CacheFactory.shutdown() to release everything.