<div dir="ltr"><br><div class="gmail_extra"><br><br><div class="gmail_quote">On Fri, Feb 8, 2013 at 3:41 PM, Galder Zamarreño <span dir="ltr">&lt;<a href="mailto:galder@redhat.com" target="_blank">galder@redhat.com</a>&gt;</span> wrote:<br>

<blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">Hi all,<br>
<br>
We&#39;ve got a small class loading puzzle to solve in our JSR-107 implementation.<br>
<br>
JSR-107 has a class called Caching which keeps a singleton enum reference (AFAIK, has same semantics as static) to the systemt&#39;s CacheManagerFactory, which in our case it would be InfinispanCacheManagerFactory:<br>
<a href="https://github.com/jsr107/jsr107spec/blob/master/src/main/java/javax/cache/Caching.java" target="_blank">https://github.com/jsr107/jsr107spec/blob/master/src/main/java/javax/cache/Caching.java</a><br>
<br>
A naive user of JSR-107 could decide to use this Caching class in an app server environment and get a reference to the CMF through it, which could cause major classloading issues if we don&#39;t protect ourselves.<br>
<br>
Within out CMF implementation, we need to keep some kind of mapping which given a name *and* a classloader, which can find the CacheManager instance associated to it.<br>
<br>
This poses a potential risk of a static strong reference being held indirectly on the classloader associated with the Infinispan Cache Manager (amongst other sensible components...).<br>
<br>
One way to break this strong reference is for CMF implementation to hold a weak reference on the CM as done here:<br>
<a href="https://github.com/galderz/infinispan/blob/t_2639/jsr107/src/main/java/org/infinispan/jsr107/cache/InfinispanCacheManagerFactory.java#L56" target="_blank">https://github.com/galderz/infinispan/blob/t_2639/jsr107/src/main/java/org/infinispan/jsr107/cache/InfinispanCacheManagerFactory.java#L56</a><br>


<br>
This poses a problem though in that the Infinispan Cache Manager can be evicted from memory without it&#39;s stop/shutdown method being called, leading to resources being left open (i.e. jgroups, jmx…etc).<br>
<br>
The only safe way to deal with this that I&#39;ve thought so far is to have a finalyze() method in InfinispanCacheManager (JSR-107 impl of CacheManager) that makes sure this cache manager is shut down. I&#39;m fully aware this is an expensive operation, but so far is the only way I can see in which we can avoid leaking stuff, while not affecting the actual Infinispan core module.<br>


<br>
I&#39;ve found a good example of this in <a href="https://github.com/jbossas/jboss-as/blob/master/controller-client/src/main/java/org/jboss/as/controller/client/impl/RemotingModelControllerClient.java" target="_blank">https://github.com/jbossas/jboss-as/blob/master/controller-client/src/main/java/org/jboss/as/controller/client/impl/RemotingModelControllerClient.java</a> - It even tracks creation time so that if all references to InfinispanCacheManager are lost but the ICM instance is not closed, it will print a warm message.<br>


<br>
If anyone has any other thoughts, it&#39;d be interesting to hear about them.<br>
<br></blockquote><div><br></div><div><br>The Caching javadoc seems to prohibit stopping the CacheManagers without user intervention (<a href="https://github.com/jsr107/jsr107spec/blob/master/src/main/java/javax/cache/Caching.java#L35">https://github.com/jsr107/jsr107spec/blob/master/src/main/java/javax/cache/Caching.java#L35</a>):<br>

<br><pre><div class="" id="LC35"><span class=""> * Also keeps track of all CacheManagers created by the factory. Subsequent calls</span></div><div class="" id="LC36"><span class=""> * to {@link #getCacheManager()} return the same CacheManager.</span></div>

</pre><br></div><div>And in the javadoc of Caching.close() (<a href="https://github.com/jsr107/jsr107spec/blob/master/src/main/java/javax/cache/Caching.java#L153">https://github.com/jsr107/jsr107spec/blob/master/src/main/java/javax/cache/Caching.java#L153</a>):<br>

<pre><div class="" id="LC139"><span class="">     * All cache managers obtained from the factory are shutdown.</span></div><div class="" id="LC140"><span class="">     * &lt;p/&gt;</span></div><div class="" id="LC141"><span class="">     * Subsequent requests from this factory will return different cache managers than would have been obtained before</span></div>

<div class="" id="LC142"><span class="">     * shutdown. So for example</span></div></pre><pre><div class="" id="LC143"><span class="">     * &lt;pre&gt;</span></div><div class="" id="LC144"><span class="">     *  CacheManager cacheManager = CacheFactory.getCacheManager();</span></div>

<div class="" id="LC145"><span class="">     *  assertSame(cacheManager, CacheFactory.getCacheManager());</span></div><div class="" id="LC146"><span class="">     *  CacheFactory.close();</span></div><div class="" id="LC147">

<span class="">     *  assertNotSame(cacheManager, CacheFactory.getCacheManager());</span></div><div class="" id="LC148"><span class="">     * &lt;/pre&gt;<br><br></span></div></pre></div></div><div class="gmail_quote">We can&#39;t guarantee that getCacheManager() will return the same instance unless we keep a hard reference to it in our CacheManagerFactory. So I think the only option is to add a finalize() method to CacheManagerFactory that will stop all the CacheManagers if the user didn&#39;t explicitly call Caching.close().<br>

</div><div class="gmail_quote"><br>Cheers<br></div><div class="gmail_quote">Dan<br><br></div><div class="gmail_quote"><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">


Cheers,<br>
--<br>
Galder Zamarreño<br>
<a href="mailto:galder@redhat.com">galder@redhat.com</a><br>
<a href="http://twitter.com/galderz" target="_blank">twitter.com/galderz</a><br>
<br>
Project Lead, Escalante<br>
<a href="http://escalante.io" target="_blank">http://escalante.io</a><br>
<br>
Engineer, Infinispan<br>
<a href="http://infinispan.org" target="_blank">http://infinispan.org</a><br>
<br>
<br>
_______________________________________________<br>
infinispan-dev mailing list<br>
<a href="mailto:infinispan-dev@lists.jboss.org">infinispan-dev@lists.jboss.org</a><br>
<a href="https://lists.jboss.org/mailman/listinfo/infinispan-dev" target="_blank">https://lists.jboss.org/mailman/listinfo/infinispan-dev</a><br>
</blockquote></div><br></div></div>