对比实验: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. Linux_kernel_exploits

    功能:自动生成UAF类型漏洞exp文件的工具,目前缺少文档介绍,可以参考test文件下的使用实例,但是源码中缺少dataflowanalyzer模块 相关内容:源码路径https://github.c ...

  2. sshd安全加固

    常见的防护措施: ——用户限制,黑白名单 ——更改验证方式(密码——>密钥对) ——防火墙..... 修改 sshd 配置文件 /etc/ssh/sshd_config -port 3389 # ...

  3. 检查SQL Server数据库各个库表空间使用的方法

    /*创建一张表:表名Data,列名:表名,列数,预留空间,数据占用空间,索引占用空间,剩余空间*/ CREATE TABLE Data ( 表名 ), 列数 ), 预留空间 ), 数据占用空间 ), ...

  4. 实现多层DIV叠加的js事件穿透

    前几天做的一个功能:在地图上加载标注,这个标注是列表,就直接放的 DIV. 后来发现,当鼠标在这个标注上面的时候,滚动鼠标滚轮,地图的缩放功能失效. 想了下,应该是最上面的标注 DIV 拦截了滚轮滚动 ...

  5. HihoCoder1236 Scores

    Scores 求五维偏序(≤).强制在线. \(n,q\le 50000\) 烂大街的题解 考虑如果我们能用bitset找到每一维有哪些比询问点小,然后把每一维的结果取交集,得到的就是答案了. 然后对 ...

  6. git常用命令总结--原创

    0.git status 仓库状态1.git add 工作区-->暂存区2.git commit 暂存区-->版本库3.git log 查看日志4.git reset --hard hea ...

  7. GITHUB使用指南、

    一.安装Git1.通过官网(https://www.git-scm.com/download/)下载git,进入官网,如下图所示:2.选择对应的操作系统后,页面跳转并自动下载对应的Git版本,如下图所 ...

  8. NOIP2018模板总结【数学】

    质因数分解 //质因数分解 int prime[MAXN], tim[MAXN], cnt; void Divide(int N) { printf("%d = ", N); fo ...

  9. VS - Microsoft.Practices.EnterpriseLibrary.Logging

    string fileName = AppDomain.CurrentDomain.BaseDirectory + "\\log.txt";File.AppendAllText(f ...

  10. HTML5新增常用标签

    1.header 标签定义文档的页眉(介绍信息). <body> <article> <header> <h1>What Does WWF Do?< ...