user829174
user829174

Reputation: 6362

How to browse h2database

I am using h2 in my spring application on runtime mode

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

I was able to insert and pull data (using postman) but I want to see the database myself and explore the schemes and data

I'm using Intellij

I've installed H2 Client http://www.h2database.com/html/main.html and browse via sa and blank password (don't understand why the password is blank nor how do I changed it)

where are my tables? am I connected to the right instance?enter image description here

enter image description here

UPDATE I see that when I stop my Spring application, I'm still being able to browse to H2 via H2 console, I was expecting it will be offline... I don't get it

Upvotes: 2

Views: 24945

Answers (3)

Maciej Dobrowolski
Maciej Dobrowolski

Reputation: 12130

As H2 documentation says:

The following connection modes are supported:

  • Embedded mode (local connections using JDBC)
  • Server mode (remote connections using JDBC or ODBC over TCP/IP)
  • Mixed mode (local and remote connections at the same time)

To be able to simply connect to a database from two separate applications, the connection mode should be Server Mode or Mixed Mode (read h2 documentation for further information). The mode you are using is determined by the connection url you use to connect to it and in your case the connection url is jdbc:h2:~/test which means that you're starting H2 in an Embedded Mode. This means that the database server will be started from within your application and will be accessible only to that single JVM.

TLDR

You can simply change the mode to Mixed mode using Automatic Mixed-Mode feature. To do so, just change your jdbc url (in both applications - your app and the client app) to: jdbc:h2:~/databasefile;AUTO_SERVER=TRUE.

This ~/databasefile is a location of where actual data will be stored - again, it is important to access the same file for both connections/applications.

If you're using Spring Boot with default configuration, to change the jdbc url you just have to add following property:

spring.datasource.url=jdbc:h2:~/databasefile;AUTO_SERVER=TRUE

Upvotes: 1

abaghel
abaghel

Reputation: 15297

If you are using in-memory h2 database then use below JDBC url.

jdbc:h2:mem:testdb

Check the configuration provided at post How to connect H2 console to embedded Spring H2 DB

Upvotes: 4

Essex Boy
Essex Boy

Reputation: 7950

I found that I could only do this when I used a file database.

url: jdbc:h2:~/nexin;DB_CLOSE_DELAY=-1;MODE=MySQL;MV_STORE=FALSE;MVCC=FALSE

I was using dbVizualizer not Intelij.

Upvotes: 1

Related Questions