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

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

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

问题

  1. 我有一个使用[Armeria][1]作为Web服务的Java应用程序。当我在主方法中创建Hibernate SessionFactory时,它运行正常。但是我试图在调用特定Http端点时创建SessionFactory。在处理程序方法中无法创建会话工厂。
  2. `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
  1. 关于这个错误,我能找到的所有信息都是JaxBJava 8之后不再提供,但我正在使用Java 8,如果我只是在应用程序启动时创建它,它就可以正常运行。
  2. [1]: https://github.com/line/armeria
  3. <details>
  4. <summary>英文:</summary>
  5. 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
  6. `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
  1. 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.
  2. [1]: https://github.com/line/armeria
  3. </details>
  4. # 答案1
  5. **得分**: 3
  6. 我认为这是某种类路径冲突。在Java 8中,以下代码会因为`ClassNotFoundException: com/sun/xml/bind/v2/ContextFactory`而失败,正如问题中所报告的:
  7. ```lang-java
  8. public class MyService {
  9. @Get("/start")
  10. public HttpResponse start() throws Exception {
  11. final StandardServiceRegistryBuilder registryBuilder =
  12. new StandardServiceRegistryBuilder().configure();
  13. ...
  14. }
  15. }

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

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

  1. @Get("/start")
  2. public HttpResponse start() throws Exception {
  3. Thread.currentThread().setContextClassLoader(MyService.class.getClassLoader());
  4. // ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  5. final StandardServiceRegistryBuilder registryBuilder =
  6. new StandardServiceRegistryBuilder().configure();
  7. ...
  8. }
英文:

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:

  1. public class MyService {
  2. @Get(&quot;/start&quot;)
  3. public HttpResponse start() throws Exception {
  4. final StandardServiceRegistryBuilder registryBuilder =
  5. new StandardServiceRegistryBuilder().configure();
  6. ...
  7. }
  8. }

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:

  1. @Get(&quot;/start&quot;)
  2. public HttpResponse start() throws Exception {
  3. Thread.currentThread().setContextClassLoader(MyService.class.getClassLoader());
  4. // ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  5. final StandardServiceRegistryBuilder registryBuilder =
  6. new StandardServiceRegistryBuilder().configure();
  7. ...
  8. }

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:

确定