Java调用存储过程长时间未执行完问题-解决方案

简介: Java调用存储过程长时间未执行完问题-解决方案

上午在生产服务器发现一个不小的问题,就是一个程序在调用存储过程中抢到了锁,但抢到锁后调用存储过程执行出现卡死的情况,导致抢到的锁迟迟没有释放,这导致第二天程序执行时,因为无法获取到锁而无法正常执行。


解决方案:引入Future类,并设定调用存储过程执行的超时时间,通过get(long timeout, TimeUnit unit),当抛出超时异常时,记录异常,往下进行其他处理逻辑,并正常释放锁。


当创建了Future实例,任务可能有以下三种状态:


  • 等待状态。此时调用cancel()方法不管传入true还是false都会标记为取消,任务依然保存在任务队列中,但当轮到此任务运行时会直接跳过。
  • 完成状态。此时cancel()不会起任何作用,因为任务已经完成了。
  • 运行中。此时传入true会中断正在执行的任务,传入false则不会中断。


总结:


Future.cancel(true)适用于:


(1) 长时间处于运行的任务,并且能够处理interruption

Future.cancel(false)适用于:

(1) 未能处理interruption的任务 ;

(2) 不清楚任务是否支持取消 ;

(3) 需要等待已经开始的任务执行完成


吊诡的事情:当超时后,我调用Future的cancel(true)方法,在本地demo测试如下代码时,均可以正常中断任务线程,但在Spring项目工程中使用时,却没有实现效果。这个坑,暂且留下,还待后面再补上。


回复吊诡的事情:这里把坑补上,其实吊诡的事情并不吊诡,主要是我事先没有一个词“能够处理interruption”或是“可中断的方法”。下面解释一下:


当一个方法内部调用了wait、sleep、join等方法时,会使得当前线程进入阻塞状态,若另外的一个线程调用被阻塞线程的interrupt方法,则会打断这种阻塞,因此这种方法有时会被称为可中断方法。记住,打断一个线程并不等于该线程的生命周期结束,仅仅是打断了当前线程的阻塞状态。


而所谓“吊诡的事情”发生,正是因为我在demo测试的代码中调用sleep方法,使得demo测试的代码成为了可中断的方法,而Spring工程中的代码,未调用sleep等类方法,也就是未进入阻塞状态,故而无法被中断。


demo测试代码

package com.xgh.demo.threaddemo;
import java.util.concurrent.*;
public class TestFuture {
    public static void main(String[] args) {
        try {
            testTimeout3(100);
            System.out.println("执行结束3。。。。");
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
    }
    public static void testTimeout1(final int num) throws InterruptedException, ExecutionException {
        ExecutorService executor = Executors.newScheduledThreadPool(5);
        Future result1 = executor.submit(new Callable() {
            @Override
            public Integer call() throws Exception {
                Thread.sleep(10000);
                System.out.println(num + "-22222222222222" + Thread.currentThread().getName());
                return num;
            }
        });
        System.out.println("下面开始判断程序是否超时或已经执行完毕。。。");
        long currentTimeMillis = System.currentTimeMillis();
        long timeout = 5 * 1000L;
        while (!result1.isDone()) {
            long timecha = System.currentTimeMillis() - currentTimeMillis;
            if (timecha >= timeout) {
                System.out.println("revoke timeout");
                boolean cancel = result1.cancel(true);
                System.out.println("revoke cancel result : --->" + cancel + " ;result isCancelled: " + result1.isCancelled());
                cancel = result1.cancel(true);
                System.out.println("revoke cancel result2 : --->" + cancel + " ;result isCancelled: " + result1.isCancelled());
                break;
            }
            if (result1.isDone()) {
                System.out.println("result1----->" + result1.get());
                System.out.println("revoke success...");
                boolean cancel = result1.cancel(true);
                System.out.println("revoke cancel result : --->" + cancel);
                break;
            }
        }
        executor.shutdown(); //关闭线程池
    }
    public static void testTimeout2(final int num) throws InterruptedException, ExecutionException {
        ExecutorService executor = Executors.newScheduledThreadPool(5);
        Future result1 = executor.submit(new Callable() {
            @Override
            public Boolean call() throws Exception {
                Thread.sleep(10000);
                System.out.println(num + "=========" + Thread.currentThread().getName());
                return true;
            }
        });
        long timeout = 5 * 1000L;
        try {
            boolean result = (boolean) result1.get(timeout, TimeUnit.MILLISECONDS);
            System.out.println("revoke success,result ----->" + result);
        } catch (TimeoutException e) {
            System.out.println("revoke timeout:" + e);
            boolean cancel = result1.cancel(true);
            System.out.println("revoke cancel result : --->" + cancel + " ;result isCancelled: " + result1.isCancelled());
        }
        executor.shutdown();
    }
    /**
     * 验证不可中断的方法
     * @param num
     * @throws InterruptedException
     * @throws ExecutionException
     */
    public static void testTimeout3(final int num) throws InterruptedException, ExecutionException {
        ExecutorService executor = Executors.newScheduledThreadPool(5);
        Future result1 = executor.submit(new Callable() {
            @Override
            public Integer call() throws Exception {
                int i= 1;
                while (true){
                    System.out.println(num + "-22222222222222" + Thread.currentThread().getName()+"=="+Thread.currentThread().isInterrupted());
                    i ++;
                    if (i == 99999999){
                        break;
                    }
                }
                return num;
            }
        });
        System.out.println("下面开始判断程序是否超时或已经执行完毕。。。");
        long currentTimeMillis = System.currentTimeMillis();
        long timeout = 2000L;
        while (!result1.isDone()) {
            long timecha = System.currentTimeMillis() - currentTimeMillis;
            if (timecha >= timeout) {
                System.out.println("revoke timeout");
                boolean cancel = result1.cancel(true);
                System.out.println("revoke cancel result : --->" + cancel + " ;result isCancelled: " + result1.isCancelled());
                cancel = result1.cancel(true);
                System.out.println("revoke cancel result2 : --->" + cancel + " ;result isCancelled: " + result1.isCancelled());
                break;
            }
            if (result1.isDone()) {
                System.out.println("result1----->" + result1.get());
                System.out.println("revoke success...");
                boolean cancel = result1.cancel(true);
                System.out.println("revoke cancel result : --->" + cancel);
                break;
            }
        }
        executor.shutdown(); //关闭线程池
    }
}


参考文章:

https://felord.blog.csdn.net/article/details/104788189

https://blog.csdn.net/u014252478/article/details/82109694

https://blog.csdn.net/qq_24630433/article/details/88537407

相关文章
|
14天前
|
Java
Java 字符串分割split空字符串丢失解决方案
Java 字符串分割split空字符串丢失解决方案
|
1月前
|
编解码 Java Apache
Java中文乱码浅析及解决方案
Java中文乱码浅析及解决方案
47 0
|
1月前
|
Java
Java中的异常链:从根源到解决方案
Java中的异常链:从根源到解决方案
37 0
|
1月前
|
搜索推荐 前端开发 Java
Java医院绩效考核系统解决方案源码
作为医院用综合绩效核算系统,系统需要和his系统进行对接,按照设定周期,从his系统获取医院科室和医生、护士、其他人员工作量,对没有录入信息化系统的工作量,绩效考核系统设有手工录入功能(可以批量导入),对获取的数据系统按照设定的公式进行汇算,且设置审核机制,可以退回修正,系统功能强大,完全模拟医院实际绩效核算过程,且每步核算都可以进行调整和参数设置,能适应医院多种绩效核算方式。
41 4
|
2月前
|
监控 安全 物联网
Java基于物联网技术的智慧工地解决方案源代码
应用先进的大数据、物联网、云计算等数字化技术,融合施工运营管理规范和技术标准,建构支撑施工和运营的一体化平台是投资、施工和运营单位能力建设的关键。应用企业架构、设计思维和软件工程方法,深入分析施工和运营技术特性与管理体系,研究开发基于大数据技术的智慧工地信息一体化平台,智慧工地管理平台是依托物联网、互联网建立的大数据管理平台,是一种全新的管理模式,能够实现劳务管理、安全施工、绿色施工的智能化和互联网化。
83 2
|
3月前
|
存储 安全 Java
深入理解HashMap:Java中的键值对存储利器
深入理解HashMap:Java中的键值对存储利器
58 0
|
3月前
|
存储 Oracle Java
Java调用存储过程小结
Java调用存储过程小结
|
2月前
|
存储 算法 Java
[Java]散列表的数据结构以及对象在JVM堆中的存储过程
[Java]散列表的数据结构以及对象在JVM堆中的存储过程
52 1
[Java]散列表的数据结构以及对象在JVM堆中的存储过程
|
2月前
|
存储 Java 索引
Java链式存储LinkedList----与ArrayList比较
Java链式存储LinkedList----与ArrayList比较
51 1
|
2月前
|
安全 Java 调度
Java中的并发编程挑战与解决方案
【2月更文挑战第5天】在日益复杂的软件开发环境中,Java作为一种广泛应用的编程语言,面临着越来越多的并发编程挑战。本文将探讨Java中常见的并发问题,并提供相应的解决方案,帮助开发人员更好地应对并发编程中的挑战。

热门文章

最新文章