7

I have created a spring boot application to connect h2 database with it. While doing so, it throws an error showing Database not found. Please help me with the solution which I can implement and resolve the issue.

I have added com.h2database dependency in the pom.xml file, then also it gives the error.

Below is my pom.xml file and application.properties file

pom.xml

<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 http://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.1.4.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>
    <groupId>com.example</groupId>
    <artifactId>santanderdbproj</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>firstproject</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
        <maven-jar-plugin.version>3.1.1</maven-jar-plugin.version>
    </properties>

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

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

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
    <repositories>
    <repository>
        <id>spring-milestones</id>
        <name>Spring Milestones</name>
        <url>https://repo.spring.io/milestone</url>
    </repository>


    <repository>
        <id>spring-snapshots</id>
        <name>Spring Snapshots</name>
        <url>https://repo.spring.io/snapshot</url>
        <snapshots>
            <enabled>true</enabled>
        </snapshots>
    </repository>
</repositories>
<pluginRepositories>
    <pluginRepository>
        <id>spring-milestones</id>
        <name>Spring Milestones</name>
        <url>https://repo.spring.io/milestone</url>
    </pluginRepository>

    <pluginRepository>
        <id>spring-snapshots</id>
        <name>Spring Snapshots</name>
        <url>https://repo.spring.io/snapshot</url>
        <snapshots>
            <enabled>true</enabled>
        </snapshots>
    </pluginRepository>
</pluginRepositories>
</project>

application.properties

spring.datasource.url = jdbc:h2:file:C:/data/sample
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
spring.h2.console.enabled=true
spring.h2.console.path=/h2

I have attached the error image below, please refer to it. Database not found error

mate00
  • 2,727
  • 5
  • 26
  • 34
Atul Agrawal
  • 105
  • 1
  • 2
  • 12
  • 1
    Do you see this error in H2 console embedded into the Spring? It does not allow database creation by default any more since 1.4.198 due to security reasons, you need to create the database first in your application. If you see this error in the stack trace of your application, post it here. – Evgenij Ryazanov Oct 09 '19 at 06:47
  • I am getting the error in web page while running the application on localhost. When I click on connect button, it throws the error. I had updated the question and attached the image link of error page, please look into it. @EvgenijRyazanov – Atul Agrawal Oct 09 '19 at 06:54
  • 1
    i was created boot project using initializer - this link helped me instead of application.properties i changed application.yml and pasted the below link has .(https://stackoverflow.com/questions/56209686/h2-database-not-found-error-90146-h2-database-is-not-created-on-start) and don't forgot to remove the semicolon at the end of db name . – SakthiSureshAnand Jul 08 '20 at 06:46

13 Answers13

12

This is an incorrect error message that appears only in 1.4.198 and 1.4.199. The next version of H2 (1.4.200) will show a better message like

Database … not found, either pre-create it or allow remote database creation (not recommended in secure environments)

You see this error message because your database doesn't exist yet. The normal way to fix your problem is to create the database first with your application before trying to login into it with H2 Console.

H2 Console can be configured to allow database creation, but it may create a security hole in your system, anyone who can open this page may do anything with your system with yours access permissions in such configuration.

Note that H2 Console in browser's session launched by H2 from its icon in the system tray (you can simply launch the h2-1.4.199 jar as Java application, or use java -jar h2-1.4.199.jar) gives you permission to create a new database in secure way. You can use it for that purpose. If you're not planning to use it as a TCP Server, close it from the system tray icon after database creation to make sure that it doesn't hold your database. You can also use the command-line Shell tool: https://h2database.com/html/tutorial.html#creating_new_databases

H2 Console from the Spring doesn't ship with such feature.

Older versions of H2 (up to 1.4.197) also allow the database creation, including the console from the Spring, but, again, it creates a security hole in you system.

Evgenij Ryazanov
  • 6,960
  • 2
  • 10
  • 18
12

As per H2 DB If below the error then we have solution enter image description here

First go to console and find the jdbc URL enter image description here

Copy the URL and put in H2 console inside JDBC URL enter image description here

Then Connect

!! Happy Coding !!

Rajat Kumar
  • 1,459
  • 1
  • 11
  • 10
8

You might have missed specifying the version, please add this dependency in your pom.xml this will work

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

if the above step doesn't solve the issue then try changing JDBC URL in the console to below URL

jdbc:h2:mem:testdb
sandeep seervi
  • 111
  • 2
  • 7
4

Adding the db version like below to maven dependency, resolved the issue for me.

<version>1.4.193</version>
mb66
  • 47
  • 3
4

Overriding the Version for h2 database dependency worked for me

<dependency>
    <groupId>com.h2database</groupId>
    <artifactId>h2</artifactId>
    <scope>runtime</scope>
    <version>1.4.193</version>
</dependency>
Arghya Sadhu
  • 41,002
  • 9
  • 78
  • 107
1

We wanted to upgrade from 1.4.193 to 1.4.200, so no, setting version to 1.4.193 was not the right solution for us. We're optionally starting the TCP server in our process, so I read the link from accepted answer: https://h2database.com/html/tutorial.html#creating_new_databases

As suggested (actually, not suggested for security reasons, but we accepted the risk), I added -ifNotExists argument to the Server.createTcpServer(...) call and the issue was resolved for us.

virgo47
  • 2,283
  • 24
  • 30
1

a) Try giving spring.datasource.url=jdbc:h2:mem:testdb - in application.properties b) Make sure you are using jdbc:h2:mem:testdb as the database URL in H2 Console

Vishnu KP
  • 11
  • 2
0

You can add database configuration in your property file.

server:
  port: 9092
spring:
  h2:
    console:
      enabled: true
  datasource:
    url: jdbc:h2:mem:testdb;
    username: sa
    password:
    driver-class-name: org.h2.Driver
    platform: h2
Ganesh Giri
  • 1,135
  • 11
  • 18
0

Just by adding version as mentioned in above answer didn't resolve for me along with the h2 version i also added below configuration to resolve the issue

i added below version for H2 dependencies in pom.xml

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

And added below configuration in application.properties file

spring.h2.console.enabled=true
spring.datasource.platform=h2
spring.datasource.driverClassName = org.h2.Driver
spring.datasource.url=jdbc:h2:file:~/test;
spring.datasource.username=sa
spring.datasource.password=
abhinav kumar
  • 1,487
  • 1
  • 12
  • 20
0

Just an alternative , you can get the correct JDBC url in logs as well. Example LOGS o.s.b.a.h2.H2ConsoleAutoConfiguration : H2 console available at '/h2-console'. Database available at 'jdbc:h2:mem:testDb'

Note :To enable h2 logs add below in application.properties file

spring.h2.console.enabled=true

0

After visiting localhost:8080/h2-console, check for the url. It is generally pre-populated. I removed it and entered the same url I used in my application.properties file.

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

Apart from this, adding version to h2 dependency in pom.xml file can be useful.

Follow the steps given here and you're good to go.

0

Looks like everybody trying their own ways but solution to problem is NOT configuration but PHYSICAL.

Somehow , If you want to connect via console, Spring is unable to create a PHYSICAL file testdb.mv.db in root folder, i.e C:/USERS/testdb.mv.db. I went to C:/Users/SHEKHAR , and created a New > text file ( don't save it untill you rename it to testdb.mv.db or test.mv.db. Restart Springboot, "Test Connection" is Green and Connect , takes me to Console GUI.

supernova
  • 3,111
  • 4
  • 33
  • 30
0

For this issue, you may have to check the logs which specify the clear JDBC URL which sample log where we can see the url. Now add that URL to the h2-console Image consists of details where to add the copied url. Then it will work with the given generated URL. Hope you get it!

  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jul 19 '23 at 07:11