Java oracle.jdbc.ReadTimeout vs.Connection.getNetworkTimeout vs.Statement.setQueryTimeout
Java oracle.jdbc.ReadTimeout vs.Connection.getNetworkTimeout vs.Statement.setQueryTimeout,java,oracle,jdbc,Java,Oracle,Jdbc,Connection.getNetworkTimeout()是驱动程序等待数据库请求完成的毫秒数 语句.setQueryTimeout()独立于在连接.getNetworkTimeout()中指定的超时值,并表示特定查询超时的值 我的问题是: 这些方法与可以在连接属性中设置的oracle.jdbc.ReadTimeout之间的对应关系如何。语句.setQueryTimeout()在数据库上工作,而oracle.jdbc.ReadTimeout在套接字级别上工作 setQueryTimeout()
Connection.getNetworkTimeout()
是驱动程序等待数据库请求完成的毫秒数
语句.setQueryTimeout()
独立于在连接.getNetworkTimeout()
中指定的超时值,并表示特定查询超时的值
我的问题是:
这些方法与可以在连接属性中设置的
oracle.jdbc.ReadTimeout
之间的对应关系如何。语句.setQueryTimeout()
在数据库上工作,而oracle.jdbc.ReadTimeout
在套接字级别上工作
setQueryTimeout()
将真正停止数据库处理,清除数据库资源
ReadTimeout
one只会中断套接字操作,在数据库中留下垃圾。应用程序将被取消阻止,但数据库仍将处理请求
只有当您无法访问源代码时,套接字级别设置似乎才是一个不错的选择。下面的代码是用jdbc6.jar(11.2.0.4.0)在11g(11.2.0.2)上执行的
import java.sql.*;
公共类OracleJDBC超时{
字符串dbConn=“jdbc:oracle:thin:@10.37.129.3:1521:XE”;
字符串dbUser=“系统”;
字符串dbPass=“welcome1”;
连接conn=null;
CallableStatement cs=null;
public void callORCL()引发异常{
callORCL(5,0);
}
public void callORCL(int sleepSec)引发异常{
callORCL(睡眠秒,0);
}
public void callORCL(int sleepSec,int queryTimeout)引发异常{
类forName(“oracle.jdbc.driver.OracleDriver”);
conn=DriverManager.getConnection(dbConn、dbUser、dbPass);
字符串plsqlSleep=“begin\n”+“DBMS\u LOCK.sleep(“+sleepSec+”);\n“+”end;\n”;
cs=连接准备呼叫(plsqlSleep);
cs.setQueryTimeout(queryTimeout);
cs.execute();
cs.close();
康涅狄格州关闭();
}
公共静态void main(字符串[]args){
//第一部分带内处理
System.out.println(“Test1.oracle.net.READ_TIMEOUT”);
System.out.println(“-----------------------------------”);
OracleJDBCtimeouts test1=新的OracleJDBCtimeouts();
setProperty(“oracle.net.READ_TIMEOUT”,“2000”);
//不适用于11g
试一试{
测试1.callORCL(5);
System.out.println(“失败!操作未停止!”);
}捕获(例外e){
System.out.println(“成功!操作超时!”);
System.out.println(“异常:+e.getClass());
System.out.println(“消息:+e.getMessage());
}
System.getProperties().remove(“oracle.net.READ_TIMEOUT”);
System.out.println();
System.out.println(“Test2.oracle.jdbc.ReadTimeout”);
System.out.println(“-----------------------------------”);
setProperty(“oracle.jdbc.ReadTimeout”,“2000”);
OracleJDBCtimeouts test2=新的OracleJDBCtimeouts();
试一试{
测试2.callORCL(5);
System.out.println(“失败!操作未停止!”);
}捕获(例外e){
System.out.println(“成功!操作超时!”);
System.out.println(“异常:+e.getClass());
//java.sql.SQLRecoverableException
System.out.println(“消息:+e.getMessage());
//IO错误:套接字读取超时
}
System.getProperties().remove(“oracle.jdbc.ReadTimeout”);
System.out.println();
System.out.println(“Test3.setQueryTimeout”);
System.out.println(“-------------------------”);
OracleJDBCtimeouts test3=新的OracleJDBCtimeouts();
试一试{
测试3.callORCL(5,4);
System.out.println(“失败!操作未停止!”);
}捕获(例外e){
System.out.println(“成功!操作超时!”);
System.out.println(“异常:+e.getClass());
//java.sql.SQLTimeoutException
System.out.println(“消息:+e.getMessage());
//ORA-01013:用户请求取消当前操作
}
System.out.println();
System.out.println(“Test4.oracle.jdbc.ReadTimeoutimport java.sql.*;
public class OracleJDBCtimeouts {
String dbConn = "jdbc:oracle:thin:@10.37.129.3:1521:XE";
String dbUser = "system";
String dbPass = "welcome1";
Connection conn = null;
CallableStatement cs = null;
public void callORCL() throws Exception {
callORCL(5, 0);
}
public void callORCL(int sleepSec) throws Exception {
callORCL(sleepSec, 0);
}
public void callORCL(int sleepSec, int queryTimeout) throws Exception {
Class.forName("oracle.jdbc.driver.OracleDriver");
conn = DriverManager.getConnection(dbConn, dbUser, dbPass);
String plsqlSleep = "begin\n" + "DBMS_LOCK.sleep(" + sleepSec + ");\n" + "end;\n";
cs = conn.prepareCall(plsqlSleep);
cs.setQueryTimeout(queryTimeout);
cs.execute();
cs.close();
conn.close();
}
public static void main(String[] args){
//Part I. In band processing
System.out.println("Test1. oracle.net.READ_TIMEOUT");
System.out.println("------------------------------");
OracleJDBCtimeouts test1 = new OracleJDBCtimeouts();
System.setProperty("oracle.net.READ_TIMEOUT", "2000");
//does not work on 11g
try {
test1.callORCL(5);
System.out.println("Failed! Operation not stopped!");
} catch (Exception e) {
System.out.println("Success! Operation time out!");
System.out.println("Exception:" + e.getClass());
System.out.println("Message :" + e.getMessage());
}
System.getProperties().remove("oracle.net.READ_TIMEOUT");
System.out.println();
System.out.println("Test2. oracle.jdbc.ReadTimeout");
System.out.println("------------------------------");
System.setProperty("oracle.jdbc.ReadTimeout", "2000");
OracleJDBCtimeouts test2 = new OracleJDBCtimeouts();
try {
test2.callORCL(5);
System.out.println("Failed! Operation not stopped!");
} catch (Exception e) {
System.out.println("Success! Operation time out!");
System.out.println("Exception:" + e.getClass());
//java.sql.SQLRecoverableException
System.out.println("Message :" + e.getMessage());
//IO Error: Socket read timed out
}
System.getProperties().remove("oracle.jdbc.ReadTimeout");
System.out.println();
System.out.println("Test3. setQueryTimeout");
System.out.println("----------------------");
OracleJDBCtimeouts test3 = new OracleJDBCtimeouts();
try {
test3.callORCL(5,4);
System.out.println("Failed! Operation not stopped!");
} catch (Exception e) {
System.out.println("Success! Operation time out!");
System.out.println("Exception:" + e.getClass());
//java.sql.SQLTimeoutException
System.out.println("Message :" + e.getMessage());
//ORA-01013: user requested cancel of current operation
}
System.out.println();
System.out.println("Test4. oracle.jdbc.ReadTimeout < setQueryTimeout");
System.out.println("-------------------------------------------------");
OracleJDBCtimeouts test4 = new OracleJDBCtimeouts();
System.setProperty("oracle.jdbc.ReadTimeout", "2000");
try {
test4.callORCL(5,4);
System.out.println("Failed! Operation not stopped!");
} catch (Exception e) {
System.out.println("Success! Operation time out!");
System.out.println("Exception:" + e.getClass());
//java.sql.SQLRecoverableException
System.out.println("Message :" + e.getMessage());
//IO Error: Socket read timed out
}
System.getProperties().remove("oracle.jdbc.ReadTimeout");
// Part II. out of band stop of processing
class DBThread extends Thread {
OracleJDBCtimeouts orclAccess;
public void setOrclAccess(OracleJDBCtimeouts orclAccess) {
this.orclAccess = orclAccess;
}
public void run() {
try {
orclAccess.callORCL(5);
System.out.println("Failed! Operation not stopped!");
} catch (Exception e) {
System.out.println("Success! Operation time out!");
System.out.println("Exception:" + e.getClass());
System.out.println("Message :" + e.getMessage());
}
}
}
System.out.println();
System.out.println("Test5. Thread.interrupt()");
System.out.println("-------------------------");
OracleJDBCtimeouts test5 = new OracleJDBCtimeouts();
System.setProperty("oracle.jdbc.javaNetNio", "true");
DBThread dbThread5 = new DBThread();
dbThread5.setOrclAccess(test5);
try {
dbThread5.start();
Thread.sleep(1000);
dbThread5.interrupt();
Thread.sleep(5000);
} catch (Exception e) {}
System.getProperties().remove("oracle.jdbc.javaNetNio");
System.out.println();
System.out.println("Test6. statement.cancel()");
System.out.println("-------------------------");
OracleJDBCtimeouts test6 = new OracleJDBCtimeouts();
DBThread dbThread6 = new DBThread();
dbThread6.setOrclAccess(test6);
try {
dbThread6.start();
Thread.sleep(1000);
test6.cs.cancel();
Thread.sleep(5000);
} catch (Exception e) {}
}
}