[英]H2 Database connection timeout with new computer
I'm using two H2 databases in my Java projects. 我在Java项目中使用了两个H2数据库。 Recently, I changed my computer and since then I have no problem with connecting to one of those databases using my Java code but problems with the other one. 最近,我更换了计算机,从那以后,使用Java代码连接到其中一个数据库就没有问题,而另一个数据库就出现了问题。 It's exactly the same code (up to PATH_TO_DATABASE) I use to start the connection: 它与我用来启动连接的代码完全相同(直到PATH_TO_DATABASE):
import java.sql.DriverManager;
import java.sql.SQLException;
public class DatabaseConfig {
private static java.sql.Connection connection;
public static void init() {
if (connection == null) {
try {
connection = DriverManager.getConnection("jdbc:h2:tcp://192.168.178.50:9092/C:/Users/PATH_TO_DATABASE", "USER", "PW");
} catch (SQLException e) {
System.err.println("SQLException");
e.printStackTrace();
System.exit(0);
}
}
}
[...]
}
This is the stack trace I get: 这是我得到的堆栈跟踪:
SQLException
org.h2.jdbc.JdbcSQLException: Connection is broken: "java.net.SocketTimeoutException: connect timed out: 192.168.178.21:9092" [90067-196]
at org.h2.message.DbException.getJdbcSQLException(DbException.java:345)
at org.h2.message.DbException.get(DbException.java:168)
at org.h2.engine.SessionRemote.connectServer(SessionRemote.java:457)
at org.h2.engine.SessionRemote.connectEmbeddedOrServer(SessionRemote.java:334)
at org.h2.jdbc.JdbcConnection.<init>(JdbcConnection.java:116)
at org.h2.jdbc.JdbcConnection.<init>(JdbcConnection.java:100)
at org.h2.Driver.connect(Driver.java:69)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at data.DatabaseConfig.init(DatabaseConfig.java:20)
...
Caused by: java.net.SocketTimeoutException: connect timed out
at java.net.DualStackPlainSocketImpl.waitForConnect(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
at java.net.PlainSocketImpl.connect(Unknown Source)
at java.net.SocksSocketImpl.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at org.h2.util.NetUtils.createSocket(NetUtils.java:103)
at org.h2.util.NetUtils.createSocket(NetUtils.java:83)
at org.h2.engine.SessionRemote.initTransfer(SessionRemote.java:115)
at org.h2.engine.SessionRemote.connectServer(SessionRemote.java:453)
... 8 more
Using the web interface I can connect to both databases easily. 使用Web界面,我可以轻松连接到两个数据库。
I'm a little bit lost here since I can't figure out what's going wrong with one of the databases. 我在这里有点迷茫,因为我无法弄清楚其中一个数据库出了什么问题。 The strange thing is that everything works fine with the other database. 奇怪的是,其他数据库一切正常。
EDIT: I just realized that the IP address in the error message is different from the one in the code (192.168.178.21:9092 vs. 192.168.178.50:9092). 编辑:我刚刚意识到错误消息中的IP地址与代码中的IP地址不同(192.168.178.21:9092与192.168.178.50:9092)。 Maybe this is th ereason of the error. 也许这是错误的原因。 The one in the code is the correct IP address. 代码中的一个是正确的IP地址。 Why do I get a different one in the error message? 为什么我在错误消息中得到了另一个? Is that the reason of my problem? 那是我出问题的原因吗?
If it's a remote connection, do an ipconfig
to find out if the 192.168.xx address is changed. 如果是远程连接,请执行ipconfig
以确定192.168.xx地址是否已更改。
If it's a local connection, you can simplify your URL as 如果是本地连接,则可以将URL简化为
jdbc:h2:~/PATH_TO_DATABASE
I just solved the problem (it was kind of my stupidity): The project couldn't build on the new computer because the H2 jar was not properly included in the build path. 我只是解决了这个问题(这是我的愚蠢):该项目无法在新计算机上构建,因为H2 jar未正确包含在构建路径中。 That's why eclipse called all the time the old .class file and hence tried to connect to the old computer (192.168.178.21:9092) instead to the new one (192.168.178.50:9092). 这就是为什么eclipse始终都调用旧的.class文件,并因此尝试连接到旧计算机(192.168.178.21:9092)而不是新计算机(192.168.178.50:9092)的原因。 I didn't realize that eclipse didn't compile and was executing old stuff. 我没有意识到Eclipse无法编译并且正在执行旧的东西。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.