springboot 2.3.0 while connecting to h2 database
Asked Answered
P

33

53

In Springboot 2.3.0.RELEASE I am getting the the following error while connecting to h2 database in the console

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

Prowl answered 18/5, 2020 at 8:12 Comment(2)
Can you show us the full string starting with jdbc: ? You seem to do it right but maybe a typo ? It should be: jdbc:h2:mem:test_mem , and the fact it tells you it's a remote db is suspicious.Few
H2 Console from third-party projects doesn't allow database creation any more due to security reasons and shows such error message, there is nothing suspicious. But specified JDBC URL should point to existing database created by application.Sonics
L
73

You can fix this by setting the spring.datasource.url property like so:

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

Prior to Spring Boot 2.3.0-RELEASE this was the default, but I'm not sure where it's set. As of 2.3.0-RELEASE, the schema looks to be a randomly generated GUID.

Laddie answered 29/5, 2020 at 19:48 Comment(1)
Thank you very much Chacewells. Its working fine with your guidance.Prowl
A
52

Actually, your h2 databse is looking for a file called test.mv.db. But that file was not present in your user directory. So, that it just slapping you and asking you to pre-create it in that path.

Note: That is the root file where our H2 DB store all our information.

  1. Add below line in your application.properties file
    spring.datasource.url = jdbc:h2:mem:testdb

  2. Go to your user directory, in my case, it is (C:\Users\subra)

  3. Create a new file called test.mv.db and saved it under all file option like below.

    Save Format

  4. Now restart your app.

  5. Done

Reference Screenshot:

My Problem:

My Problem


The Result

The Result

Awesome answered 12/6, 2021 at 16:38 Comment(5)
> So, that it just slapping you and asking you to pre-create it in that path. It's asking him to create the database mem:testdb, not a file test.mv.db. Why? I had the same issue and when I created this file, it worked!Ustkamenogorsk
Extremely clear and helpful!Luxuriant
thanks, this was exactly my issue. In Ubuntu, going to /home/<userName> directory and creating the file fixed the issueOrchitis
Awesome. The file is "testdb.mv.db" if connect to jdbc:h2:~testdbCharlesettacharleston
Thank you. This saved my time. Perform the same if you are using the Mac too, and the issue will be resolved.Shumate
C
36

Step 1. In application.properties:

spring.h2.console.enabled=true
spring.datasource.url=jdbc:h2:mem:testdb

Step 2. Start your Spring Boot App and open:

http://localhost:8080/h2-console/

If you still face issue try pasting the URL value which you mentioned in application.properties jdbc:h2:mem:testdb in

JDBC URL of h2-console 

Then you wont face below mentioned issue Database h2 not found, either pre-create it or allow remote database creation (not recommended in secure environments) [90149-200] 90149/90149 (Help)

Cychosz answered 22/9, 2020 at 7:45 Comment(2)
I don't understand , "try pasting the URL", can you provide an example? JDBC URL of h2-consolePairs
@Pairs op probably meant that paste the URL(jdbc:h2:mem:testdb) to the console's JDBC URL section.Thither
J
27

You are not able to connect to database because you are using old JDBC URL. Every time you start a spring project, JDBC URL changes as well.

Solution: Copy the JDBC URL from console every time you want to connect to a database or just use below property in application.properties file spring.datasource.url=jdbc:h2:mem:testdb

see screenshot

Jainism answered 18/9, 2020 at 6:55 Comment(1)
Yes, this the main reason of my problem. And setting a name prevents this action repeating again and againMiles
C
19

Another reason for the error could be a the missing JDBC dependency:

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

I got the exact same error and this was the issue in my case.

Citrange answered 11/12, 2020 at 14:38 Comment(2)
After adding this dependency I was able to see in app log the database url. Thanks!Idiopathy
yes, it works we also need jdbc to connectMilewski
B
6

In Spring Boot 2.3.0 the property spring.datasource.generate-unique-name is set to true. This enables the random generation of the schema name.

By setting it to false the database is generated as in previous versions (jdbc:h2:mem:testdb). I find this way preferable, without the need to specify the URL manually.

Balfour answered 9/8, 2020 at 18:35 Comment(0)
H
6

Create a file with a .mv.db extension in your project's folder. Make sure that the file's name ends with .mv.db otherwise H2 console does not recognize the file (I know it doesn't make sense and I have no explanation why :D). After creating the file, enter jdbc:h2:./devdb to h2 console's JDBC URL like this:

Check this image out

Then you should be OK!

Hypercritical answered 6/9, 2020 at 14:26 Comment(0)
F
3

Step 1. Download h2 database engine from here:

https://www.h2database.com/html/main.html

Step 2. In application.properties:

spring.h2.console.enabled=true

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

Step 3. Start your Spring Boot App and open:

http://localhost:8080/h2-console/

It must be working fine for you.

Freshwater answered 13/8, 2020 at 7:41 Comment(0)
B
3

Even I came across the same set of errors when started learning data persistence with h2 database. The solution is in the console log itself. The database name is auto-generated by and can be found in the spring logs. The reason behind the error is after from 2.3.0 version release onwards if the URL is not mentioned its auto-generated.

Here is the log message which includes database URL: INFO 16096 --- [ restartedMain] o.s.b.a.h2.H2ConsoleAutoConfiguration : H2 console available at '/h2-console'. Database available at 'jdbc:h2:mem:f1dfeade-60dd-4809-8978-42547353b2f3'

Bilious answered 27/1, 2021 at 12:10 Comment(0)
S
3

Use h2 dependency with the old version (1.4.192).

<dependency>
    <groupId>com.h2database</groupId>
    <artifactId>h2</artifactId>
    <version>1.4.192</version>
    <scope>runtime</scope>
</dependency>
Salesmanship answered 5/10, 2021 at 17:21 Comment(0)
M
3

I added "test.mv.db" file into "C:\Users\YourUserName" then restart the server and it's worked.

enter image description here

enter image description here

Molt answered 28/10, 2021 at 6:11 Comment(1)
works perfictly fine !Mastoid
I
2

Since 2.3.0, default value of generate-unique-name is true.

You can revert to the previous behavior by following setting:

spring.datasource.generate-unique-name=false

see also: https://mcmap.net/q/340291/-h2-localhost-url-different-from-testdb

Ious answered 4/9, 2020 at 0:42 Comment(0)
D
2

Add property to application.properties:

spring.datasource.generate-unique-name=false

I had also missed adding the following dependencies to pom.xml:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
    
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
Downtown answered 20/8, 2021 at 16:56 Comment(1)
The missing part was we must beed to add JPA dependencyUnsuspecting
P
2

I had the same problem and I got it solved this way

  1. Go to application.properties
  2. Add the following :
spring.datasource.url=jdbc:h2:mem:testdb
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=password
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect

now you can use sa for username and password for password

PS : that's usually the by default configuration that spring boot uses

Protozoal answered 12/9, 2021 at 22:39 Comment(1)
This solution worked for me, but after set the password in the h2 consoleHemisphere
R
1

In case this helps anybody. My issue was because I have this in my application.properties

spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration

Not sure why I added this config before. But once this is taken out, it solves my issue

R answered 3/4, 2021 at 9:6 Comment(0)
F
1

Adding Spring-boot started JDBC solved my issue.

https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-jdbc/2.5.2

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-jdbc</artifactId>
    <version>2.5.2</version>
</dependency>
Fatimahfatimid answered 15/7, 2021 at 8:19 Comment(0)
W
1

I connect h2 by jdbc:h2:mem:testdb but not jdbc:h2:~/testdb

Wheelsman answered 18/7, 2021 at 11:15 Comment(0)
A
1

NOTE:->spring.datasource.url=jdbc:h2:mem:nilesh-->Should be same in console(JDBC URL)

application.properties

spring.h2.console.enabled=true
spring.datasource.platform=h2
spring.datasource.url=jdbc:h2:mem:nilesh

enter image description here

Astrometry answered 9/7, 2022 at 11:36 Comment(0)
I
1

It's error of H2-Database:

Solution -

Let's suppose ~/Test-H-2/Project-X/DB-files path of your project (Database files)

  1. create myTest.mv.db file in ~/Test-H-2/Project-X/DB-files folder
  2. Add jdbc:h2:~/Test-H-2/Project-X/DB-files/myTest to JDBC URL in H2-Database console

OR

simply you can create ~\test.mv.db file & add jdbc:h2:~/test in to JDBC URL in H2-Database console

Working for me

Incorporeal answered 27/7, 2022 at 8:6 Comment(0)
T
1

Remove the spring-boot-devtools dependency from pom.xml and restart the application. for me, this worked.

Taipan answered 8/1, 2023 at 11:46 Comment(1)
Happened to me as well.Scandinavia
S
1

copy the jdbc url --> jdbc:h2:mem:test like below. then your problem will be solved.

h2-console

Surround answered 7/3, 2023 at 11:1 Comment(1)
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.Sitin
E
1

I am using spring boot 2.5. I tried many things mentioned above to solve the issue such as adding spring.datasource.generate-unique-name=false and add "INIT=CREATE SCHEMA" in the spring.datasource.url to be something like spring.datasource.url=jdbc:h2:mem:apiDB;INIT=CREATE SCHEMA IF NOT EXISTS user_scheme\;SET SCHEMA user_scheme;

But Nothing solved the issue

But when I added the below depedency to my pom.xml

     <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>

The issue was gone

I think the main reason is that in case of in-memory DB, spring needs a DB connection to execute creating the DB (apiDB), and this can not be done without spring data-jpa library otherwise it will complains that the DB does not exist.

Do not forget to add the below line to your application.properties

spring.h2.console.enabled=true

Excitement answered 9/9, 2023 at 9:54 Comment(0)
W
0

i had the same problem while creating schema for h2 database in spring version 2.3.0 ...sometimes the database would connect but could not show me my structure of table so i tried chaneging the spring version and it worked like an magic try changeing spring version to 2.1.15.RELEASAE

Wahoo answered 10/7, 2020 at 5:34 Comment(0)
R
0

Replace the default JDBC url of h2-console from application.properties to use JDBC Url=jdbc:h2:mem:testdb Below is the application.properties

spring.datasource.url=jdbc:h2:mem:testdb
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=user
spring.datasource.password=password
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
spring.h2.console.enabled=true 
Replay answered 28/5, 2021 at 6:34 Comment(0)
T
0

I had same issue in windows 10. Try to replace

spring.datasource.url=jdbc:h2:~/testdb

with

spring.datasource.url=jdbc:h2:mem:testdb
Turbofan answered 28/5, 2021 at 13:38 Comment(0)
B
0

Add these two properties in application.property file

spring.h2.console.enabled=true
spring.datasource.url=jdbc:h2:mem:testdb

type url: localhost:<portnumber>/h2-console in browser You will get a page regrading login database Remove existing JDBC url in the page and put this jdbc:h2:mem:testdb

Burbank answered 5/7, 2021 at 8:1 Comment(1)
This seems to be the same answer as: https://mcmap.net/q/336821/-springboot-2-3-0-while-connecting-to-h2-databaseNorseman
A
0

my solution for this problem is:

in case you you didn't made a database folder in home directory (in windows under the: C:\Users*USERNAME\test* || in Linux: under the: ~/test) make it and add below lines to application.properties:

spring.jpa.hibernate.ddl-auto=create-drop
spring.datasource.url=jdbc:h2:~/test
spring.jpa.hibernate.ddl-auto=create-drop

it should help you to login.

((use blank username and password for H2))

Airminded answered 20/12, 2021 at 9:45 Comment(0)
B
0

Adding to answers, which mention auto-generated database name in Spring Boot 2.3+ – this is the way how to get the generated name into H2 Console programatically in Spring Boot, so that you can keep the generated database name. It basically gets the first H2 database source and updates/generates the H2 Console configuration file ~/.h2.server.properties, which is then loaded by H2 Console when it is first accessed.

Configure pom.xml to use H2 types directly:

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

Enable H2 Console in application.properties (true is a default value):

spring.h2.console.enabled=true

Code to use auto-generated database name:

import java.io.OutputStream;
import java.sql.Connection;
import java.util.List;
import java.util.Objects;
import java.util.Properties;
import java.util.stream.Collectors;

import javax.sql.DataSource;

import org.h2.engine.Constants;
import org.h2.store.fs.FileUtils;
import org.h2.util.SortedProperties;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.context.annotation.Configuration;

@Configuration
@AutoConfigureAfter(DataSourceAutoConfiguration.class)
public class H2ConsoleDatabaseAutoConfigurator {

  @Autowired
  public void configure(ObjectProvider<DataSource> dataSource) throws Exception
  {
    Properties properties = SortedProperties.loadProperties(Constants.SERVER_PROPERTIES_DIR + "/" + Constants.SERVER_PROPERTIES_NAME);

    List<String> urls = dataSource.orderedStream().map((available) -> {
      try (Connection connection = available.getConnection()) {
        if (connection.getMetaData().getURL().startsWith("jdbc:h2:mem:")) {
          return connection.getMetaData().getURL() + "|" + connection.getMetaData().getUserName();
        } else {
          return null;
        }
      }
      catch (Exception ex) {
        return null;
      }
    }).filter(Objects::nonNull).collect(Collectors.toList());

    if (urls.size() > 0)
    {
      for (int i = 0;; i++)
      {
        String value = properties.getProperty(String.valueOf(i), null);
        if (value == null || value.startsWith("Local H2|")) {
          properties.setProperty(String.valueOf(i), "Local H2|org.h2.Driver|" + urls.get(0));
          break;
        }
      }

      OutputStream out = FileUtils.newOutputStream(
          Constants.SERVER_PROPERTIES_DIR + "/" + Constants.SERVER_PROPERTIES_NAME, false);
      properties.store(out, "H2 Server Properties");
      out.close();
    }
  }
}

The console will contain the current H2 name as Local H2 menu entry:

H2 Console

The code is a composite of sources from H2 Console and Spring Boot H2 Console Autoconfiguration.

Biafra answered 5/3, 2022 at 0:10 Comment(0)
R
0

Adding JPA Dependency to pom.xml fixed this error for me.

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>

Here is my application.properties file, just for your reference.

spring.h2.console.enabled=true
spring.datasource.url=jdbc:h2:mem:testapp
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=password
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
Reins answered 20/8, 2022 at 19:34 Comment(0)
A
0

I am using Spring 2.7.3. I had the same issue with mem:testdb not found. After trying out all the different solutions I could find in Stackoverflow, the only solution that worked for me was adding both "spring.datasource.url=jdbc:h2:mem:testdb" AND "spring.datasource.generate-unique-name=false" in my application properties file.

I thought that by adding just the "spring.datasource.url=jdbc:h2:mem:testdb" should stop spring from generating unique name everytime I restart the application but apparently not. Manually creating the test.mv file did not work for me either. Hope this helps.

Asti answered 29/8, 2022 at 10:52 Comment(0)
C
0

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

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 .mv.db. Restart Springboot, "Test Connection" is Green and Connect , takes me to Console GUI.

Crock answered 9/10, 2022 at 20:28 Comment(0)
A
0

Step 1: add the dependencies in 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>
    </dependency>
</dependencies>

Step 2:

add below code in the application.properties file:

spring.h2.console.enabled=true

spring.datasource.username=sergey

spring.datasource.password=sergey

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

spring.datasource.driverClassName=org.h2.Driver

spring.jpa.database-platform=org.hibernate.dialect.H2Dialect

Step 3 :

run : http://localhost:8099/h2-console/

and entered the below details:

  1. jdbc url : jdbc:h2:mem:testdb
  2. user name : sergey
  3. password : sergey

enter image description here

it worked for me .

Acidophil answered 15/9, 2023 at 4:0 Comment(0)
C
-3

Please use this below solution it's working.

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.

Croteau answered 1/8, 2021 at 16:43 Comment(1)
This is the same solution as in this other answer (and mentioned in others).Friary

© 2022 - 2024 — McMap. All rights reserved.