对比实验:https://www.cnblogs.com/xiandedanteng/p/11960320.html

注:setQueryTimeout语句还是好用的,但有些环境不支持,下文是在单位虚拟机上的Oracle发生的事情,而setQueryTimeout语句在我家机器上的Oracle是支持的,详情请见。

本以为,遇到其它session导致行锁发生的情况,设置Statement.setQueryTimeout(seconds)就好了,至少不会让程序等待太长时间,但是事与愿违,我发现无论是设置自动Commit还是手动commit,setQueryTimeout都不好用.

似乎有人也遇到了和我一样的情况:https://community.oracle.com/thread/552257

我的数据库是: select * from v$version;

Oracle Database 12c Enterprise Edition Release 12.2.0.1.0 - 64bit Production
PL/SQL Release 12.2.0.1.0 - Production
"CORE 12.2.0.1.0 Production"
TNS for Linux: Version 12.2.0.1.0 - Production
NLSRTL Version 12.2.0.1.0 - Production

两端设置超时无效的程序如下:

package tablelock;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

import org.apache.log4j.Logger;

public class Deleter {
    private static Logger log = Logger.getLogger(Deleter.class);

    public void doDelete() {
        Connection conn = null;
        Statement stmt = null;

        try{
            Class.forName(DBParam.Driver).newInstance();
            conn = DriverManager.getConnection(DBParam.DbUrl, DBParam.User, DBParam.Pswd);
            conn.setAutoCommit(false);
            stmt = conn.createStatement();
            stmt.setQueryTimeout(1);// It dosen't work

            String sql="delete from TestTB17 where id=2";
            int deleted=stmt.executeUpdate(sql);

            log.info("Deleter deleted "+deleted+" records.");

            conn.commit();
            log.info("committed.");
        } catch (Exception e) {
            e.printStackTrace();
            //System.out.print(e.getMessage());
        } finally {
            try {
                stmt.close();
                conn.close();
            } catch (SQLException e) {
                System.out.print("Can't close stmt/conn because of " + e.getMessage());
            }
        }
    }

    public static void main(String[] args) {
        Deleter d=new Deleter();
        d.doDelete();
    }
}
package tablelock;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

import org.apache.log4j.Logger;

public class Deleter2 {
    private static Logger log = Logger.getLogger(Deleter2.class);

    public void doDelete() {
        Connection conn = null;
        Statement stmt = null;

        try{
            Class.forName(DBParam.Driver).newInstance();
            conn = DriverManager.getConnection(DBParam.DbUrl, DBParam.User, DBParam.Pswd);
            stmt = conn.createStatement();
            stmt.setQueryTimeout(1);// It dosen't work

            String sql="delete from TestTB17 where id=3";
            int deleted=stmt.executeUpdate(sql);

            log.info("Deleter deleted "+deleted+" records.");
        } catch (Exception e) {
            e.printStackTrace();
            //System.out.print(e.getMessage());
        } finally {
            try {
                stmt.close();
                conn.close();
            } catch (SQLException e) {
                System.out.print("Can't close stmt/conn because of " + e.getMessage());
            }
        }
    }

    public static void main(String[] args) {
        Deleter2 d=new Deleter2();
        d.doDelete();
    }
}

在以下程序的log.info处设置断点就能使上面两个Deleter执行不下去:

package tablelock;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

import org.apache.log4j.Logger;

public class Selecter {
    private static Logger log = Logger.getLogger(Selecter.class);

    public void doSelectfor() {
        Connection conn = null;
        Statement stmt = null;

        try{
            Class.forName(DBParam.Driver).newInstance();
            conn = DriverManager.getConnection(DBParam.DbUrl, DBParam.User, DBParam.Pswd);
            conn.setAutoCommit(false);
            stmt = conn.createStatement();

            String sql="select * from TestTB17 for update";
            stmt.executeUpdate(sql);

            log.info("Will block other session before commit/rollback.");

            conn.commit();
            log.info("committed.");
        } catch (Exception e) {
            System.out.print(e.getMessage());
        } finally {
            try {
                stmt.close();
                conn.close();
            } catch (SQLException e) {
                System.out.print("Can't close stmt/conn because of " + e.getMessage());
            }
        }
    }

    public static void main(String[] args) {
        Selecter d=new Selecter();
        d.doSelectfor();
    }
}

具体是何原因还有待查.

--To be continued-- 2019-11-29 10:47

[JDBC/Oracle]设置Statement.setQueryTimeout(seconds)并不好用 原因:环境问题的更多相关文章

  1. Statement.setQueryTimeout(seconds)在家中环境的再次试验 证明此语句还是有效的

    对比实验:https://www.cnblogs.com/xiandedanteng/p/11955887.html 这次实验的环境是T440p上安装的Windows版Oracle11g,版本为: O ...

  2. JDBC超时设置【转】

    恰当的JDBC超时设置能够有效地减少服务失效的时间.本文将对数据库的各种超时设置及其设置方法做介绍. 真实案例:应用服务器在遭到DDos攻击后无法响应 在遭到DDos攻击后,整个服务都垮掉了.由于第四 ...

  3. JDBC的使用-----Statement

    JDBC的查询步骤1.加载数据库驱动类 1)在工程下新建lib文件夹,将 ojdbc6.jar(jar包在:E:\oracle\product\11.2.0\dbhome_1\jdbc\lib)拷贝至 ...

  4. Java -- JDBC 学习--通过Statement进行数据库更新操作

    通过 JDBC 向指定的数据表中插入一条记录. 1. Statement: 用于执行 SQL 语句的对象 1). 通过 Connection 的 createStatement() 方法来获取 2). ...

  5. jdbc oracle clob

    import java.io.BufferedReader; import java.io.Reader; import java.io.Writer; import java.sql.Callabl ...

  6. MySQL异常:Caused by: com.mysql.jdbc.exceptions.MySQLTimeoutException: Statement cancelled due to timeout or client request

    Caused by: com.mysql.jdbc.exceptions.MySQLTimeoutException: Statement cancelled due to timeout or cl ...

  7. JDBC中的Statement和PreparedStatement的区别

    JDBC中的Statement和PreparedStatement的区别  

  8. jdbc:oracle:thin:@192.168.3.98:1521:orcl(详解)

    整理自互联网 一. jdbc:oracle:thin:@192.168.3.98:1521:orcljdbc:表示采用jdbc方式连接数据库oracle:表示连接的是oracle数据库thin:表示连 ...

  9. 关于jdbc Oracle数据库连接的URL错误

    今天写了个java类连接oracle,抛出了这个问题 java.sql.SQLException: No suitable driver found for jdbc:oracle:thin:@127 ...

随机推荐

  1. Android笔记(六十二)网络框架volley

    什么是Volley 很多时候,我们的APP都需要用到网络技术,使用HTTP协议来发送接收数据,谷歌推出了一个网络框架——volley,该框架适合进行数据量不大,但通信频繁的网络操作. 它的优点: (1 ...

  2. 学习python的日常5

    形如__xxx__的变量或者函数名,在python中是有特殊用途的,例如__slots__是为了绑定属性的名称, __len()__方法是为了让class作用于len()函数,很多这样的函数都可以帮忙 ...

  3. centos7 安装Virtualenv

    若想在同一个服务器上,存在多个不同的解析器版本,使用虚拟环境 1.安装虚拟环境 pip3 install virtualenv 2.创建虚拟环境 virtualenv --no-site-packag ...

  4. html布局-子div浮动后,父容器撑不开解决

    文章:子div撑不开父div的几种解决办法: 1,可以在所有子元素后增加<div style="clear:both;"></div> 2,给父容器增加样式 ...

  5. linux 下安装node 并使用nginx做域名绑定

    #1 ,home目录下 下载nodejs安装包,解压 并修改文件夹名称 wget https://nodejs.org/dist/v8.11.4/node-v8.11.4-linux-x64.tar. ...

  6. 史诗级干货-python爬虫之增加CSDN访问量

    史诗级干货-python爬虫之增加CSDN访问量 搜索微信公众号:'AI-ming3526'或者'计算机视觉这件小事' 获取更多算法.机器学习干货 csdn:https://blog.csdn.net ...

  7. 局部敏感哈希算法(Locality Sensitive Hashing)

    from:https://www.cnblogs.com/maybe2030/p/4953039.html 阅读目录 1. 基本思想 2. 局部敏感哈希LSH 3. 文档相似度计算 局部敏感哈希(Lo ...

  8. 如何轻松愉快地理解条件随机场(CRF)

    https://blog.csdn.net/DCX_abc/article/details/78319246 机器学习之条件随机场(CRF): https://blog.csdn.net/wangya ...

  9. segfault at 0 ip sp error 14

    error 14从未见过.谁能帮我解答什么情况才会出现这个,而且怎么定位崩溃函数地址? 备忘: segfault at 引起故障的地址ip 指令的内存地址sp 堆栈指针地址, 及栈顶指针err is ...

  10. 《挑战30天C++入门极限》C++的iostream标准库介绍(3)

        C++的iostream标准库介绍(3) C语言提供了格式化输入输出的方法,C++也同样,但是C++的控制符使用起来更为简单方便,在c++下有两中方法控制格式化输入输出. 1.有流对象的成员函 ...