users@jaxb.java.net

Re: XmlAdapter lifetime/concurrency/thread-safety

From: Taras Tielkes <taras.tielkes_at_gmail.com>
Date: Tue, 3 Dec 2013 11:17:31 +0100

Hi Iaroslav,

The thread-safety of JAXBContext, and the lack of thread-safety of
Marshaller/Unmarshaller is well-known (and indeed documented).

My question however is about the XmlAdapter objects used during the
(un)marshall process. Does the JAXB runtime create one instance per
XmlAdapter type, and re-use it across threads, or does it create a new
instance per marshall/unmarshall operation. It's not documented how this
works, while it would have implications of thread-safety.

Kind regards,
-tt



On Tue, Dec 3, 2013 at 10:36 AM, Iaroslav Savytskyi <
iaroslav.savytskyi_at_oracle.com> wrote:

> Hi, Taras,
>
> Here is some info:
> https://jaxb.java.net/guide/Performance_and_thread_safety.html
>
> As you see you should have additional synchronisation.
>
> —
> Best regards,
> Iaroslav
>
> On 2 Dec 2013, at 21:13, Taras Tielkes <taras.tielkes_at_gmail.com> wrote:
>
> > Hi,
> >
> > A common use case for XmlAdapter is customization of the way numbers and
> dates are marshalled. Since many of the underlying classes implementing
> this in the JDK (NumberFormat, SimpleDateFormat) are not thread-safe, I'm
> wondering if it is safe to use them without additional synchronization from
> a JAXB XmlAdapter.
> >
> > If the JAXB runtime is caching and reusing (in a concurrent fashion)
> XmlAdapter instances, then obviously additional care is required. On the
> other hand, if the lifetime of an XmlAdapter instance is limited to a
> single marshall()/unmarshall() operation, then this seems to be perfectly
> safe.
> >
> > Unfortunately, as far as I can see, the JAXB specification does not
> provide an explicit contract for the lifetime of XmlAdapter instances.
> Neither did I see any remarks regarding the need for thread-safety.
> > Did I miss something in my reading of the specification, or is this
> effectively unspecified?
> >
> > Thanks,
> > -tt
> >
>
>