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

huangapple go评论87阅读模式
英文:

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

问题

我有一个使用[Armeria][1]作为Web服务的Java应用程序。当我在主方法中创建Hibernate SessionFactory时,它运行正常。但是我试图在调用特定Http端点时创建SessionFactory。在处理程序方法中无法创建会话工厂。
`Exception in thread "Thread-1" org.hibernate.internal.util.config.ConfigurationException: 无法在资源hibernate.cfg.xml的行号0和列号0执行解组。消息:null`

由于: javax.xml.bind.JAXBException

  • 附带链接的异常:
    [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
    由于: 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

关于这个错误,我能找到的所有信息都是JaxB在Java 8之后不再提供,但我正在使用Java 8,如果我只是在应用程序启动时创建它,它就可以正常运行。

  [1]: https://github.com/line/armeria


<details>
<summary>英文:</summary>

I have a Java Application that uses [Armeria][1] 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 &quot;Thread-1&quot; 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 &gt; 8 but i am using Java 8 and it works fine if I just create it at Application launch.

  [1]: https://github.com/line/armeria

</details>


# 答案1
**得分**: 3

我认为这是某种类路径冲突。在Java 8中,以下代码会因为`ClassNotFoundException: com/sun/xml/bind/v2/ContextFactory`而失败,正如问题中所报告的:

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

然而,在升级到较新的Java版本(例如Java 11)后,问题就消失了。

幸运的是,可以通过显式地指定上下文类加载器来解决这个问题:

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

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(&quot;/start&quot;)
    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(&quot;/start&quot;)
    public HttpResponse start() throws Exception {
        Thread.currentThread().setContextClassLoader(MyService.class.getClassLoader());
        // ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
        final StandardServiceRegistryBuilder registryBuilder =
            new StandardServiceRegistryBuilder().configure();
        ...
    }

huangapple
  • 本文由 发表于 2020年3月15日 18:42:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/60692021.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定