H2数据库仅在文件中无法在内存中工作 [英] H2 database doesn't work in memory only with file

查看:48
本文介绍了H2数据库仅在文件中无法在内存中工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个与Hibernate一起建立的Spring MVC项目,想要为某些服务创建一些测试.主应用程序使用PostgreSQL数据库,而对于测试我想在内存数据库中使用H2

I have a Spring MVC project set up with Hibernate and wanted to create some tests for some Services. The main application uses a PostgreSQL database and for tests I want to use H2 in memory database

我为测试创建了单独的配置文件(Spring和Hibernate)一切正常,直到我尝试访问内存数据库

I created separate configuration files for the tests (both Spring and Hibernate) Everything works well until I try to hit the in memory database

休眠配置:

@Configuration
@EnableTransactionManagement
@EnableJpaRepositories("<repository>")
public class DataSourceTestConfig {

private static final Logger LOG = LogManager.getLogger(DataSourceTestConfig.class);

private static final String DATABASE_DRIVER = "org.h2.Driver";
//private static final String DATABASE_URL = "jdbc:h2:file:d:/test";
private static final String DATABASE_URL = "jdbc:h2:mem:test";
private static final String DATABASE_USERNAME = "sa";
private static final String DATABASE_PASSWORD = "";

private static final String HIBERNATE_DIALECT = "org.hibernate.dialect.H2Dialect";
private static final String HIBERNATE_SHOW_SQL = "true";
private static final String ENTITYMANAGER_PACKAGES_TO_SCAN = "<packages>";
private static final String HIBERNATE_HBM2DDL_AUTO = "create";
...

问题似乎出在DATABASE_URL:

The problem seems to be with the DATABASE_URL:

如果我使用:

DATABASE_URL = "jdbc:h2:file:d:/test";

一切正常.所有测试均应按需运行

Everything works as expected. All tests run as they should

如果我使用:

DATABASE_URL = "jdbc:h2:mem:test";

所有的地狱都崩溃了,它不起作用:)

All Hell breaks loose and it does not work :)

在这种情况下,我得到

 org.h2.jdbc.JdbcSQLException: Table "test" not found; SQL statement: ...

浏览Hibernate日志,可以清楚地看到该表实际上是生成的:

Looking through the Hibernate logs it can be clearly seen that the table is in fact generated:

 DEBUG org.hibernate.SQL - drop table test if exists
 ...
 DEBUG org.hibernate.SQL - create table test (<columns>)
 ...
 DEBUG org.hibernate.SQL - alter table test add constraint FKg74a38x6t762qifuge9cux03i foreign key ...

依此类推...

在我看来,在这种情况下,数据库已生成,并且以某种方式我正在工作或在其他实例上工作,或者发生了某些事情,并且在创建和测试之间删除了表.

To me it looks like in this case the database is generated and somehow I am working or on a different instance or something happens and the tables are dropped between the creation and my tests.

在我看来,这些情况似乎不太可能,因为没有Hibernate日志来表明这一点(无丢弃查询),测试日志在创建日志后立即开始

These scenarios seem to me unlikely as there are no Hibernate logs to indicate this (no drop queries) the test logs start immediately after the creation logs

我在这里发现了类似的问题:

I found this similar problem here: H2 in-mem-DB with hibernate set to create giving me table not found errors

但是解决方案是使用文件.

But the solution there is to use a file.

这不是我的解决方案,因为这些测试必须在运行不同操作系统的许多计算机上执行,因此硬编码路径文件不是一个选择.同样,JDBC不允许相对路径,因此我可以将数据库文件放在资源文件夹中.

This is not a solution for me because these tests have to be performed on a lot of machines running different operating systems so a hard coded path file is not an option. Also JDBC doesn't permit a relative path so that I can put a database file in the resources folder.

有人知道如何解决此问题吗?

Does anyone have an idea how to fix this?

谢谢.

推荐答案

我遇到了同样的问题,并最终找到了解决方案:这是因为在初始化数据库之后,它再次关闭了.但是,使用标准设置,这将导致删除数据库.在测试中再次连接相同的URL时,您将拥有一个新"实例,其中包含所有已创建的表.

I faced the same issue and eventually found a solution: It's because the after the database has been initialized it is closed again. However with the standard settings this causes the DB to be deleted. When the same URL is connected again in your tests you will have a "fresh" instance withour any tables created.

您可以通过在JDBC URL的末尾附加; DB_CLOSE_DELAY = -1"来防止关闭数据库,例如

You can prevent the DB being closed by appending ";DB_CLOSE_DELAY=-1" at the end of your your JDBC URL, e.g.

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

H2文档提供了有关这些设置的更多详细信息.

The H2 documentation gives more details on these settings.

这篇关于H2数据库仅在文件中无法在内存中工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆