Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

H2 Database not found error: 90146. H2 database is not created on start

Tags:

spring-boot

h2

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

like image 887
Richard Payne Avatar asked May 19 '19 16:05

Richard Payne


4 Answers

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

like image 200
Ashwin Patil Avatar answered Nov 19 '22 22:11

Ashwin Patil


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.

like image 24
Tarun Kolla Avatar answered Nov 19 '22 22:11

Tarun Kolla


just append this to your application.properties file.

spring.datasource.url=jdbc:h2:mem:testdb
spring.data.jpa.repositories.bootstrap-mode=default
like image 5
Priyank Avatar answered Nov 19 '22 21:11

Priyank


As Stuck said.

Simply remove the semicolon:

wrong:    jdbc:h2:mem:testdb;
correct:  jdbc:h2:mem:testdb
like image 4
Simon Martinelli Avatar answered Nov 19 '22 22:11

Simon Martinelli