2

I have web application, where I have classes generated from xsd schema. I am creating one Marshaller instance and one Unmarshaller instance as static final fields on application startup.

I want to know whether this is good practice, or it is better to create independent instances per need ?

Yoh0xFF
  • 1,450
  • 2
  • 18
  • 31

1 Answers1

5

Caching Marschaller and Unmarshaller is not a good idea. Cache the JAXBContext instead.

JAXBContext may be reused to create marshallers and unmarshallers.

Marshaller and Unmarshaller instances are NOT (necessarily) thread-safe or reusable.

I'll look for the references and update the answer.

Here's the reference:

The JAXBContext class is thread safe, but the Marshaller, Unmarshaller, and Validator classes are not thread safe.

Related answer:

JAXB creating context and marshallers cost

Community
  • 1
  • 1
lexicore
  • 42,748
  • 17
  • 132
  • 221