Java 随机出现“IO 错误:网络适配器无法建立连接”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22213580/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Getting "IO Error: The Network Adapter could not establish the connection" randomly
提问by farbodg
I'm randomly getting "IO Error: The Network Adapter could not establish the connection" when trying to connect to an Oracle database through Java. Sometimes I have to run my application a couple times before it stops throwing the error.
尝试通过 Java 连接到 Oracle 数据库时,我随机收到“IO 错误:网络适配器无法建立连接”。有时我必须运行我的应用程序几次才能停止抛出错误。
// initializes database connection
private static Connection initializeDatabaseConnection(Properties prop) {
System.setProperty("oracle.net.tns_admin", prop.getProperty("tnsLocation"));
try {
Class.forName("oracle.jdbc.OracleDriver");
}
catch (ClassNotFoundException ex)
{
System.out.println(ex.getMessage());
}
String dbURL = "jdbc:oracle:thin:@" + prop.getProperty("serviceName");
String username = prop.getProperty("username");
String password = prop.getProperty("password");
Connection conn = null;
try {
conn = DriverManager.getConnection(dbURL, username, password);
}
catch (SQLException ex)
{
System.out.println("Error initializing database connection. " + ex.getMessage());
System.exit(1);
}
return conn;
}
Any ideas as to why it throws that error randomly? I'm using JDK 1.7 with the ojdbc6.jar driver.
关于为什么它会随机抛出该错误的任何想法?我正在使用带有 ojdbc6.jar 驱动程序的 JDK 1.7。
采纳答案by codenheim
Download PingPlotter and turn it on (leave it running for 24 hours or so). Watch the historical graph, you'll likely find the IP address dropping or response times going through the roof at the same time your app can't connect to Oracle. You don't have a logic error, either the server is drastically overloaded, or you've got major congestion between you and it. Are you connecting through a VPN?
下载 PingPlotter 并打开它(让它运行 24 小时左右)。观看历史图表,您可能会发现 IP 地址下降或响应时间急剧下降,同时您的应用程序无法连接到 Oracle。您没有逻辑错误,要么服务器严重超载,要么您和它之间出现严重拥塞。您是通过 VPN 连接的吗?
Or just use ping from the command line in continuous mode for an hour or so:
或者在连续模式下从命令行使用 ping 一个小时左右:
ping -t <IP address or hostname>
Ping plotter doesn't ping as often and it creates nice graphs.
Ping 绘图仪不经常 ping 并创建漂亮的图形。
If you are working remotely, then it could be your ISP. Otherwise, talk to the network administrator or the DBA.
如果您在远程工作,那么它可能是您的 ISP。否则,请与网络管理员或 DBA 联系。
Obviously you can't go into production with a poor DB connection, so hopefully you just see this issue from your development environment. Try deploying to the machine where you plan to run the application eventually and see if it improves.
显然,您无法在数据库连接不佳的情况下投入生产,因此希望您能从开发环境中看到这个问题。尝试部署到您计划最终运行应用程序的机器上,看看它是否有所改进。
If there is nothing you can do to make it better, I recommend increasing your initial login timeout for your JDBC driver.
如果您无能为力,我建议您增加 JDBC 驱动程序的初始登录超时。
Google JDBC setLoginTimeout
Google JDBC setLoginTimeout
Here is an example, although there are several ways to go about it, depending on your driver.
这是一个示例,尽管有多种方法可以解决此问题,具体取决于您的驱动程序。