10

Just created a simple spring-boot project from the spring initializer. I went to add a local h2 db for testing and am unable to login. Seems that it cannot create the test db when starting up but cannot figure out why this may be the case.

spring:
  h2:
    console:
      enabled: true
      path: /h2
  datasource:
    url: jdbc:h2:mem:testdb;
    username: sa
    password:
    driver-class-name: org.h2.Driver
    platform: h2
  jpa:
    show-sql: true
    hibernate:
      ddl-auto: create
    properties:
      hibernate:
        dialect=org:
          hibernate:
            dialect:
              H2Dialect: org.hibernate.dialect.H2Dialect

Database "mem:testdb" not found, and IFEXISTS=true, so we cant auto-create it [90146-199] 90146/90146

Tarun Kolla
  • 917
  • 1
  • 10
  • 30
Richard Payne
  • 283
  • 2
  • 4
  • 16

9 Answers9

24

In earlier version of spring, default url will be jdbc:h2:mem:testdb and testdb was created by default. from 2.3.0 onwards, if url is not mentioned it will auto generate database name. auto generated database name can be found in the spring logs.

enter image description here

Ashwin Patil
  • 1,307
  • 1
  • 23
  • 27
  • 6
    for me it is the twelfth message from the top (in the console). Message is as follows: `o.s.b.a.h2.H2ConsoleAutoConfiguration : H2 console available at '/h2-console'. Database available at 'jdbc:h2:mem:36963133-9e0a-40bf-bac6-ed118afb3a97'` I just copied and pasted the location into the console and connected. – Nate T Jul 25 '20 at 00:32
  • Great tip! I found it in the log as well. – Paul Lewallen Nov 21 '20 at 22:36
15

I had the same error and I found these to be helpful:

Adding a pre-2019 version to the pom.xml file as below:

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

This fixes the error but isn't the right way to do it. The newer version of H2 Database do not create a new database as it doesn't exist by default and are enabled to false for security purposes.

A better way would be adding making changes to url as:

jdbc:h2:mem:testdb;IFEXISTS=FALSE;

Hope it helps. I made changes in my application.properties file.

Tarun Kolla
  • 917
  • 1
  • 10
  • 30
6

just append this to your application.properties file.

spring.datasource.url=jdbc:h2:mem:testdb
spring.data.jpa.repositories.bootstrap-mode=default
Hamza Abdaoui
  • 2,029
  • 4
  • 23
  • 36
Priyank
  • 61
  • 1
  • 1
4

As Stuck said.

Simply remove the semicolon:

wrong:    jdbc:h2:mem:testdb;
correct:  jdbc:h2:mem:testdb
Simon Martinelli
  • 34,053
  • 5
  • 48
  • 82
3

I have the same problem with windows. I just change spring.datasource.url in application property file to jdbc:h2:file:C:/data/sample and run project. after that set JDBC URL to jdbc:h2:file:C:/data/sample. look like picture JDBC URL

2

I had a same problem and I changed the version of Spring Boot to 2.1.3 and it works

Ronald
  • 291
  • 3
  • 7
  • 23
1

wrong:

spring.datasource.url=jdbc:h2:mem:testdb

This below solution worked for me

spring.datasource.url=jdbc:h2:file:~/test;
Dobromir Kirov
  • 934
  • 4
  • 16
Java lover
  • 21
  • 3
0

If you are using spring boot in order to use h2 DB, make sure you have dependencies on your pom.xml file

<dependency>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
     <groupId>com.h2database</groupId>
     <artifactId>h2</artifactId>
     <scope>runtime</scope>
</dependency>

Your application.yml makes it possible to access h2 DB by URI /h2-console preceded by server URL and to connect to DB named "testdb" with username "sa" and no password, after the application has been started on the server.

kylewelsby
  • 4,031
  • 2
  • 29
  • 35
vit
  • 1
0

maybe you have to setting about h2.

In application.properties

spring.h2.console.enabled=true
spring.h2.console.settings.web-allow-others=true
spring.datasource.generate-unique-name = false 
spring.h2.console.path=/h2-console
spring.datasource.url=jdbc:h2:mem:testdb
spring.datasource.driverClassName=org.h2.Driver

when you put these codes in Application.properties you can see how it work!

mandoo
  • 1