在Java中设置Oracle 10g数据库连接超时 [英] Set Oracle 10g database connection timeout in Java
问题描述
我尝试使用以下代码设置连接超时:
I tried to set a connection timeout with the following code:
public class ConnectionTimeout {
public static void main(String[] args) throws Exception {
String entry = "jdbc:oracle:thin:@xxx:1521:xxx";
Connection con=null;
Class.forName("oracle.jdbc.driver.OracleDriver");
DriverManager.setLoginTimeout(1);
con=DriverManager.getConnection(entry,"username","password");
Statement s=con.createStatement();
s.execute("select 1 from dual");
s.close();
con.close();
}
}
实例 xxx
不存在。但是我得到以下异常:
The instance xxx
is not existing. But I get the following exception:
Exception in thread "main" java.sql.SQLException: E/A-Exception: Socket is not connected
at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:112)
at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:146)
at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:255)
at oracle.jdbc.driver.T4CConnection.logon(T4CConnection.java:387)
at oracle.jdbc.driver.PhysicalConnection.<init>(PhysicalConnection.java:439)
at oracle.jdbc.driver.T4CConnection.<init>(T4CConnection.java:165)
at oracle.jdbc.driver.T4CDriverExtension.getConnection(T4CDriverExtension.java:35)
at oracle.jdbc.driver.OracleDriver.connect(OracleDriver.java:801)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at my.package.connection.timeout.ConnectionTimeout.main(ConnectionTimeout.java:22)
如何实现对不存在的或可用的Oracle数据库实例?
How I can implement a timeout to an not existing or available Oracle database instance?
编辑:
如果我设置 DriverManager.setLoginTimeout(30);
到30秒,异常发生得如此之快!
If I set the DriverManager.setLoginTimeout(30);
to 30 second, the exception happens so fast as before!
推荐答案
你的 DriverManager.setLoginTimeout(1);
设置驱动程序在连接数据库时等待的最长时间秒。在你的情况下,它被设置为1.
Your DriverManager.setLoginTimeout(1);
sets the maximum time in seconds for the driver to wait while connecting to the database. In your case, it's set to 1.
要没有限制,请设置 setLoginTimeout(0)
其中 0
表示无限制。
To have no limit, set setLoginTimeout(0)
where 0
means no limit.
我希望这有帮助。
更新如果您的实例 xxx
不存在,您希望您的Oracle驱动程序如何连接数据库?设置 loginTimeout
的时间不会有任何差别,没有主机可以连接。
Update if your instance xxx
doesn't exists, how would you expect your Oracle Driver to connect to the database? It won't make any difference how long you set your loginTimeout
there's not "host" to connect to.
这篇关于在Java中设置Oracle 10g数据库连接超时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!