Search code examples
javahibernatearmeria

Bootstrap Hibernate SessionFactory in Netty/Armeria handler method fails with ClassNotFoundException


I have a Java Application that uses Armeria for a Web Service. When I create my Hibernate SessionFactory in the main method it works fine. But I am trying to create the SessionFactory when a certain Http Endpoint is called. In the handler method the session factory can not be created Exception in thread "Thread-1" org.hibernate.internal.util.config.ConfigurationException: Unable to perform unmarshalling at line number 0 and column 0 in RESOURCE hibernate.cfg.xml. Message: null

Caused by: javax.xml.bind.JAXBException
 - with linked exception:
[java.lang.ClassNotFoundException: com/sun/xml/bind/v2/ContextFactory]
    at javax.xml.bind.ContextFinder.newInstance(ContextFinder.java:226)
    at javax.xml.bind.ContextFinder.find(ContextFinder.java:441)
    at javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:641)
    at javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:584)
    at org.hibernate.boot.cfgxml.internal.JaxbCfgProcessor.unmarshal(JaxbCfgProcessor.java:122)
    ... 17 more
Caused by: java.lang.ClassNotFoundException: com/sun/xml/bind/v2/ContextFactory
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:264)
    at javax.xml.bind.ContextFinder.safeLoadClass(ContextFinder.java:577)
    at javax.xml.bind.ContextFinder.newInstance(ContextFinder.java:224)
    ... 21 more

All I could find about this error is that JaxB is not provided for Java > 8 but i am using Java 8 and it works fine if I just create it at Application launch.


Solution

  • I believe it's some sort of class path conflict. In Java 8, the following code fails with ClassNotFoundException: com/sun/xml/bind/v2/ContextFactory, as reported in the question:

    public class MyService {
        @Get("/start")
        public HttpResponse start() throws Exception {
            final StandardServiceRegistryBuilder registryBuilder =
                new StandardServiceRegistryBuilder().configure();
            ...
        }
    }
    

    However, the problem goes away after upgrading to a newer Java version, such as Java 11.

    Fortunately, the problem can be worked around by specifying the context class loader explicitly:

        @Get("/start")
        public HttpResponse start() throws Exception {
            Thread.currentThread().setContextClassLoader(MyService.class.getClassLoader());
            // ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
            final StandardServiceRegistryBuilder registryBuilder =
                new StandardServiceRegistryBuilder().configure();
            ...
        }