StringWriter sw = new StringWriter(); JAXBContext jaxbContext = JAXBContext.newInstance(myObject.getClass()); Marshaller marshal = jaxbContext.createMarshaller(); marshal.marshal(myObject, sw);
The unfortunate thing is that this is exactly what one would come up with after skimming the examples in the javadoc.
The recommended solution on the internets is to cache the JAXBContext, including re-using it on multiple threads. This is a bit novel as the documentation for the JAXBContext doesn't indicate this will happen and it also doesn't indicate the JAXBContext is thread-safe. To discover that you have to go into specific implementation information, such as https://jaxb.dev.java.net/guide/Performance_and_thread_safety.html.
These details vary by implementation so one might legitimately argue the javase documentation is correct. It's really just aggravating to discover that the implementation is built in a way so likely to cause clients to leak. This strikes me as a great example of a very bad implementation of an API, especially given that the implementation could easily maintain a lookup and avoid re-creating the dynamic classes and creating the leak for us.
3 comments:
Performance and thread-safety
http://jaxb.java.net/guide/Performance_and_thread_safety.html
Online Stock Broker is an informational guide to help you find the right online trader in order to maximize your trading experience. Our search results are sorted by price, regulation and client score. We ensure that all services displayed on Online Stock Broker are highly rated and fully regulated.
"Great post! The detailed explanation of the issues surrounding JAXB and how it interacts with Scala is really insightful. It's interesting to see how the nuances of Java’s XML binding framework can cause problems when transitioning to a more modern language like Scala. I particularly appreciate the way you've broken down the leaking of JAXB and how developers can address it. Looking forward to more posts on Scala and interoperability challenges!"
Digital Marketing Course In Ameerpet
Post a Comment