KROGOT88
KROGOT88

Reputation: 79

H2 console Spring Boot not opening

I have very simple project POM:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
         https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.5.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <packaging>jar</packaging>
    <groupId>com.example</groupId>
    <artifactId>managing-transactions</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>managing-transactions</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jdbc</artifactId>
        </dependency>

        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <scope>compile</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

in appliation.properties i have tried different variants

#spring.datasource.url=jdbc:h2:mem:testdb
spring.datasource.url=jdbc:h2:mem:test;DB_CLOSE_DELAY=-1
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=sa
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
spring.h2.console.enabled=true
spring.h2.console.path=/h2-console

but console do not open on http://localhost:8080/h2-console

Only way it work is with old way

Runnable runServer = new Runnable() {
    @Override
    public void run() {
        try {
            Server.startWebServer(DriverManager.getConnection("jdbc:h2:mem:test;DB_CLOSE_DELAY=-1", "sa", ""));
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
};
Thread threadServer = new Thread(runServer);
threadServer.start();

Upvotes: 4

Views: 12681

Answers (2)

Shankar
Shankar

Reputation: 2835

To view the H2 Console with your Spring Boot project, a web server needs to be running so that it serves up the "h2-console" url.

So instead of using the JDBC project alone:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-jdbc</artifactId>
</dependency>

Also add the starter-web project.

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>

Then, either explicitly configure the H2 Console to be enabled in an application.properties file. Or add the Spring Boot Dev Tools dependency to your pom.xml, which will implicitly configure the H2 Console to be enabled.

application.properties:

spring.h2.console.enabled=true

Spring Boot Dev Tools dependency in pom.xml.

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-devtools</artifactId>
    <optional>true</optional>
</dependency>

Here is the minimum code to demonstrate the capability. It is an application made up of only this one class.

package org.example;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class MinimumSpringBootWithWebForH2 {

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

}

Here is the console output from this minimum example: console output of H2 MVP

Here is the link to a sample project: https://github.com/shankarps/H2Project

Here is an article that has some details: https://medium.com/@harittweets/how-to-connect-to-h2-database-during-development-testing-using-spring-boot-44bbb287570

Here is a link to the Spring Boot docs, which specify the requirements for serving up the H2 Console with your application: https://docs.spring.io/spring-boot/docs/current/reference/html/data.html#data.sql.h2-web-console

Upvotes: 6

ReMadWeb
ReMadWeb

Reputation: 58

you can also my pom.xml, which is built and run in Java 11.

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.0.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>org.remad</groupId>
    <artifactId>JPAStarter</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>JPAStarter</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>11</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

application.properties

###############################################################################
# Spring database source config
###############################################################################
spring.datasource.url=jdbc:h2:file:/tmp/JpaStarter/db
#spring.datasource.username=sa
#spring.datasource.password=password
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
spring.datasource.driverClassName=org.h2.Driver

###############################################################################
# Hibernate Configuration
###############################################################################
spring.jpa.hbernate.ddl-auto = update

###############################################################################
# Debug configuration
###############################################################################
spring.h2.console.enabled=true
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.format_sql=true

Hope this helps :-)

Upvotes: 0

Related Questions