CoderP
CoderP

Reputation: 1371

Embedded h2 database: getting connection but table not found

I am using h2 embedded db. When I start my application the db gets initialized and I am able to get connection object. However when application tries to insert data in the table it throws the sql exception 'table XXXX not found'.

I am able to view the tables from H2 consoles. And same query runs perfectly on the console.

Going through some of the answers of the other question on this topic, I've understood such behavior occurs when the database is empty. But from the H2 console I am able to view and execute queries and I have cross check it is the same db because I am using the exact same url and user to connect both through application and through H2 console.

Any suggestions to resolve?

Upvotes: 2

Views: 4117

Answers (2)

Andreas L.
Andreas L.

Reputation: 2923

My mistake was to work on two different databases. H2 consists of two databases, one memory only (jdbc:h2:mem/...) and the other is persistent (jdbc:h2:~/...).

In addition it seems like the memory database depends on session. It mean if you connect with one tool and create table, then you will not find this table with your server.

Upvotes: 1

CoderP
CoderP

Reputation: 1371

All privileges were in place. So one thing was for sure that I was indeed pointing to the wrong db. But how I did not know. Then I noticed that when I started the application server for the first time, the db file that got created was of extension mv.db. When I connected through h2 console for the first time, a file with extension h2.db got created. So indeed two different databases were getting created for the same user and url.

The reason was that the h2 driver jar that was present in my application was of version 1.4 (beta) which creates db with extension mv.db. The console that I was accessing was through h2 version 1.3 (last stable) which creates h2.db file.

This issue was coming due to version mismatch between application and console. Once I moved both to 1.4 (beta) the issue was resolved.

Upvotes: 4

Related Questions