h2混合模式连接问题

发布于 2024-10-08 18:36:54 字数 1162 浏览 5 评论 0原文

我在 servlet 上下文侦听器中启动 h2 数据库:

public void contextInitialized(ServletContextEvent sce) {
     org.h2.Driver.load();
     String apprealPath = sce.getServletContext().getRealPath("\\");
     String h2Url = "jdbc:h2:file:" + apprealPath + "DB\\cdb;AUTO_SERVER=true";
     LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory(); 
        StatusPrinter.print(lc); 
     logger.debug("h2 url : " + h2Url);
     try {
   conn = DriverManager.getConnection(h2Url, "sa", "sa");
  } catch (SQLException e) {
   e.printStackTrace();
  }
  logger.debug("h2 database started in embedded mode");
        sce.getServletContext().setAttribute("connection", conn);
    }

然后我尝试使用 dbvisualizer 使用以下 url 连接到 h2:

jdbc:h2:tcp://localhost/~/cdb

但收到以下错误消息:

An error occurred while establishing the connection:
   Type: org.h2.jdbc.JdbcSQLException   Error Code: 90067   SQL State: 90067
Message:
   Connection is broken: "Connection refused: connect" [90067-148]

我尝试将 localhost 替换为“172.17.33.181:58524”(我在 cdb.txt 中找到它)。锁.db) 重新连接用户“sa”密码“sa”,然后服务器响应更改为: 用户名或密码错误!

I start h2 database in a servlet context listener:

public void contextInitialized(ServletContextEvent sce) {
     org.h2.Driver.load();
     String apprealPath = sce.getServletContext().getRealPath("\\");
     String h2Url = "jdbc:h2:file:" + apprealPath + "DB\\cdb;AUTO_SERVER=true";
     LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory(); 
        StatusPrinter.print(lc); 
     logger.debug("h2 url : " + h2Url);
     try {
   conn = DriverManager.getConnection(h2Url, "sa", "sa");
  } catch (SQLException e) {
   e.printStackTrace();
  }
  logger.debug("h2 database started in embedded mode");
        sce.getServletContext().setAttribute("connection", conn);
    }

then I try to use dbvisualizer to connect to h2 using following url :

jdbc:h2:tcp://localhost/~/cdb

but get these error messages:

An error occurred while establishing the connection:
   Type: org.h2.jdbc.JdbcSQLException   Error Code: 90067   SQL State: 90067
Message:
   Connection is broken: "Connection refused: connect" [90067-148]

I tried to replace localhost with "172.17.33.181:58524" (I found it in cdb.lock.db)
reconnect with user "sa" password "sa" ,then server response changed to :
wrong username or password !

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

白馒头 2024-10-15 18:36:54

自动混合模式中,您不需要(也不能)使用jdbc:h2:tcp://localhost。只需在各处使用相同的 URL,这意味着 jdbc:h2:file:...DB\\cdb;AUTO_SERVER=true

您可以使用相同的数据库 URL,无论数据库是否已打开。不支持显式客户端/服务器连接(使用 jdbc:h2:tcp:// 或 ssl://)。

In the Automatic Mixed Mode, you don't need to (and you can't) use jdbc:h2:tcp://localhost. Just use the same URL everywhere, that means jdbc:h2:file:...DB\\cdb;AUTO_SERVER=true.

You can use the same database URL independent of whether the database is already open or not. Explicit client/server connections (using jdbc:h2:tcp:// or ssl://) are not supported.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文