【问题标题】:How do you connect to H2 as a remote database instead of embedded mode using Spring Boot?如何使用 Spring Boot 作为远程数据库而不是嵌入式模式连接到 H2?
【发布时间】:2015-12-28 19:06:20
【问题描述】:

我的小型 Spring Boot 应用程序在 src/main/resources 下有此配置:

server.port = 8090
spring.datasource.driverClassName = org.h2.Driver
spring.datasource.url = jdbc:h2:file:~/stapler

我知道这个配置是正确的,因为应用程序启动日志中有有效的端口号 8090。还有一个 @PostConstruct initDb() 方法可以创建数据并将其插入到该数据库的 2 个表中:

package com.avk.stapler.init;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.jdbc.core.JdbcTemplate;

import javax.annotation.PostConstruct;

@SpringBootApplication
public class DbInitializer {
    @Autowired
    private JdbcTemplate jdbcTemplate;

    public static void main(String[] args) {
        SpringApplication.run(DbInitializer.class, args);
    }

    @PostConstruct
    private void initDb() {
        System.out.println("Creating table employees");
        jdbcTemplate.execute("drop table employees if exists");
        jdbcTemplate.execute("create table employees(id serial, name varchar(255), surname varchar(255))");
        jdbcTemplate.execute("insert into employees(name, surname) values('Jan', 'Kowalski')");
        jdbcTemplate.execute("insert into employees(name, surname) values('Stefan', 'Nowak')");


        System.out.println("Creating table allocations");
        jdbcTemplate.execute("drop table allocations if exists");
        jdbcTemplate.execute("create table allocations(id serial, week int, year int, shift int, employee_id bigint)");
        jdbcTemplate.execute("insert into allocations(week, year, shift, employee_id) values(29, 2015, 1, 1)");
        jdbcTemplate.execute("insert into allocations(week, year, shift, employee_id) values(28, 2015, 2, 1)");
        jdbcTemplate.execute("insert into allocations(week, year, shift, employee_id) values(29, 2015, 3, 2)");
        jdbcTemplate.execute("insert into allocations(week, year, shift, employee_id) values(28, 2015, 2, 2)");
    }
}

我可以在启动时看到这个记录,我认为没有更多关于 DB 的日志:

2015-09-30 22:41:22.948  INFO 2832 --- [           main] o.s.j.d.e.EmbeddedDatabaseFactory        : Creating embedded database 'testdb'
Creating table employees
Creating table allocations

由于上述原因,我希望在我的主目录中看到一个“stapler.h2.db”文件,但事实并非如此。此处应更改哪些内容才能显示 DB 文件?

【问题讨论】:

  • 只是一个疯狂的猜测:你确定数据库不只在内存中吗? h2database.com/html/features.html#in_memory_databases
  • 内存中只是 H2 支持的模式之一,我相信。我之前肯定在我的驱动器上看到过 *.h2.db 文件。
  • 我并不是暗示 H2 仅在内存中,而是您可能正在运行它......

标签: java spring-boot spring-data h2


【解决方案1】:

确保您的 maven 依赖项如下所示:

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

如果您想使用 JDBC 将 H2 用作远程数据库,则需要确保您已经在连接 url 中指定的文件路径处运行 H2 数据库。

如果您还没有安装 H2,可以在此处获取以服务器模式运行 H2 的说明:http://www.h2database.com/html/tutorial.html#tutorial_starting_h2_console

一旦您运行它,您就可以使用您提供的相同 JDBC 连接 URL 连接到它。只需使用以下应用程序属性。

spring.datasource.url=jdbc:h2:tcp://localhost/~/stapler
spring.datasource.username=sa
spring.datasource.password=

如果您希望嵌入式 H2 数据库创建您的 H2 文件,这也是可能的。只需使用下面的配置即可。

spring.datasource.url=jdbc:h2:file:~/stapler;AUTO_SERVER=true
spring.datasource.username=
spring.datasource.password=

创建的文件可能会被命名为stapler.mv.db。要告诉 H2 Embedded 使用 stapler.h2.db,您可以在此处了解如何操作:Why is my embedded h2 program writing to a .mv.db file

(非常感谢 Stéphane Nicoll 帮我回答这个问题)

【讨论】:

  • 肯尼,我想我不想要任何远程的东西,我想要一个文件保存在任何位置的硬盘驱动器上的最简单的情况。也就是说,看起来我缺少一些 pom 依赖项——我没有 spring-boot-starter-jdbc,而我只有 spring-jdbc。添加 spring-boot-starter-jdbc 为我修复了它,我可以在我的主目录中看到 mv/h2 文件,但我不知道这是什么原因。
【解决方案2】:

在你的 application.properties 中试试这个。它对我有用:

  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

【讨论】:

    猜你喜欢
    • 2018-06-13
    • 2019-12-26
    • 2019-04-11
    • 2020-04-12
    • 1970-01-01
    • 1970-01-01
    • 2021-01-06
    • 2016-05-05
    • 2022-01-20
    相关资源
    最近更新 更多