cxf-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Rex Wang <rwo...@gmail.com>
Subject Re: SoftReference as jaxb context cache?
Date Tue, 16 Apr 2013 08:28:33 GMT
Hi Dan,
I met the nullpointer issue again, after some investigation, this is
another problem about GC, for example:
1public Context abc(){
2 Context context = new Context();
3 WeakReference<Context> weakcontext = new WeakReference<Context>(context);
4 ...  <-- nothing about context
5 ...  <-- nothing about context
6 ...  <-- nothing about context
7 return weakcontext.get();    <-- might retrun null
8}
I consult some GC experts about this, and if there is a GC after line 3
context will be collected and the WeakReference get() method will return
NULL.  This is correct behavior and completely valid with the Java Spec.

So I reopened CXF-4947, and upload a new patch CXF-4947-2.patch, the main
changes are:
1. add a new construct method in CachedContextAndSchemas:
  private CachedContextAndSchemas(JAXBContext context, Set<Class<?>>
classes, CachedContextAndSchemasInternal i)
and then in getCachedContextAndSchemas()
  return new CachedContextAndSchemas(context, classes,
cachedContextAndSchemasInternal);
so that we can hold the context strong reference until the method is
returned.
2. rename all local variable cachedContextAndSchemas to
cachedContextAndSchemasInternal in getCachedContextAndSchemas()
so that this part of code is more readable..

could you please review?

Rex



2013/4/5 Daniel Kulp <dkulp@apache.org>

>
> On Apr 4, 2013, at 12:24 PM, Rex Wang <rwonly@gmail.com> wrote:
>
> Hi Dan,
> Thank you for fixing it very quickly, very smart wrapper!
>
> I would suggest:
> *public *static final class CachedContextAndSchemasInternal   --> *private
> *static final class CachedContextAndSchemasInternal
>
>
> Fixed (along with a  couple other minor cleanups).   This was accidentally
> left over from all the experiments I was doing.  I originally was playing
> around with having the "internal" one subclass the external one, then
> flipped that the other way, then decided to do the wrapper thing.  Wrapper
> thing really seemed to work best with pretty much no changes to the rest of
> the CXF code.  (I did go through all the calls to make sure none of them
> were holding onto things unexpectedly though.)
>
> Dan
>
>
>
> I still opened a JIRA CXF-4947<https://issues.apache.org/jira/browse/CXF-4947>to
record this issue so that I can get a numbered patch to help merge to my
> runtime, and I would like suggest CXF community do this for every defect
> changeset..
>
>
>
>
> 2013/4/4 Daniel Kulp <dkulp@apache.org>
>
>>
>> I think this is now fixed.
>>
>> Dan
>>
>>
>>
>> On Apr 3, 2013, at 1:29 PM, Daniel Kulp <dkulp@apache.org> wrote:
>>
>> >
>> > On Apr 3, 2013, at 3:48 AM, Rex Wang <rwonly@gmail.com> wrote:
>> >
>> >> hi devs,
>> >> I found some intermittent NullpointerException, and it is very hard to
>> >> reproduce but I did have met a lot of times, and it is thrown from
>> >> different places from runtime:
>> >> Stack Dump = java.lang.NullPointerException
>> >>   at
>> >>
>> org.apache.cxf.common.jaxb.JAXBUtils.generateJaxbSchemas(JAXBUtils.java:765)
>> >>   at
>> >>
>> org.apache.cxf.jaxb.JAXBDataBinding.generateJaxbSchemas(JAXBDataBinding.java:440)
>> >>   at
>> >>
>> org.apache.cxf.jaxb.JAXBDataBinding.initialize(JAXBDataBinding.java:363)
>> >>   at
>> >>
>> org.apache.cxf.service.factory.AbstractServiceFactoryBean.initializeDataBindings(AbstractServiceFactoryBean.java:86)
>> >>   at
>> >>
>> org.apache.cxf.service.factory.ReflectionServiceFactoryBean.buildServiceFromClass(ReflectionServiceFactoryBean.java:469)
>> >>   at
>> >>
>> org.apache.cxf.jaxws.support.JaxWsServiceFactoryBean.buildServiceFromClass(JaxWsServiceFactoryBean.java:690)
>> >>   at
>> >>
>> org.apache.cxf.service.factory.ReflectionServiceFactoryBean.initializeServiceModel(ReflectionServiceFactoryBean.java:531)
>> >>   at
>> >>
>> org.apache.cxf.service.factory.ReflectionServiceFactoryBean.create(ReflectionServiceFactoryBean.java:247)
>> >>
>> >> After checking the codes, they are all about the jaxbcontext is null.
>> >> From the JAXBContextCache.CachedContextAndSchemas, I notice this line:
>> >> private WeakReference<JAXBContext> context;
>> >>
>> >> IMHO, WeakReference is not a good choice to do cache, it could be gc
>> in any
>> >> time. For example,
>> >> JAXBContextCache.getCachedContextAndSchemas(..)
>> >> <gc>
>> >> ctx = cachedContextAndSchemas.getContext()    <-- then the ctx will be
>> null
>> >> here
>> >>
>> >> So I guess this is the root cause of the intermittent issue. Any
>> insights
>> >> or stories about the current design? Can we change it to SoftReference?
>> >
>> >
>> > A SoftReference wouldn't really change anything.  It would make the
>> issue occur less often, but it could still be garbage collected at any time.
>> >
>> > There's definitely an issue here though.  I'll need to think about this
>> some more.  Not sure on the best fix right now.   Hmm…..
>> >
>> >
>> > --
>> > Daniel Kulp
>> > dkulp@apache.org - http://dankulp.com/blog
>> > Talend Community Coder - http://coders.talend.com
>> >
>>
>> --
>> Daniel Kulp
>> dkulp@apache.org - http://dankulp.com/blog
>> Talend Community Coder - http://coders.talend.com
>>
>>
>
>
> -
> Lei Wang (Rex)
> rwonly AT apache.org
>
>
> --
> Daniel Kulp
> dkulp@apache.org - http://dankulp.com/blog
> Talend Community Coder - http://coders.talend.com
>
>


-- 
Lei Wang (Rex)
rwonly AT apache.org

Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message