在 Java 中设置 Oracle 10g 数据库连接超时
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4669052/
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
Set Oracle 10g database connection timeout in Java
提问by Tim
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();
}
}
The instance xxx
is not existing. But I get the following exception:
实例xxx
不存在。但我收到以下异常:
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)
How I can implement a timeout to an not existing or available Oracle database instance?
如何对不存在或不可用的 Oracle 数据库实例实现超时?
Edit:If I set the DriverManager.setLoginTimeout(30);
to 30 second, the exception happens so fast as before!
编辑:如果我将 设置DriverManager.setLoginTimeout(30);
为 30 秒,则异常发生得像以前一样快!
回答by Buhake Sindi
Your DriverManager.setLoginTimeout(1);
sets the maximum time in secondsfor the driver to wait while connecting to the database. In your case, it's set to 1.
您DriverManager.setLoginTimeout(1);
设置驱动程序在连接到数据库时等待的最长时间(以秒为单位)。在您的情况下,它设置为 1。
To have no limit, set setLoginTimeout(0)
where 0
means no limit.
要没有限制,请设置setLoginTimeout(0)
where0
表示没有限制。
I hope this helps.
我希望这有帮助。
Updateif 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.
更新如果您的实例xxx
不存在,您希望您的 Oracle 驱动程序如何连接到数据库?设置loginTimeout
不连接“主机”的时间长短没有任何区别。
回答by trauds
Because, in Java doc, it shows: timeout in seconds, but in the implementation of JDBC Oracle, it is milliseconds.
因为,在Java doc中,它显示:超时以秒为单位,但在JDBC Oracle的实现中,它是毫秒。
You can try using a measure of milliseconds.
您可以尝试使用毫秒为单位。