Connecting to H2 database from H2 Console

Viewed 14004

I have used the below settings in my Application properties file. But still cant see my tables in h2 console.

enter image description here

application.properties

logging.level.org.springframework.web=INFO
spring.datasource.url=jdbc:h2:mem:challenge;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE;MV_STORE=FALSE
spring.datasource.driver-class-name=org.h2.Driver
spring.datasource.schema=classpath:/schema.sql
spring.datasource.data=classpath:/data.sql
spring.h2.console.enabled=true
spring.h2.console.path=/h2
server.port = 8080

I used the string jdbc:h2:mem:challenge;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE;MV_STORE=FALSE in the JDBC URL on H2 console to login. Yet i am not seeing any tables

The below is my Graddle file

buildscript {
    ext {
        springBootVersion = '1.4.4.RELEASE'
    }
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
    }
}

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'

jar {
    baseName = 'challenge'
    version = '0.0.1-SNAPSHOT'
}

sourceCompatibility = 1.8

repositories {
    mavenCentral()
}


dependencies {
    compile('org.springframework.boot:spring-boot-starter-jdbc')
    compile('org.springframework.boot:spring-boot-starter-security')
    compile('org.springframework.boot:spring-boot-starter-web')
    runtime('org.springframework.boot:spring-boot-devtools')
    runtime('com.h2database:h2')
    testCompile('org.springframework.boot:spring-boot-starter-test')
}

The following is the class which implements Springsecurity for my spring boot app

@Configuration
@EnableWebSecurity
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {

     http.authorizeRequests().antMatchers("/h2/*").permitAll();


        http.csrf().disable();
        http.headers().frameOptions().disable();


    }
}
3 Answers

Use a pre-2019 version of the H2 database dependency that will auto-creates the database every time you run standalone application.

<dependency>
        <groupId>com.h2database</groupId>
        <artifactId>h2</artifactId>
        <version>1.4.193</version>
</dependency>

When the Spring Boot application starts, you should output similar to this:

2020-08-12 07:30:10.902  INFO 44404 --- [           main] net.querybuilder4j.Application           : Starting Application on sm7chrisjones with PID 44404 (/Users/chris.jones/repos/qb4j-api/target/classes started by chris.jones in /Users/chris.jones/repos/qb4j-api)
2020-08-12 07:30:10.905  INFO 44404 --- [           main] net.querybuilder4j.Application           : No active profile set, falling back to default profiles: default
2020-08-12 07:30:11.906  INFO 44404 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2020-08-12 07:30:11.913  INFO 44404 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2020-08-12 07:30:11.913  INFO 44404 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.30]
2020-08-12 07:30:11.985  INFO 44404 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2020-08-12 07:30:11.986  INFO 44404 --- [           main] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 1032 ms
2020-08-12 07:30:11.999  INFO 44404 --- [           main] o.s.j.d.e.EmbeddedDatabaseFactory        : Starting embedded database: url='jdbc:h2:mem:testdb;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=false', username='sa'
2020-08-12 07:30:12.085  INFO 44404 --- [           main] o.s.b.a.h2.H2ConsoleAutoConfiguration    : H2 console available at '/h2-console'. Database available at 'jdbc:h2:mem:testdb'
2020-08-12 07:30:12.532  INFO 44404 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
2020-08-12 07:30:12.681  WARN 44404 --- [           main] ion$DefaultTemplateResolverConfiguration : Cannot find template location: classpath:/templates/ (please add some templates or check your Thymeleaf configuration)
2020-08-12 07:30:12.801  INFO 44404 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
2020-08-12 07:30:12.804  INFO 44404 --- [           main] net.querybuilder4j.Application           : Started Application in 2.228 seconds (JVM running for 2.816)

I noticed these lines:

2020-08-12 07:30:11.999  INFO 44404 --- [           main] o.s.j.d.e.EmbeddedDatabaseFactory        : Starting embedded database: url='jdbc:h2:mem:testdb;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=false', username='sa'
2020-08-12 07:30:12.085  INFO 44404 --- [           main] o.s.b.a.h2.H2ConsoleAutoConfiguration    : H2 console available at '/h2-console'. Database available at 'jdbc:h2:mem:testdb'

I placed the url and username in the H2 console and was able to connect successfully. Without this output, it would have taken a long time to figure out, because I called the in-memory database myDb in the application.properties, but apparently that's ignored and all H2 in-memory databases are called testdb.

I'm using these spring-boot-starter-web and h2 maven dependencies in case you find that another version of the dependencies does not provide this output on application start up:

<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-web -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    <version>2.2.3.RELEASE</version>
</dependency>

<!-- https://mvnrepository.com/artifact/com.h2database/h2 -->
<dependency>
    <groupId>com.h2database</groupId>
    <artifactId>h2</artifactId>
    <version>1.4.200</version>
    <scope>runtime</scope>
</dependency>
Related