【问题标题】:I get a PersistenceException and I cannot understand why我得到一个 PersistenceException 我不明白为什么
【发布时间】:2012-05-01 18:46:05
【问题描述】:

我正在使用 EclipseLink 运行 Web 服务,它运行良好。但有时我会收到异常消息“无法预部署 PersistenceUnit”。我不知道为什么我会得到这个。其他一切似乎都正常,异常似乎并没有干扰应用程序,它只是被捕捉到了。

Exception Description: Unable to predeploy PersistenceUnit [MyLibPU] in invalid state [Undeployed]
javax.persistence.PersistenceException: Exception [EclipseLink-28017] (Eclipse Persistence Services - 2.2.0.v20110202-r8913): org.eclipse.persistence.exceptions.EntityManagerSetupException
Exception Description: Unable to predeploy PersistenceUnit [MyLibPU] in invalid state [Undeployed]
    at org.eclipse.persistence.internal.jpa.EntityManagerSetupImpl.predeploy(EntityManagerSetupImpl.java:956)
    at org.eclipse.persistence.jpa.PersistenceProvider.createEntityManagerFactory(PersistenceProvider.java:121)
    at javax.persistence.Persistence.createEntityManagerFactory(Unknown Source)
    at javax.persistence.Persistence.createEntityManagerFactory(Unknown Source)
....
....
Caused by: Exception [EclipseLink-28017] (Eclipse Persistence Services - 2.2.0.v20110202-r8913): org.eclipse.persistence.exceptions.EntityManagerSetupException
Exception Description: Unable to predeploy PersistenceUnit [MyLibPU] in invalid state [Undeployed]
    at org.eclipse.persistence.exceptions.EntityManagerSetupException.cannotPredeploy(EntityManagerSetupException.java:202)
    ... 30 more

在异常中,我可以找到方法中一行的跟踪:

EntityManagerFactory emf = Persistence.createEntityManagerFactory("MyLibPU");

我为什么会得到这个?

编辑
这是我在将 eclipselink 日志设置为 FINEST 时得到的输出。

[EL Finest]: 2012-04-23 15:19:06.025--ServerSession(2134178364)--Thread(Thread[Finalizer,8,system])--End undeploying Persistence Unit MyLibPU; session file:/home/Rox/MyLib/trunk/MyLib/build/classes/_MyLibPU; state Undeployed; factoryCount 0
[EL Finest]: 2012-04-23 15:19:06.025--ServerSession(2113649746)--Thread(Thread[Finalizer,8,system])--Begin undeploying Persistence Unit MyLibPU; session file:/home/Rox/MyLib/trunk/MyLib/src/_MyLibPU; state Deployed; factoryCount 3
[EL Finest]: 2012-04-23 15:19:06.025--ServerSession(2113649746)--Thread(Thread[Finalizer,8,system])--End undeploying Persistence Unit MyLibPU; session file:/home/Rox/MyLib/trunk/MyLib/src/_MyLibPU; state Deployed; factoryCount 2
Apr 23, 2012 3:19:06 PM com.sun.xml.internal.ws.server.sei.EndpointMethodHandler invoke
SEVERE: Exception [EclipseLink-28017] (Eclipse Persistence Services - 2.2.0.v20110202-r8913): org.eclipse.persistence.exceptions.EntityManagerSetupException
Exception Description: Unable to predeploy PersistenceUnit [MyLibPU] in invalid state [Undeployed]
javax.persistence.PersistenceException: Exception [EclipseLink-28017] (Eclipse Persistence Services - 2.2.0.v20110202-r8913): org.eclipse.persistence.exceptions.EntityManagerSetupException
Exception Description: Unable to predeploy PersistenceUnit [MyLibPU] in invalid state [Undeployed]
    at org.eclipse.persistence.internal.jpa.EntityManagerSetupImpl.predeploy(EntityManagerSetupImpl.java:956)
    at org.eclipse.persistence.jpa.PersistenceProvider.createEntityManagerFactory(PersistenceProvider.java:121)
    at javax.persistence.Persistence.createEntityManagerFactory(Unknown Source)
    at javax.persistence.Persistence.createEntityManagerFactory(Unknown Source)
    at com.mylib.MyImplementation.getPersons(MyImplementation.java:171)
    at sun.reflect.GeneratedMethodAccessor13.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:601)
    at com.sun.xml.internal.ws.api.server.InstanceResolver$1.invoke(InstanceResolver.java:235)
    at com.sun.xml.internal.ws.server.InvokerTube$2.invoke(InvokerTube.java:135)
    at com.sun.xml.internal.ws.server.sei.EndpointMethodHandler.invoke(EndpointMethodHandler.java:246)
    at com.sun.xml.internal.ws.server.sei.SEIInvokerTube.processRequest(SEIInvokerTube.java:85)
    at com.sun.xml.internal.ws.api.pipe.Fiber.__doRun(Fiber.java:626)
    at com.sun.xml.internal.ws.api.pipe.Fiber._doRun(Fiber.java:585)
    at com.sun.xml.internal.ws.api.pipe.Fiber.doRun(Fiber.java:570)
    at com.sun.xml.internal.ws.api.pipe.Fiber.runSync(Fiber.java:467)
    at com.sun.xml.internal.ws.server.WSEndpointImpl$2.process(WSEndpointImpl.java:299)
    at com.sun.xml.internal.ws.transport.http.HttpAdapter$HttpToolkit.handle(HttpAdapter.java:593)
    at com.sun.xml.internal.ws.transport.http.HttpAdapter.handle(HttpAdapter.java:244)
    at com.sun.xml.internal.ws.transport.http.server.WSHttpHandler.handleExchange(WSHttpHandler.java:95)
    at com.sun.xml.internal.ws.transport.http.server.WSHttpHandler.handle(WSHttpHandler.java:80)
    at com.sun.net.httpserver.Filter$Chain.doFilter(Filter.java:77)
    at sun.net.httpserver.AuthFilter.doFilter(AuthFilter.java:83)
    at com.sun.net.httpserver.Filter$Chain.doFilter(Filter.java:80)
    at sun.net.httpserver.ServerImpl$Exchange$LinkHandler.handle(ServerImpl.java:668)
    at com.sun.net.httpserver.Filter$Chain.doFilter(Filter.java:77)
    at sun.net.httpserver.ServerImpl$Exchange.run(ServerImpl.java:640)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
    at java.lang.Thread.run(Thread.java:722)
Caused by: Exception [EclipseLink-28017] (Eclipse Persistence Services - 2.2.0.v20110202-r8913): org.eclipse.persistence.exceptions.EntityManagerSetupException
Exception Description: Unable to predeploy PersistenceUnit [MyLibPU] in invalid state [Undeployed]
    at org.eclipse.persistence.exceptions.EntityManagerSetupException.cannotPredeploy(EntityManagerSetupException.java:202)
    ... 30 more

[EL Finer]: 2012-04-23 15:20:06.003--Thread(Thread[pool-1-thread-61,5,main])--fixUNC: before fixing: url = file:/home/Rox/MyLib/build/classes/, authority = , file = /home/Rox/MyLib/build/classes/ (There is no English translation for this message.)
[EL Finer]: 2012-04-23 15:20:06.012--Thread(Thread[pool-1-thread-61,5,main])--fixUNC: after fixing: url = file:/home.....

编辑 2
我的persistence.xml:

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
  <persistence-unit name="MyLibPU" transaction-type="RESOURCE_LOCAL">
    <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
    <class>com.mylib.Person</class>
    <class>com.mylib.Group</class>
    <properties>
      <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/MY_DB"/>
      <property name="javax.persistence.jdbc.password" value="MyPassword"/>
      <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
      <property name="javax.persistence.jdbc.user" value="root"/>
      <property name="eclipselink.logging.level" value="FINEST"/>;
      <property name="eclipselink.logging.level.sql" value="FINEST"/>;
      <property name="eclipselink.logging.parameters" value="true"/>;
    </properties>
  </persistence-unit>
</persistence>

【问题讨论】:

  • 还有更多例外吗? EclipseLink 在创建持久性单元时遇到错误,因此如果没有更多错误,您需要将日志记录设置为最好以了解更多信息:。稍后获得工厂可能会“工作”,因为大部分单元可能在错误发生之前已经初始化,但没有办法判断什么不是。不应丢弃创建工厂的异常。
  • @Chris:抱歉回复晚了。查看我在上面帖子中的编辑,当 EclipseLink 日志记录设置为 FINEST 时,您将看到输出。您将在我上面的编辑中看到整个异常。你能弄清楚问题是什么吗?我不能。 =/
  • 即使在您编辑之后也应该有更多的堆栈跟踪。检查服务器。以任何方式发布您的 persistence.xml 。那里有些不对劲。甚至可能只是没有被解析的无效 XML。
  • @Shahzeb:没有更多的堆栈跟踪行。我发布了整个异常。查看我在上面帖子中的第二次编辑以查看 persistence.xml。
  • 在此之前发生错误,请检查您的服务器日志。在运行测试之前重新启动服务器。

标签: java exception jakarta-ee jpa eclipselink


【解决方案1】:

如果错误是零星的,这可能意味着这是一个时间问题。此外,显然您在应用程序的生命周期中多次创建工厂(如果我错了,请告诉我)。因此,对您的 Web 应用程序的并发请求可能会触发工厂的并发创建。可能不支持这种情况。

无论如何,只创建一次工厂是好的。请参阅this question 了解如何操作。然后你可以同时在它上面调用createEntityManager——这个方法是线程安全的。

【讨论】:

    【解决方案2】:

    取消部署 Hibernate 存在问题。当您尝试取消部署和重新部署时,您似乎遇到了它们。当你做一个完整的服务器重启时有问题吗?

    编辑:抱歉,您明确表示您使用的是 EclipseLink,不知道为什么我认为您使用的是 Hibernate。

    无论如何,您日志中的这一行令人担忧:

    [EL Finest]: 2012-04-23 15:19:06.025--ServerSession(2113649746)--Thread(Thread[Finalizer,8,system])--Begin undeploying Persistence Unit MyLibPU; session file:/home/Rox/MyLib/trunk/MyLib/src/_MyLibPU; state Deployed; factoryCount 3
    

    我不确定factoryCount 是什么意思,但我不喜欢它 > 1。不过,最令人担忧的是“开始取消部署 Persistence Unit MyLibPU”。为什么要取消部署 PU?我认为这是因为您正在取消部署该应用程序,但在您的 cmets 中您说“错误可能在应用程序生命周期内的任何时间发生,并且每 24 小时仅发生 2-5 次。”在这种情况下,您需要弄清楚是什么触发了取消部署。

    【讨论】:

    • 我可以检查并返回答案。但我没有运行 Hibernate。我正在运行 EclipseLink。错误可能在应用程序的生命周期内随时发生,每 24 小时仅发生 2-5 次,不会造成任何麻烦或其他问题。它们只是发生。 :-)
    【解决方案3】:

    在使用数据库时不要使用静态构造函数或类似的构造。这可能会避免麻烦。尝试在您的代码中插入暂停和显式日志命令以查找错误以了解您的代码走了多远。

    【讨论】:

      猜你喜欢
      • 2013-11-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多