29

I am getting error after opening the h2 database console. I enter database name but it is showing database not found error:

Database "C:/Users/Barlekar/onlineshoppings" not found, and IFEXISTS=true, so we cant auto-create it [90146-199] 90146/90146 (Help)

org.h2.jdbc.JdbcSQLNonTransientConnectionException: Database "C:/Users/Barlekar/onlineshoppings" not found, and IFEXISTS=true, so we cant auto-create it [90146-199]

Masoud Keshavarz
  • 2,166
  • 9
  • 36
  • 48
sonal barlekar
  • 311
  • 1
  • 3
  • 3
  • 1
    Try previous version [1.4.198](https://mvnrepository.com/artifact/com.h2database/h2/1.4.198). I also had the problems after updateing from [1.4.188] to [1.4.199], but [1.4.198] works fine for me. – Yuriy Balakhonov May 21 '19 at 12:49

24 Answers24

82

If you are dealing with the Spring Boot project, please change the JDBC URL jdbc:h2:~/test to jdbc:h2:mem:testdb in the login page, which is the default URL configured by Spring Boot.

Ivan Xue
  • 879
  • 6
  • 9
27

Use a pre-2019 version of the H2 database dependency that auto-creates the database every time you run your standalone application. For example version 1.4.193. Your pom.xml should include this dependency:

<dependency>
        <groupId>com.h2database</groupId>
        <artifactId>h2</artifactId>
        <version>1.4.193</version>
</dependency>
Shant Dashjian
  • 888
  • 11
  • 20
18

Just append this to the application.properties file:

spring.datasource.url=jdbc:h2:mem:testdb
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
Fahad Israr
  • 1,041
  • 10
  • 9
17

Have you upgraded H2 recently? This could be the cause.

This is related to the following H2 commit:

https://github.com/h2database/h2database/commit/8b53f3999c6c5c3d5ca29020e2657968f4f59ec4

and the change was made because of this exploit:

https://www.exploit-db.com/exploits/45506

This means that the default for H2 is now to not auto-create databases when run in standalone network mode.

If you have read and understood the above, and you still want to allow the database to be auto-created, then just add the -ifNotExists flag to your h2 start command like this:

java -cp h2*.jar org.h2.tools.Server -ifNotExists
joelittlejohn
  • 11,665
  • 2
  • 41
  • 54
  • 3
    Please give us the solution. – Bandham Manikanta May 18 '19 at 16:16
  • @BandhamManikanta As I wrote in the answer, just add `-ifNotExists` to the command you use to start h2. – joelittlejohn Jun 05 '19 at 15:46
  • @joelittlejohn, I have tried adding `-ifNotExists` to my IntelliJ runtime configuration (under "Program arguments"), but it does not seem to work. Am I doing something wrong? – AnonymousAngelo Jun 26 '19 at 08:19
  • @AnonymousAngelo you can also add that configuration in your application.properties like so: spring.datasource.url=jdbc:h2:mem:testdb;IFEXISTS=false" – Ivan Jul 25 '19 at 00:48
  • 1
    @Anon567, I tried that... the only I got it to work was to downgrade to an earlier version as Shaun Dashjian mentions above. – AnonymousAngelo Jul 25 '19 at 12:14
  • 2
    @AnonymousAngelo `java -cp h2*.jar org.h2.tools.Server -ifNotExists` will do the job – shvahabi May 15 '20 at 12:50
  • @shvahabi: please turn your comment into an answer, you'll definitely get a "correct answer" checkmark. Please also add that this changed somewhere between 1.4.197 and 1.4.200 because I've just upgraded and couldn't create a database the way I used to. – rychu May 17 '20 at 13:47
  • 1
    @rychu since it was heavily based on current answer I decided not to give a new answer, but just edit it and gave an example to make it clear. – shvahabi May 23 '20 at 11:07
10

i changed JDBC URL to

jdbc:h2:mem:testdb

and it's work

osama Hassan
  • 111
  • 1
  • 4
6

Problem:

Database not found or test folder not in c:/user/username/test

Copy&paste it in application.properties file:

spring.datasource.url=jdbc:h2:~/test
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect

Issue was of URL, check it. Problem solved.

סטנלי גרונן
  • 2,917
  • 23
  • 46
  • 68
3

enter image description here

Maven dependency

        <dependency>
        <groupId>com.h2database</groupId>
        <artifactId>h2</artifactId>
        <scope>runtime</scope>
    </dependency>

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

MacOS Catalina 10.15.4 Worked!

Rimvydas
  • 31
  • 2
3

Make sure you also have Spring Boot Data JPA in your project.

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
chris
  • 2,490
  • 4
  • 32
  • 56
3

In latest versions of H2 Database, the following screenshot might help you to login into H2 database screen. While in Application run console, we can find the JDBC URL link which is required to start the H2 database console in the browser.

While in Application run console, we can find the JDBC url link which is required to start the H2 database console in browser

karel
  • 5,489
  • 46
  • 45
  • 50
2

please change the JDBC URL jdbc:h2:~/test to jdbc:h2:mem:testdb in the login page, which is the default URL configured by Spring Boot.

enter image description here

1

I found that you can add a parameter to the "-ifNotExists" start options when booting up h2 db. Programmatically, you can do it this way:

Server.createTcpServer("-tcpPort", "[PORT]", "-tcpAllowOthers", "-ifNotExists").start();

If you are using the command prompt, the same applies

mainas
  • 754
  • 1
  • 6
  • 15
1

When we try from the H2DB Console, we don't see the actual error completely.

##In Console It shows:##

>Database "mem:testdb" not found, either pre-create it or allow remote database creation 
>(not recommended in secure environments) [90149-200] 90149/90149

However, when we execute the command for opening H2 Console

>    java -cp h2-1.4.200.jar org.h2.tools.Server -ifNotExists

##It shown me the actual error which port already used#

The Web Console server could not be started. Possible cause: another server is already >running at http://191.167.0.7:8082 Exception in thread "main" org.h2.jdbc.JdbcSQLNonTransientConnectionException: Exception >opening port "8082" (port may be in use), cause: "java.net.BindException: Address already >in use: bind" [90061-200] at org.h2.message.DbException.getJdbcSQLException(DbException.java:622) at org.h2.message.DbException.getJdbcSQLException(DbException.java:429)

##Solution: Kill the process running on the port. So that port is available to H2## > Find the pid used for the port

>>D:\H2\bin>netstat -ano | findstr :8082
>>  TCP    [::]:8082              [::]:0                 LISTENING       6376

> Kill The PID

>>D:\H2\bin>taskkill /PID 6376 /F
>>SUCCESS: The process with PID 6376 has been terminated.

> Start the H2 Console now

>java -cp h2-1.4.200.jar org.h2.tools.Server -ifNotExists

This worked for me :). What I feel is the Exception shown on the console have to be more clear to show about port issue.

Thanks

Nagendar M
  • 11
  • 3
1

the problem is not with the url or anything, below is the rule that we have to use in this case, –

  1. For In-memory database:please replace the URL at H2 Console as jdbc:h2:mem:testdb
  2. For on Disk database: use the jdbc:h2:~/testdb
0

By degrading the version, the H2 DB is working but table i am unable to see. Code snippet

Controller

@RestController
public class CurrencyExchangeController {

    @Autowired
    private Environment env;
    @GetMapping("/currency-exchange/from/{from}/to/{to}")
    public CurrencyExchange retriveCurrencyExchange(@PathVariable String from,@PathVariable String to)
    {
        CurrencyExchange currencyExchange = new CurrencyExchange(1000L, from, to, BigDecimal.valueOf(65));
        currencyExchange.setPort(Integer.parseInt(env.getProperty("local.server.port")));
        return currencyExchange;

    }

POJO

@Entity
public class CurrencyExchange {
    @Id
    private Long id;
    @Column(name ="currency_from")
    private String from;
    @Column(name ="currency_to")
    private String to;
    @Column(name ="conversion_multiple")
    private BigDecimal conversion;
    private int port;

Spring boot main

@SpringBootApplication
@ComponentScan(basePackages = {"com.example"})
public class CurrencyExchangeServiceApplication {

    public static void main(String[] args) throws SQLException {
        SpringApplication.run(CurrencyExchangeServiceApplication.class, args);

    }

app.prop

spring.application.name=currency-exchange-service
server.port=8000

spring.jpa.show-sql=true
spring.h2.console.enabled=true

data.sql file
insert into currency_exchange(id,currency_from,currency_to,conversion_multiple,port)
values(1001,'USD','INR',65,0);
insert into currency_exchange(id,currency_from,currency_to,conversion_multiple,port)
values(1002,'EUR','INR',75,0);
deHaar
  • 17,687
  • 10
  • 38
  • 51
Abdul Wajeed
  • 101
  • 1
  • 1
  • 5
0

first url to jdbc:h2:mem:testdb second changed the version of Spring Boot to 2.1.3 and it works

0

I have faced this issue and resolved in the following way

  • To use H2 Database - Your application should be running in JDK Environment, not JRE Environment , to change please use below steps :

    1. Right-click your project > properties
    2. Select “Java Build Path” on left, then “JRE System Library”, click Edit…
    3. Select "Workspace Default JRE"
    4. Click "Installed JREs"
    5. If you see JRE you want in the list select it (selecting a JDK is OK too)
    6. If not, click Search…, navigate to Computer > Windows C: > Program Files > Java, then click OK
    7. Now you should see all installed JREs, select the one you want
    8. Click OK/Finish a million times. I have used Georgie provided steps here -> https://stackoverflow.com/a/29640138


  • In H2 Login console, default URL come up by Spring Boot will be jdbc:h2:~/test, it should actually match with the spring boot application properties spring.datasource.url=jdbc:h2:mem:testdb so replace the url in the login console and click on connect I have used [Ivan Xue][2] provided steps here -> https://stackoverflow.com/a/56539107

  • Please clear target using mvn clean command and allow project to build if doesn't enforce using maven force update snapshot or mvn install and then start your spring boot app again to see the difference
0

use the below properties in the pom.xml and run the application again.

    <?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

<groupId>com.in28minutes.database</groupId>
<artifactId>database-demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>database-demo</name>
<description>Demo project for Spring Boot</description>

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.0.0.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <java.version>1.8</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-jdbc</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>
    </dependency>

</dependencies>

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

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

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

Mdhar9e
  • 1,376
  • 4
  • 23
  • 46
0

I faced this issue recently, there were two issues.

  1. By default, it was not populated with in-memory database rather jdbc:h2:~/test.
  2. The db name I configured in the datasource was not test.

when I changed the JDBC url to jdbc:h2:mem:{db-name-in-config}, it worked like a charm.

0

Check The jdbc Url used in h2 console to see if it match what you have specified

0
Along with h2 dependency in POM :
<dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <scope>runtime</scope>
    </dependency>

I added jpa, crud repository dependency in POM Like given below and It worked fine for me:

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
Pankaj
  • 121
  • 7
0

Adding to @Prateik Upadhyay's answer, if you want to rename your db connection name to something else the only thing that worked for me is below. I am using spring boot 2.2.4 version. with h2 1.4.200 version.

spring.jpa.hibernate.ddl-auto=create-drop
spring.datasource.url=jdbc:h2:~/MyComponentDB;DB_CLOSE_ON_EXIT=FALSE;AUTO_SERVER=TRUE;DB_CLOSE_DELAY=-1;MODE=Oracle
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
kkk
  • 166
  • 2
  • 18
0

Here the question is how to enable database creation when database not exists in given path the default start of h2 database will enable ifexists=true condition.

to avoid this scenario one has to start h2 database from terminal with following option. java -cp h2-1.4.199.jar org.h2.tools.Server -ifNotExists

once u give this option. can one can create database with in the url Many times I want to create database file in a specific folder. once u start the server with flag -ifNotExists, you can use below syntax to create database folder of your choice jdbc:h2:tcp://localhost/D:/Working/Sri/Technology/h2/data/books here books is the name of the database. if doesn't exists, it will create books data base in given folder.

0

I got the same error when for the very first time I tried to open the H2 console. The best way is to, change the JDBC URL and replace it with the value that is present in the terminal, the line which says ->( H2 console available at '/h2-console'. Database available at 'JDBC URL') It is the correct way to do

0

Please use the same URL path that is given for datasource in application.properties file datasource URL should be same as given in application.properties file