oracle.jdbc. ReadQuery与Connection. getNetworkQuery与Statement. setQueryQuery

whlutmcx  于 2023-11-17  发布在  Oracle
关注(0)|答案(2)|浏览(139)

Connection.getNetworkTimeout()是驱动程序等待数据库请求完成的毫秒数。
Statement.setQueryTimeout()独立于Connection.getNetworkTimeout()中指定的超时值,表示特定查询超时的值。
我的问题是:这些方法和oracle.jdbc.ReadTimeout之间的相关性是什么,可以在连接属性中设置。

6mw9ycah

6mw9ycah1#

Statement.setQueryTimeout()在数据库上工作,oracle.jdbc.ReadTimeout在套接字级别上工作。
setQueryTimeout()将真正停止数据库处理,清除数据库资源。
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) {}

    }
}

字符串

8tntrjer

8tntrjer2#

对于dotnet中的任何人来说,等效的超时是
using OracleCommand command = oracleConnection. CommandCommand();command. CommandCommand = 1200;
默认值是60,这会杀死我的长查询。

相关问题