29

I have downloaded the H2 console from http://www.h2database.com/html/download.html
and I have configured the URL in my jdbc.properties file
to jdbc:h2:c:/data/Messaging.

I am using the same URL in the file to connect to the database but I cannot see the tables; I can only see the Information Schema and when I try to select * from tables in it I cannot see the tables neither.

Does anybody have any idea what could be wrong?

Maarten Bodewes
  • 90,524
  • 13
  • 150
  • 263
Dunken
  • 1,311
  • 7
  • 18
  • 30

8 Answers8

37

One tricky thing is that the H2 console will not give you an error if you try to connect to a JDBC URL that doesn't exist. It will instead create a new database at that URL! To connect to the in memory DB, use this JDBC URL (http://localhost:8080/h2-console is the default console):

jdbc:h2:mem:testdb

If you were to enter something like jdbc:h2:~/test then a test.mv file would be created under your home directory. But your application would still be using the in memory database.

The console is available if you have the h2 dependency in your pom, and also the spring developer tools dependency. If you don't have the tools dependency, then you can also see it by having the h2 dependency and adding the following to your application.properties file:

spring.h2.console.enabled=true  #not needed if you have spring-boot-devtools dependency

If you want the db as a file, and not in memory, add the following to applications.properties:

spring.datasource.url=jdbc:h2:~/test_db  #You will see the file in your home directory.

H2 isn't meant for persisted data, but if you want to persist for testing purposes, then add:

spring.jpa.hibernate.ddl-auto = update

Then start up the app, and at the console, use this JDBC URL:

jdbc:h2:~/test_db

In case you were wondering, I only have 1 entry in application.properties (for the database file) and here are my dependencies:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-devtools</artifactId>
    <optional>true</optional>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-rest</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
    <groupId>com.h2database</groupId>
    <artifactId>h2</artifactId>
</dependency>
MattC
  • 5,874
  • 1
  • 47
  • 40
  • 1
    This is a terrific answer, very clear and well written. It helped clarify the tech for me to the point where I was able to find an answer to my own experience of this problem. I am only putting a note here because some of the info given appears to have gone obsolete: the string `jdbc:h2:mem:testdb` will no longer work, but instead a rather long random string appears to be necessary. I added a new answer with the updated info below, moments ago. – Phil Freihofner Apr 27 '21 at 05:12
18

This is how you enable memory enable database using h2 module. You need to ensure the following things

  1. You had class that has @Entity annotations.
  2. you need to enable the following in application.properties file spring.h2.console.enabled=true
  3. Run Spring Boot and enter the following URL localhost:8080/h2-console
  4. This will show a connection screen. Enter the following changes in the JDBC URL: -> jdbc:h2:mem:testdb
  5. Hit the connection button.
Michu93
  • 5,058
  • 7
  • 47
  • 80
HA S
  • 1,129
  • 12
  • 10
15

Based on your question, it doesn't look like you fell victim to this particular pitfall, but this thread ended up helping me nail down the issue, so I am recording the solution here for posterity since it may help others with the same problem.

I also found that when I tried to open my database with the H2 console that I got what appeared to be a blank H2 database (basically, just an INFORMATION_SCHEMA table). While double-checking that I got the name of the DB correct (mydb.mv.db), I discovered that the H2 console had created a second database file, mydb.mv.db.mv.db. Odd.

It turns out that the H2 Console expects you to omit the suffix .mv.db from the filename. Since I hadn't, it was looking for mydb.mv.db.mv.db. Changing the JDBC string to jdbc:h2:mydb solved the problem and I could then open the file from the H2 Console.

Dan Barowy
  • 2,270
  • 24
  • 35
  • Dear me, you have to omit the suffix? This finally solved it for me after like four days of agonising over the problem. Thanks! – flith Nov 22 '16 at 12:58
  • This answer helped me to figure out that I had to change the default suggested JDBC URL in the H2 console from "jdbc:h2:~/test" to "jdbc:h2:mem:test" to see the tables in my in-memory database "test". – Stephanie Apr 11 '18 at 09:58
  • I gave complete path to that file /home/saravana/demo/config/test.mv.db => /home/saravana/demo/config/test Now able to see all tables in my DB. – saravanakumar Jun 05 '20 at 09:23
9

Had the same Problem.

This solved it for me: Why is my embedded h2 program writing to a .mv.db file

Just added ;MV_STORE=FALSE and ;MVCC=FALSE to the jdbc url and everything worked just fine.

Community
  • 1
  • 1
Framus
  • 91
  • 2
  • 1
    Fantastic finally added these so I can view the file database in fbvizualizer jdbc:h2:~/nexin;DB_CLOSE_DELAY=-1;MODE=MySQL;MV_STORE=FALSE;MVCC=FALSE – Essex Boy Feb 12 '17 at 15:22
3

I have used the below and I see my table get created.


spring.datasource.url=jdbc:h2:mem:testdb;DB_CLOSE_ON_EXIT=FALSE
spring.h2.console.enabled=true
spring.h2.console.path=/h2console
spring.datasource.username=sa
spring.datasource.password=
spring.datasource.driverClassName=org.h2.Driver
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.H2Dialect
Soraz
  • 6,610
  • 4
  • 31
  • 48
Krishnendu
  • 31
  • 1
0

You can also avoid this problem by using the same version between H2 console and your Java code.

This is how I solved this same issue here.

Community
  • 1
  • 1
Stephan
  • 41,764
  • 65
  • 238
  • 329
0

Add Annotation @EntityScan("packageName") in main class

VRadhe
  • 529
  • 5
  • 8
0

I finally figured out how to view my default in-memory H2 database tables. I'm sharing this solution because I think the process must have changed recently, due to security concerns. It is no longer possible to use the default jdbc:h2:mem:testdb to gain access to the db, nor to set a name for entry via the application.properties line spring.datasource.name=yourdbnamehere.

As of this writing, when the application starts, there is a console message similar to the following:

2021-04-26 21:16:44.424  INFO 23142 --- [  restartedMain] o.s.b.a.h2.H2ConsoleAutoConfiguration    : H2 console available at '/h2-console'. Database available at 'jdbc:h2:mem:6da7bb82-4008-4396-b825-09e44c17388b'

I'm using Spring Tool Suite 4. Other IDEs may format this message differently.

The key part is the last section of this string, where the database address is specified. The identifier for the database now changes with each boot. (Maybe there is a way to suppress this, but I don't know it.)

I copied the address jdbc:h2:mem:whatever-long-random-name, and pasted it into the h2-console Login Form field labeled "JDBC URL". Once in, hitting or clicking Connect will bring up the database schema and console.

The tutorial I'm working through is Dan Vega's "Springit" application, from his Getting Started With Spring Boot 2.0 course. I'm including this info as a search breadcrumb for anyone following. This course may be a couple years old now, but Vega does a much better-than-average job of explaining, so it remains worthwhile. However, tech marches on, and I assume the need for enhanced security has grown to the point where it's no longer safe to allow an easily guessable database name.

Phil Freihofner
  • 7,645
  • 1
  • 20
  • 41
  • 1
    The default value of the `spring.datasource.generate-unique-name` property was changed to `true` in Spring Boot 2.3.0 (released May 15, 2020) to prevent conflicts when running multiple instances of the same application (think automated testing). When this property is set to true, each instance of the application will use a different H2 database with a unique name (`UUID` as per https://en.wikipedia.org/wiki/Universally_unique_identifier), avoiding conflicts between instances. See https://github.com/spring-projects/spring-boot/issues/16747 – Anthony Holland May 02 '23 at 02:35