javaspringspring-booth2dbeaver

Cannot see H2 database tables and data via DBeaver or IntelliJ IDEA


In a Spring Boot app, I am trying to read data via DBeaver as I generally did before in SERVER Mode:

Here is my app props:

spring:
  datasource:
    driver-class-name: org.h2.Driver
    url: jdbc:h2:mem:${db_name};DATABASE_TO_UPPER=FALSE;DB_CLOSE_ON_EXIT=false
    username: ${db_username}
    password: # leave password field empty for H2 database Server Mode connection
  jpa:
    hibernate:
      ddl-auto: create
    show-sql: true
    open-in-view: false
    properties:
      hibernate.format_sql: true
      hibernate:
        dialect: org.hibernate.dialect.H2Dialect

    h2:
      console:
        enabled: 'true'
        path: /h2-console

and pom.xml:

<dependency>
    <groupId>com.h2database</groupId>
    <artifactId>h2</artifactId>
    <version>1.4.193</version>
    <!--<scope>runtime</scope>-->
</dependency>

Although the data is inserted to the database when running my app and I can query the data via Postman, I cannot see anything under PUBLIC > Tables when connecting to the database via DBeaver or IntelliJ IDEA.

So, what is the reason, do you have an idea? I tried some workarounds but not worked.


Solution

  • You are using a memory-based h2 instance, your connection from DBeaver or IntellijIDEA might not connected to the same instance.

    If you want to make sure your DBeaver or IntellijIDEA connected to the same h2 instance, you'd better use file mode instead of mem mode for you h2 instance.

    Using file mode for your h2 database, you can checkout H2 database - file mode for more information.

    Just for simple, you can change spring.datasource.url from jdbc:h2:mem:${db_name};DATABASE_TO_UPPER=FALSE;DB_CLOSE_ON_EXIT=false to jdbc:h2:file:/path/to/${db_name};DATABASE_TO_UPPER=FALSE;DB_CLOSE_ON_EXIT=false. Now you can connect to your h2 instance in DBeaver or IntellijIDEA with new connection URL jdbc:h2:file:/path/to/${db_name};DATABASE_TO_UPPER=FALSE;DB_CLOSE_ON_EXIT=false.