2

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

user829174
  • 5,566
  • 18
  • 67
  • 111
  • You are looking at an in-memory DB that's in the process of the H2 browser, not the embedded DB that's in your own application. Don't run the H2 console as a separate app, but embed it in your own app. See this: [View content of H2 or HSQLDB in-memory database](http://stackoverflow.com/questions/7309359/view-content-of-h2-or-hsqldb-in-memory-database). – Jesper May 02 '17 at 10:18

3 Answers3

2

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

Community
  • 1
  • 1
abaghel
  • 13,316
  • 2
  • 43
  • 60
0

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.

Essex Boy
  • 6,423
  • 2
  • 15
  • 19
0

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

Community
  • 1
  • 1
Maciej Dobrowolski
  • 10,083
  • 3
  • 40
  • 58