H2无法以编程方式启动 [英] h2 doesn't start programmatically

查看:176
本文介绍了H2无法以编程方式启动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了以下代码:

  private static void startH2(){
        Server server = null;
        try {
            server = Server.createTcpServer("-tcpAllowOthers").start();
            Class.forName("org.h2.Driver");
            Connection conn = DriverManager.
                    getConnection("jdbc:h2:tcp://localhost/~/test;MODE=PostgreSQL", "sa", "");
        } catch (Exception e) {
            LOG.error("Error while initialize", e);
        }
        System.out.println("finish");
    }
    public static void main(String [] args){
        startH2();
    }

我运行我的主要方法并看到以下情况:

I run my main method and see following situation:

看起来像Server.createTcpServer会创建新的非守护线程.

Looks like Server.createTcpServer creates new non daemon thread.

但是通过URL localhost:8082我看不到h2 Web控制台(实际结果-ERR_CONNECTION_REFUSED)

but by url localhost:8082 I don't see h2 web console(actual result - ERR_CONNECTION_REFUSED)

该如何解决?

我通过url注意到了

http://localhost:9092/

我的浏览器下载了包含奇怪内容的文件:

my browser downlods file with strange content:

如果要解码此文本,我会看到以下消息:

if to decode this text I see following message:

版本不匹配,驱动程序版本为"0",但服务器版本为"15"

Version mismatch, driver version is "0" but server version is "15"

我使用的是h2版本1.4.182

推荐答案

H2包含多个服务器:

  • the TCP Server (for H2 JDBC clients),
  • the Web Server (for browsers, the H2 Console application), and
  • the PG Server (for PostgreSQL clients).

您已经启动了TCP服务器.如果要使用浏览器,还需要启动Web服务器:

You have started the TCP Server. If you want to use a browser, you also need to start the Web Server:

private static void startH2(){
    Server tcpServer = null;
    Server webServer = null;
    try {
        tcpServer = Server.createTcpServer("-tcpAllowOthers").start();
        System.out.println("TCP Server Port: " + tcpServer.getPort());
        Class.forName("org.h2.Driver");
        Connection conn = DriverManager.
                getConnection("jdbc:h2:tcp://localhost/~/test22;MODE=PostgreSQL", "sa", "");
        webServer = Server.createWebServer().start();
        System.out.println("Web Server (H2Console) Port: " + webServer.getPort());
    } catch (Exception e) {
        e.printStackTrace();
    }
    System.out.println("finish");
}

这篇关于H2无法以编程方式启动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆