Database not found, and IFEXISTS=true, so we cant auto-create it

社会主义新天地 提交于 2019-11-27 23:48:12

问题


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]


回答1:


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.




回答2:


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>



回答3:


Have you upgraded H2 by any chance?

I think this is related to the following H2 commit:

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

and this change was made because of the following 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 (your java -cp ... command).




回答4:


i changed JDBC URL to

jdbc:h2:mem:testdb

and it's work




回答5:


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);



回答6:


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




回答7:


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



回答8:


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>




回答9:


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.




回答10:


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



来源:https://stackoverflow.com/questions/55349373/database-not-found-and-ifexists-true-so-we-cant-auto-create-it

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!