h2 mixed mode connection problem

匿名 (未验证) 提交于 2019-12-03 02:21:02

问题:

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 !

回答1:

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.



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