Jetty 上 H2/Hibernate JPA 的 Hikari 池 - SQL 错误 TABLE 不存在
Hikari pool for H2/Hibernate JPA on Jetty - SQL errors TABLE does not exist
我正在使用 Hikari 在 Jetty 9 服务器中为 Web 应用汇集 H2 内存连接。
效果很好,直到我离开几个小时让它闲置。当我回来时,我所有的数据库查询都告诉我 table 在我重新加载页面时不存在。我不确定这是哪里出了问题,我假设连接仍然很好,因为它正在尝试 运行 查询,但是出现了奇怪的错误。
这是我的设置:
码头-env.xml:
<Configure id="wac" class="org.eclipse.jetty.webapp.WebAppContext">
<New id="DSTest" class="org.eclipse.jetty.plus.jndi.Resource">
<Arg>jdbc/myds</Arg>
<Arg>
<New class="com.zaxxer.hikari.HikariDataSource">
<Arg>
<New class="com.zaxxer.hikari.HikariConfig">
<Set type="int" name="maximumPoolSize">1</Set>
<Set name="dataSourceClassName">
org.h2.jdbcx.JdbcDataSource-
</Set>
<Call name="addDataSourceProperty">
<Arg>url</Arg>
<Arg>jdbc:h2:mem:test_mem</Arg>
</Call>
</New>
</Arg>
</New>
</Arg>
</New>
</Configure>
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="MyJPAJAXRS" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<non-jta-data-source>jdbc/myds</non-jta-data-source>
<class>net.b.Configuration</class>
<class>net.b.Form</class>
<class>net.b.FormValue</class>
<class>net.b.Translation</class>
<properties>
<property name="javax.persistence.schema-generation.database.action"
value="drop-and-create"/>
<property name="javax.persistence.sql-load-script-source" value="META-INF/seed.sql"/>
</properties>
</persistence-unit>
</persistence>
<!--<property name="javax.persistence.jdbc.driver" value="org.h2.Driver"/>-->
<!--<property name="javax.persistence.jdbc.url" value="jdbc:h2:mem:test_mem"/>-->
<!--<property name="javax.persistence.schema-generation.create-source"-->
<!--value="metadata-then-script"/>-->
示例查询:
TypedQuery<String> query = em.createQuery(
"SELECT t.value FROM Translation t WHERE t.key = :key", String.class);
query.setParameter("key", key);
result = query.getSingleResult();
这个 REST 端点将工作 100 次,但在空闲时间后我得到异常 table "TRANSLATION" 不存在。给出了什么?
您的答案可以在这里找到 here。需要使用jdbc:h2:mem:test;DB_CLOSE_DELAY=-1
,否则一旦最后一个连接关闭,数据库和表就会消失。
我正在使用 Hikari 在 Jetty 9 服务器中为 Web 应用汇集 H2 内存连接。
效果很好,直到我离开几个小时让它闲置。当我回来时,我所有的数据库查询都告诉我 table 在我重新加载页面时不存在。我不确定这是哪里出了问题,我假设连接仍然很好,因为它正在尝试 运行 查询,但是出现了奇怪的错误。
这是我的设置:
码头-env.xml:
<Configure id="wac" class="org.eclipse.jetty.webapp.WebAppContext">
<New id="DSTest" class="org.eclipse.jetty.plus.jndi.Resource">
<Arg>jdbc/myds</Arg>
<Arg>
<New class="com.zaxxer.hikari.HikariDataSource">
<Arg>
<New class="com.zaxxer.hikari.HikariConfig">
<Set type="int" name="maximumPoolSize">1</Set>
<Set name="dataSourceClassName">
org.h2.jdbcx.JdbcDataSource-
</Set>
<Call name="addDataSourceProperty">
<Arg>url</Arg>
<Arg>jdbc:h2:mem:test_mem</Arg>
</Call>
</New>
</Arg>
</New>
</Arg>
</New>
</Configure>
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="MyJPAJAXRS" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<non-jta-data-source>jdbc/myds</non-jta-data-source>
<class>net.b.Configuration</class>
<class>net.b.Form</class>
<class>net.b.FormValue</class>
<class>net.b.Translation</class>
<properties>
<property name="javax.persistence.schema-generation.database.action"
value="drop-and-create"/>
<property name="javax.persistence.sql-load-script-source" value="META-INF/seed.sql"/>
</properties>
</persistence-unit>
</persistence>
<!--<property name="javax.persistence.jdbc.driver" value="org.h2.Driver"/>-->
<!--<property name="javax.persistence.jdbc.url" value="jdbc:h2:mem:test_mem"/>-->
<!--<property name="javax.persistence.schema-generation.create-source"-->
<!--value="metadata-then-script"/>-->
示例查询:
TypedQuery<String> query = em.createQuery(
"SELECT t.value FROM Translation t WHERE t.key = :key", String.class);
query.setParameter("key", key);
result = query.getSingleResult();
这个 REST 端点将工作 100 次,但在空闲时间后我得到异常 table "TRANSLATION" 不存在。给出了什么?
您的答案可以在这里找到 here。需要使用jdbc:h2:mem:test;DB_CLOSE_DELAY=-1
,否则一旦最后一个连接关闭,数据库和表就会消失。