alex
alex

Reputation: 414

i can't connect to the H2 database

I'm looking at a training project on working with a database. The main database there is MySQL, and H2 is used in tests, here is a tutorial:

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

The following settings are specified in the application.properties file:

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:

The test passes, i.e. data is entered into the database, checked, everything is OK.

But when I run the test in debug mode, put a breakpoint at the end of the test and try to connect to the database, it is empty, there are not even tables, let alone data. to connect, i use Dbever. In the connection string, I write jdbc:h2:mem:mytestdb;DB_CLOSE_ON_EXIT=FALSE (I tried jdbc:h2:tcp://localhost:9092/mem:mytestdb). Checking the connection pass, writes that everything is OK, the connection is established. What could be the problem?

I tried writing to a file, instead of 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.

Similar questions on this topic were looked at.

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

Upvotes: 1

Views: 731

Answers (1)

Reto H&#246;hener
Reto H&#246;hener

Reputation: 5808

You already figured out how to switch from in-memory database to file-based storage.

Try setting your breakpoint earlier. Maybe at the end of the test there is some cleanup that drops all tables? Or maybe there is a tearDown method doing the cleanup?

Another idea: Write a backup somewhere in the middle of your test:

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

Upvotes: 1

Related Questions