H2 内存数据库.未找到表 [英] H2 in-memory database. Table not found
问题描述
我有一个带有 URL "jdbc:h2:test"
的 H2 数据库.我使用 CREATE TABLE PERSON (ID INT PRIMARY KEY, FIRSTNAME VARCHAR(64), LASTNAME VARCHAR(64));
创建了一个表.然后我使用 SELECT * FROM PERSON
从这个(空)表中选择所有内容.到目前为止,一切都很好.
I've got a H2 database with URL "jdbc:h2:test"
. I create a table using CREATE TABLE PERSON (ID INT PRIMARY KEY, FIRSTNAME VARCHAR(64), LASTNAME VARCHAR(64));
. I then select everything from this (empty) table using SELECT * FROM PERSON
. So far, so good.
但是,如果我将 URL 更改为 "jdbc:h2:mem:test"
,唯一的区别是数据库现在仅在内存中,这给了我一个 org.h2.jdbc.JdbcSQLException:找不到表PERSON";SQL 语句:SELECT * FROM PERSON [42102-154]
.我可能在这里遗漏了一些简单的东西,但任何帮助将不胜感激.
However, if I change the URL to "jdbc:h2:mem:test"
, the only difference being the database is now in memory only, this gives me an org.h2.jdbc.JdbcSQLException: Table "PERSON" not found; SQL statement: SELECT * FROM PERSON [42102-154]
. I'm probably missing something simple here, but any help would be appreciated.
推荐答案
DB_CLOSE_DELAY=-1
hbm2ddl 在创建表后关闭连接,因此 h2 丢弃它.
DB_CLOSE_DELAY=-1
hbm2ddl closes the connection after creating the table, so h2 discards it.
如果你的连接 URL 是这样配置的
If you have your connection-url configured like this
jdbc:h2:mem:test
最后一个连接关闭时数据库的内容丢失了.
the content of the database is lost at the moment the last connection is closed.
如果你想保留你的内容,你必须像这样配置网址
If you want to keep your content you have to configure the url like this
jdbc:h2:mem:test;DB_CLOSE_DELAY=-1
如果这样做,只要 vm 存在,h2 就会保留其内容.
If doing so, h2 will keep its content as long as the vm lives.
注意分号 (;
) 而不是冒号 (:
).
Notice the semicolon (;
) rather than colon (:
).
请参阅内存数据库部分功能页面.引用:
默认情况下,关闭与数据库的最后一个连接会关闭数据库.对于内存数据库,这意味着内容丢失.要保持数据库打开,请将 ;DB_CLOSE_DELAY=-1
添加到数据库 URL.要在虚拟机处于活动状态时保留内存数据库的内容,请使用 jdbc:h2:mem:test;DB_CLOSE_DELAY=-1
.
By default, closing the last connection to a database closes the database. For an in-memory database, this means the content is lost. To keep the database open, add
;DB_CLOSE_DELAY=-1
to the database URL. To keep the content of an in-memory database as long as the virtual machine is alive, usejdbc:h2:mem:test;DB_CLOSE_DELAY=-1
.
这篇关于H2 内存数据库.未找到表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!