我有一个H2数据库的URL“jdbc: H2:test”。我使用create table PERSON (ID INT主键,名字VARCHAR(64),姓VARCHAR(64));;然后使用select * from PERSON从这个(空)表中选择所有内容。到目前为止,一切顺利。

然而,如果我将URL更改为“jdbc:h2:mem:test”,唯一的区别是数据库现在只在内存中,这给了我一个org.h2.jdbc。JdbcSQLException:表“PERSON”未找到;SQL语句:SELECT * FROM PERSON[42102-154]。我可能错过了一些简单的东西,但任何帮助将不胜感激。


当前回答

一个原因可能是jpa试图在创建表结构之前插入数据,为了解决这个问题,在应用程序中插入这一行。属性:

spring.jpa.defer-datasource-initialization=true

其他回答

DB_CLOSE_DELAY = 1

Hbm2ddl在创建表后关闭连接,因此h2将丢弃它。

如果您像这样配置了connection-url

jdbc:h2:mem:test

在关闭最后一个连接时,数据库的内容将丢失。

如果你想保留你的内容,你必须这样配置url

jdbc:h2:mem:test;DB_CLOSE_DELAY=-1

如果这样做,h2将保留它的内容,只要vm存在。

注意分号(;)而不是冒号(:)。

请参阅功能页面的内存数据库部分。引用:

默认情况下,关闭到数据库的最后一个连接将关闭数据库。对于内存中的数据库,这意味着内容丢失。要保持数据库打开,在数据库URL中添加;DB_CLOSE_DELAY=-1。要在虚拟机处于活动状态时保持内存中数据库的内容,请使用jdbc:h2:mem:test;DB_CLOSE_DELAY=-1。

我也遇到了同样的问题,并在应用程序测试中更改了配置。属性:

#Test Properties
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.url=jdbc:h2:mem:testdb;DB_CLOSE_DELAY=-1
spring.datasource.username=sa
spring.datasource.password=
spring.jpa.hibernate.ddl-auto=create-drop

和我的依赖:

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>

    <!-- https://mvnrepository.com/artifact/com.h2database/h2 -->
    <dependency>
        <groupId>com.h2database</groupId>
        <artifactId>h2</artifactId>
        <version>1.4.198</version>
        <scope>test</scope>
    </dependency>

以及测试类上使用的注释:

@RunWith(SpringRunner.class)
@DataJpaTest
@ActiveProfiles("test")
public class CommentServicesIntegrationTests {
...
}

H2内存数据库将数据存储在JVM内部的内存中。当JVM退出时,这些数据就丢失了。

我怀疑您正在做的事情与下面的两个Java类类似。其中一个类创建了一个表,而另一个类尝试插入表:

import java.sql.*;

public class CreateTable {
    public static void main(String[] args) throws Exception {
        DriverManager.registerDriver(new org.h2.Driver());
        Connection c = DriverManager.getConnection("jdbc:h2:mem:test");
        PreparedStatement stmt = c.prepareStatement("CREATE TABLE PERSON (ID INT PRIMARY KEY, FIRSTNAME VARCHAR(64), LASTNAME VARCHAR(64))");
        stmt.execute();
        stmt.close();
        c.close();
    }
}

and

import java.sql.*;

public class InsertIntoTable {
    public static void main(String[] args) throws Exception {
        DriverManager.registerDriver(new org.h2.Driver());
        Connection c = DriverManager.getConnection("jdbc:h2:mem:test");
        PreparedStatement stmt = c.prepareStatement("INSERT INTO PERSON (ID, FIRSTNAME, LASTNAME) VALUES (1, 'John', 'Doe')");
        stmt.execute();
        stmt.close();
        c.close();
    }
}

当我一个接一个地运行这些类时,我得到了以下输出:

C:\Users\Luke\stuff>java CreateTable

C:\Users\Luke\stuff>java InsertIntoTable
Exception in thread "main" org.h2.jdbc.JdbcSQLException: Table "PERSON" not found; SQL statement:
INSERT INTO PERSON (ID, FIRSTNAME, LASTNAME) VALUES (1, 'John', 'Doe') [42102-154]
        at org.h2.message.DbException.getJdbcSQLException(DbException.java:327)
        at org.h2.message.DbException.get(DbException.java:167)
        at org.h2.message.DbException.get(DbException.java:144)
        ...

一旦第一个java进程退出,由CreateTable创建的表就不再存在。因此,当InsertIntoTable类出现时,没有供它插入的表。

当我将连接字符串更改为jdbc:h2:test时,我发现没有这样的错误。我还发现出现了一个文件test.h2.db。这是H2放置表的位置,因为它存储在磁盘上,所以表仍然在那里,供InsertIntoTable类查找。

在我的情况下,缺少表错误发生在jpa测试期间,表是由方案创建的。SQL文件,问题在放置@org.springframework.transaction.annotation后被修复。测试中的事务性

<bean id="benchmarkDataSource"
    class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="org.h2.Driver" />
    <property name="url" value="jdbc:h2:mem:testdb;DB_CLOSE_DELAY=-1" />
    <property name="username" value="sa" />
    <property name="password" value="" />
</bean>