11

I'm using a H2 database with a file using Spring Boot.

In my application.properties, I have this entry:

spring.datasource.url=jdbc:h2:file:c:/Testprojekte/spring-boot-h2-db

But now I would like to be able to look at the database while running the application, which currently isn't possible because I need to have the database running in server mode in order to do so. In the documentation I found that I have to add AUTO_SERVER=TRUE to the URL but this doesn't solve the problem.

So, what do I have to change to be able to connect to that database from different processes at the same time ?

thanks for any help! Thorsten

user1119859
  • 669
  • 2
  • 9
  • 20

2 Answers2

15

You can start the H2 TCP server as a bean:

<dependency>
    <groupId>com.h2database</groupId>
    <artifactId>h2</artifactId>
    <!-- <scope>runtime</scope> -->
</dependency>
@SpringBootApplication
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

    @Bean(initMethod = "start", destroyMethod = "stop")
    public Server h2Server() throws SQLException {
        return Server.createTcpServer("-tcp", "-tcpAllowOthers", "-tcpPort", "9092");
    }
}

Then connect to it from your IDE with the following params (password - empty):

url: jdbc:h2:tcp://localhost:9092/mem:testdb
user: sa

More info is here and here.

Cepr0
  • 28,144
  • 8
  • 75
  • 101
  • 1
    i had to add this to the url: jdbc:h2:tcp://localhost:9090/~/database-name, cause i was using a file h2 database. – cabaji99 Aug 31 '19 at 21:07
  • that's not a good choice from a performance perspective if db is on the same host, cause you using kernel call and full tcp stack call for accessing db. and that config is not mandatory to access h2 console, it can be embedded but with additional h2 servlet – Yura Sep 12 '21 at 18:56
  • I get "Connection is broken: "java.net.SocketTimeoutException: connect timed out: localhost:9092" error. I also try via `spring.datasource.url=jdbc:h2:tcp://localhost:9092/~/test-db` as I use H2 database. Any idea amigo? –  Jun 27 '22 at 16:34
  • To get `Server` use `import org.h2.tools.Server;`. If it is not available make sure your maven import is as follows: `com.h2databaseh2` (without "runtime", see https://stackoverflow.com/questions/29904999/cannot-resolve-org-h2-tools-server) – Semjon Mössinger May 19 '23 at 09:32
7

You can enable h2 web console to access your h2 in memory or in file database using a web interface in your browser.

therefor add in application.properties the lines:

spring.h2.console.enabled=true
spring.h2.console.path=/h2-console

after that restart your spring boot application and check http://localhost:8080/h2-console with your browser.

Sma Ma
  • 3,343
  • 2
  • 31
  • 39
  • In my case my h2 console is broken; connection is done but maybe due to some JS error I cannot see anything. I just see some frames with no content, only error info "refused connection" or so. – WesternGun Feb 19 '23 at 15:56