Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
[eclipselink-users] Is XMLContext really threadsafe?

From javadoc:

 *  <p>The XMLContext is thread-safe.  If multiple threads accessing the same XMLContext object
 *  request an XMLMarshaller, each will receive their own instance of XMLMarshaller, so any
 *  state that the XMLMarshaller maintains will be unique to that process.  The same is true
 *  of instances of XMLUnmarshaller and XMLValidator.

Our mappings use a XMLObjectReferenceMapping like this:

    public XMLDescriptor createOperationReferenceDescriptor() {
        return this.createReferenceDescriptor(OperationReference.class);

    protected XMLDescriptor createReferenceDescriptor(final Class<? extends Reference> type) {
        XMLDescriptor reference = this.buildDescriptor(type);

        XMLObjectReferenceMapping object = new XMLObjectReferenceMapping();
        object.addSourceToTargetKeyFieldAssociation("@xlink:href", "@gml:id");

        return reference;

We then create a project:

Project project = new Project(new XMLLogin());

Add descriptors (we have implemented some annotations to make it possible to scan for different sets of descriptors at runtime, as seen above with the @MapperDescriptor).

for (ClassDescriptor classDescriptor : this.scanForDescriptors()) {

And return a XMLContext.

return new XMLContext(project);

Any attempt to use this XMLContext across threads will fail, since the org.eclipse.persistence.internal.oxm.ReferenceResolver used by the XMLObjectReferenceMapping is shared across threads! This means that thread1 might resolve to objects use by the referenceresolver in thread2. And it does...

XMLMarshaller m1 = xmlContext.createMarshaller();
XMLMarshaller m2 = xmlContext.createMarshaller();

m2 and m2 will now reference the same ReferenceResolver.

This does not sound like thread-safe to me. Is this behaviour a bug, am I doing somethings dumb while creating Projects and XMLContext, or is the XMLContext simply not thread safe?

As a workaround, I tried to create a new XMLContext from the same Project, but that didn't work either, since the ClassDescriptors configured initially with the Project had changed at the next XMLContext call. XMLContext(projects) seems to be possible only once for each project instance.

We ended up with creating a new Project, XMLContext and XMLMarshaller each time a thread requests a marshaller, and that works, but takes time...


 /Magnus Heino

Back to the top