美文网首页
H2数据库设置本地文件库(Server Mode)时Spring

H2数据库设置本地文件库(Server Mode)时Spring

作者: DinghuiYe | 来源:发表于2018-02-18 22:20 被阅读0次

H2数据库配置为类似jdbc:h2:tcp://localhost/~/test/database的形式,就是Server Mode,如果没有单独启动H2服务器,那么会导致如下错误:

org.h2.jdbc.JdbcSQLException: Connection is broken: "java.net.ConnectException: Connection refused: connect: localhost" [90067-196]

Caused by: java.net.ConnectException: Connection refused: connect

org.springframework.jdbc.support.MetaDataAccessException: Could not get Connection for extracting meta data; nested exception is org.springframework.jdbc.CannotGetJdbcConnectionException: Could not get JDBC Connection; nested exception is org.h2.jdbc.JdbcSQLException: Connection is broken: "java.net.ConnectException: Connection refused: connect: localhost" [90067-196]

Caused by: org.springframework.jdbc.CannotGetJdbcConnectionException: Could not get JDBC Connection; nested exception is org.h2.jdbc.JdbcSQLException: Connection is broken: "java.net.ConnectException: Connection refused: connect: localhost" [90067-196]

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaAutoConfiguration.class]: Invocation of init method failed; nested exception is org.hibernate.service.spi.ServiceException: Unable to create requested service [org.hibernate.engine.jdbc.env.spi.JdbcEnvironment]

Caused by: org.hibernate.service.spi.ServiceException: Unable to create requested service [org.hibernate.engine.jdbc.env.spi.JdbcEnvironment]

Caused by: org.hibernate.HibernateException: Access to DialectResolutionInfo cannot be null when 'hibernate.dialect' not set

大概就是连接失败一类的问题,网上说是因为springboot比h2服务器启动更早,然后导致这个错误的,具体原因还有待查证,解决方案是:

  1. 创建类Starters(类名无所谓,后面引用就可以)
import org.h2.tools.Server;

public class Starters {

    private static final Logger logger = LoggerFactory.getLogger(Starters.class);

    public static void startH2Server() {
        try {
            Server h2Server = Server.createTcpServer().start(); // 关键代码
            if (h2Server.isRunning(true)) {
                logger.info("H2 server was started and is running.");
            } else {
                throw new RuntimeException("Could not start H2 server.");
            }
        } catch (SQLException e) {
            throw new RuntimeException("Failed to start H2 server: ", e);
        }
    }
}

注意:Springboot默认设置h2的依赖范围是runtime,需要删除那句代码才能编译时调用org.h2.tools.Server

  1. 分别在SpringBoot启动类ServletInitializerApplication中调用H2 server的启动代码
public class ServletInitializer extends SpringBootServletInitializer {

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        Starters.startH2Server(); // 关键代码
        return application.sources(Application.class);
    }
}
@SpringBootApplication
public class Application {

    public static void main(String[] args) {
        Starters.startH2Server(); // 关键代码
        SpringApplication.run(Application.class, args);
    }
}

这样修改之后,就可以正常启动了!

参考:How to start H2 TCP server on Spring Boot application startup?

相关文章

  • H2数据库设置本地文件库(Server Mode)时Spring

    H2数据库配置为类似jdbc:h2:tcp://localhost/~/test/database的形式,就是Se...

  • AZKABAN (一)安装

    安装 Azkaban的部署方式有三种: solo server mode:即独立部署模块,数据库被嵌入H2,web...

  • 在Spring Boot使用H2内存数据库

    在Spring Boot使用H2内存数据库 在之前的文章中我们有提到在Spring Boot中使用H2内存数据库方...

  • H2

    隔离数据库环境 使用 H2 内存数据库提高数据库操作速度 在 spring-test 中使用 spring-tx ...

  • 四、数据库及数据库对象

    1.下列关于SQL Server 2008中mode1数据库的说法, 错误的是____。 A. mode1数据库是...

  • 操作数据库遇到的问题

    插入数据库的时间有几小时的误差 可能是数据库时区的问题在spring.datasource.url设置server...

  • Spring Boot JPA访问H2 Database

    这章介绍Spring Boot JPA 访问H2 Database,在项目开发过程中都会连接本地数据库进行业务开...

  • 4.集成mybatis实现插入操作

    导入依赖 配置连接数据库的信息,注意h2数据库URL的设置方式,可以直接选择工程文件目录 运行main函数,点击登...

  • axios

    // 本地json-server服务器搭建代码 // 引入数据库文件 var appData = require(...

  • 内存数据库h2接入springboot2.1.2

    接入原因 本地环境做测试时,没有相关数据库连接,可以使用内存数据库h2代替 默认连接池 springboot2默认...

网友评论

      本文标题:H2数据库设置本地文件库(Server Mode)时Spring

      本文链接:https://www.haomeiwen.com/subject/urnbtftx.html