簡體   English   中英

我無法連接到 H2 數據庫

[英]i can't connect to the H2 database

我正在看一個關於使用數據庫的培訓項目。 主庫有MySQL,測試用的是H2,這里有教程:

<dependency>
      <groupId>com.h2database</groupId>
      <artifactId>h2</artifactId>
      <scope>test</scope>
</dependency>

application.properties 文件中指定了以下設置:

jdbc:
  driverClassName: org.h2.Driver
  pool.size.max: 10
  statement.timeout: 0
  url: jdbc:h2:mem:mytestdb;DB_CLOSE_ON_EXIT=FALSE
  username: sa
  password:

測試通過,即數據輸入數據庫,檢查,一切正常。

但是當我在調試模式下運行測試時,在測試結束時下一個斷點並嘗試連接數據庫,它是空的,甚至沒有表,更不用說數據了。 要連接,我使用 Dbever。 在連接字符串中,我寫了 jdbc:h2:mem:mytestdb;DB_CLOSE_ON_EXIT=FALSE (我試過 jdbc:h2:ZE20BB202B1D5537B1415E326092237)。 檢查連接通過,寫道一切正常,連接建立。 可能是什么問題呢?

我嘗試寫入文件,而不是 url: jdbc:h2:mem:ump-currencymanager; DB_CLOSE_ON_EXIT=FALSE I wrote: url: jdbc:h2:file:c:\logs\logdb The file is created when I open it with Dbever, and even the tables are there, but there is no data in the tables themselves.

研究了有關該主題的類似問題。

  @Test
  public void createPeopleTable() throws SQLException {
    int count = peopleService.count();
    assertEquals(0, count);
    peopleService.createPeopleTable();
    count = peopleService.count();
    assertEquals(34, count);
  }

您已經知道如何從內存數據庫切換到基於文件的存儲。

嘗試早點設置斷點。 也許在測試結束時有一些清理會刪除所有表? 或者也許有一個tearDown方法進行清理?

另一個想法:在測試中間的某個地方寫一個備份

connection.createStatement().execute("BACKUP TO '<desired-location>/backup.zip';");

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM