h2混合模式连接问题 [英] h2 mixed mode connection problem
问题描述
我在servlet上下文侦听器中启动h2数据库:
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);
}
然后我尝试使用dbvisualizer通过以下URL连接到h2:
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]
我尝试将localhost替换为"172.17.33.181:58524"(我在cdb.lock.db中找到了它) 用用户"sa"和密码"sa"重新连接,然后服务器响应更改为: 用户名或密码错误!
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 !
推荐答案
在自动混合模式,则不需要(也不能)使用jdbc:h2:tcp://localhost
.只需在各处使用相同的URL,即jdbc:h2:file:...DB\\cdb;AUTO_SERVER=true
.
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
.
您可以使用相同的数据库URL,而与数据库是否已打开无关.不支持显式客户端/服务器连接(使用jdbc:h2:tcp://或ssl://).
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.
这篇关于h2混合模式连接问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!