oracle.jdbc.ReadTimeout与Connection.getNetworkTimeout对比Statement.setQueryTimeout

时间:2014-11-13 16:13:44

标签: java oracle jdbc

Connection.getNetworkTimeout()是驱动程序等待数据库请求完成的毫秒数。

Statement.setQueryTimeout()独立于Connection.getNetworkTimeout()中指定的超时值,表示特定查询超时的值。

我的问题是: 这些方法与可以在连接属性中设置的oracle.jdbc.ReadTimeout之间的相关性是什么。

1 个答案:

答案 0 :(得分:7)

Statement.setQueryTimeout()适用于数据库,而套接字级别的oracle.jdbc.ReadTimeout适用。以前的设置将真正停止数据库处理,清除数据库资源。后者将破坏套接字操作,在数据库中留下垃圾。应用程序将被解除阻止,但数据库仍将处理请求。仅当您无权访问源代码时,套接字级别设置似乎是一个不错的选择。下面的代码是在11g(11.2.0.2)上用jdbc6.jar(11.2.0.4.0)执行的

import 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) {}

    }
}