从Java启动和使用dbunit时与H2的连接问题

时间:2014-11-25 08:10:39

标签: java junit h2 in-memory-database

我想从内存数据库中的JUnit测试开始并用数据填充它。 当我通过h2.bat启动服务器时,一切都适用于我的JUnit测试应用程序。

但是当我尝试从Java启动服务器时,我遇到了某种连接问题 (当我尝试通过webinterface连接到数据库时,我得到了gui,但是它的功能缺失/错误)

在@BeforeClass设置中启动服务器的代码:

args = new String[] { "-tcpPort", "8082","-tcpAllowOthers"};
h2Server = Server.createTcpServer(args).start();
// i also tried the createWebServer but it didn't make any difference

打开连接一次的代码:

String url = "jdbc:h2:tcp://localhost:8082/mem:db1";
h2Connection = DriverManager.getConnection(url,"SA","");

由于某种原因,我要么没有与数据库的连接,但更有可能存在某种连接但是,就像我说得到一些不应该的连接问题。

我如何尝试实现与从cmdline运行h2.bat时相同的功能? 我在createserver上使用了错误的参数吗?

更新1:
我发现了这个问题。似乎使用dbUnit注入我的testdata无法完全关闭我的h2connection(仍然想知道为什么在运行h2.bat cmdline时没有发生这种情况)。

我现在尝试在内存模式下运行,如果有效,我会删除这个问题,或者为其他用户写下问题和解决方案。

更新2: 它现在在完全内存模式下工作,我不确定但对我来说核心问题似乎是(我没想到)调用:

IDatabaseConnection dBUnitConnection = new DatabaseConnection(h2Connection);
....
dbUnitConnection.close()

也关闭了我的h2connection。

如上所述,当我从cmdline运行h2server时没有发生这种情况。

0 个答案:

没有答案