<div dir="ltr">Arjan,<div><br></div><div>So this recently came up as well with a PR raised to clarify how instance is used to destroy references: <a href="https://github.com/cdi-spec/cdi/pull/286">https://github.com/cdi-spec/cdi/pull/286</a></div><div><br></div><div>Take a look at the last paragraph here: <a href="http://docs.jboss.org/cdi/spec/1.2/cdi-spec.html#dynamic_lookup">http://docs.jboss.org/cdi/spec/1.2/cdi-spec.html#dynamic_lookup</a></div><div><br></div><div>John</div></div><br><div class="gmail_quote"><div dir="ltr">On Tue, May 10, 2016 at 11:11 AM arjan tijms &lt;<a href="mailto:arjan.tijms@gmail.com">arjan.tijms@gmail.com</a>&gt; wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><span style="font-size:12.8px">Hi,</span><div style="font-size:12.8px"><br></div><div style="font-size:12.8px">Given a simple @Stateless bean:</div><div style="font-size:12.8px"><br></div><div style="font-size:12.8px">@Stateless</div><div style="font-size:12.8px">public class Foo {</div><div style="font-size:12.8px">    public void bar() {}</div><div style="font-size:12.8px">}</div><div style="font-size:12.8px"><br></div><div style="font-size:12.8px">Then requesting an instance of this via CDI as follows:</div><div style="font-size:12.8px"><br></div><div style="font-size:12.8px">Foo foo = CDI.current().select(Foo.class).get();</div><div style="font-size:12.8px"><br></div><div style="font-size:12.8px">Causes a lot of leaked proxy instances (at least on Weld). Now what I guess needs to be done is destroying the proxy, taking Antoine&#39;s answer here as a lead: <a href="http://stackoverflow.com/questions/28767536/scope-of-stateless-bean" target="_blank">http://stackoverflow.com/questions/28767536/scope-of-stateless-bean</a></div><div style="font-size:12.8px"><br></div><div style="font-size:12.8px">Only the following throws an UnsupportedOperationException (on Weld 2.3.2, haven&#39;t tested OWB yet)</div><div style="font-size:12.8px"><br></div><div style="font-size:12.8px"><div>Instance&lt;Foo&gt; fooInstance =<span style="white-space:pre-wrap">        </span>CDI.current().select(Foo.class);</div><div>Foo foo = fooInstance.get();</div><div>foo.bar();</div><div><span style="white-space:pre-wrap">foo</span>Instance.destroy(foo);</div></div><div style="font-size:12.8px"><br></div><div style="font-size:12.8px">The question is, is this how it&#39;s supposed to be done via the spec?</div><div style="font-size:12.8px"><br></div><div style="font-size:12.8px">Implementation wise, what happens in Weld is that the CDI/EJB proxy (com.test.Foo$Proxy$_$$_Weld$EnterpriseProxy$) in the following code doesn&#39;t hit the check for a dependent instance (comments in capitals added by me):</div><div style="font-size:12.8px"><br></div><div style="font-size:12.8px"><br></div><div style="font-size:12.8px"><div>    public void destroy(T instance) {</div><div>        Preconditions.checkNotNull(instance);</div><div><br></div><div>        // check if this is a proxy of a normal-scoped bean</div><div>        if (instance instanceof ProxyObject) {</div><div><br></div><div>            // THIS BRANCH IS TAKEN FOR CDI/EJB PROXY</div><div><br></div><div>            ProxyObject proxy = (ProxyObject) instance;</div><div>            if (proxy.getHandler() instanceof ProxyMethodHandler) {</div><div>                ProxyMethodHandler handler = (ProxyMethodHandler) proxy.getHandler();</div><div>                Bean&lt;?&gt; bean = handler.getBean();</div><div>                Context context = getBeanManager().getContext(bean.getScope());</div><div>                if (context instanceof AlterableContext) {</div><div>                    AlterableContext alterableContext = (AlterableContext) context;<br><br>                    // CONTEXT IS A DEPENDENTCONTEXTIMPL THAT THROWS</div><div>                    // UnsupportedOperationException FROM ITS DESTROY() METHOD</div><div>                    alterableContext.destroy(bean);</div><div>                    return;</div><div>                } else {</div><div>                    throw BeanLogger.LOG.destroyUnsupported(context);</div><div>                }</div><div>            }</div><div>        }</div><div><br></div><div>        // check if this is a dependent instance</div><div>        CreationalContext&lt;? super T&gt; ctx = getCreationalContext();</div><div>        if (ctx instanceof WeldCreationalContext&lt;?&gt;) {</div><div>            WeldCreationalContext&lt;? super T&gt; weldCtx = cast(ctx);</div><div><br></div><div>            // PROXY REFERENCES ARE KEPT HERE IN A </div><div>            // &quot;dependentInstances&quot; LIST, AND WOULD BE CLEARED HERE</div><div>            // BUT THIS CODE IS NEVER REACHED</div><div>            weldCtx.destroyDependentInstance(instance);</div><div>        }</div><div>    }</div></div><div style="font-size:12.8px"><br></div><div style="font-size:12.8px">Now I wonder, am I doing something wrong (according to the CDI spec), or could this be a bug in the Weld code?</div><div style="font-size:12.8px"><br></div><div style="font-size:12.8px">Kind regards,</div><div style="font-size:12.8px">Arjan Tijms</div></div>
_______________________________________________<br>
cdi-dev mailing list<br>
<a href="mailto:cdi-dev@lists.jboss.org" target="_blank">cdi-dev@lists.jboss.org</a><br>
<a href="https://lists.jboss.org/mailman/listinfo/cdi-dev" rel="noreferrer" target="_blank">https://lists.jboss.org/mailman/listinfo/cdi-dev</a><br>
<br>
Note that for all code provided on this list, the provider licenses the code under the Apache License, Version 2 (<a href="http://www.apache.org/licenses/LICENSE-2.0.html" rel="noreferrer" target="_blank">http://www.apache.org/licenses/LICENSE-2.0.html</a>). For all other ideas provided on this list, the provider waives all patent and other intellectual property rights inherent in such information.</blockquote></div>