我正在尝试将 jpa 与 tomcat 和 Derby 一起使用。但我收到此消息:Caused by: ERROR XSDB6: Another instance of Derby may already booted the database。
我的 persistence.xml 是这样的:
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
<persistence-unit name="contribs" transaction-type="RESOURCE_LOCAL">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties>
<property name="javax.persistence.jdbc.url" value="jdbc:derby:~/contribs;create=true"/>
<property name="javax.persistence.jdbc.password" value="sa"/>
<property name="javax.persistence.jdbc.driver" value="org.apache.derby.jdbc.EmbeddedDriver"/>
<property name="javax.persistence.jdbc.user" value="sa"/>
<property name="eclipselink.ddl-generation" value="create-tables"/>
</properties>
</persistence-unit>
</persistence>
我使用 ServletContextListener,用 @WebListener() 注释。
在 contextInitialized 上,我得到一个 EntityManagerFactory 和一个 EntityManager,然后在 contextDestroyed 上关闭所有。 p>
我在日志中看到 tomcat 调用了我的 ServletContextListener 两次(我不明白为什么),第二次我收到错误消息。
我也使用 Netbeans。
我该如何解决?谢谢。
最佳答案
您是否正在将您的应用程序热重新部署到 Tomcat 中? Tomcat 似乎经常在单独的应用程序类加载器中保持运行的 Web 应用程序的多个副本,并且一次只有一个 Web 应用程序副本能够打开 Derby。
一种可能性是从 Derby 的嵌入式版本切换到客户端-服务器版本。
另一种可能是在出现问题时重新启动 Tomcat。
关于java - 使用 derby/jpa/tomcat,数据库总是已经启动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20253420/