【问题标题】:Java: How to restart a db connection if it froze or disconnected?Java:如果数据库连接冻结或断开,如何重新启动它?
【发布时间】:2016-07-01 15:39:34
【问题描述】:

我很难确定我的连接何时中断,这对我来说是随机发生的。我尝试设置一个计时器来指示连接是否冻结(对于我的情况,每个使用连接的任务不应超过几秒钟),但这似乎不起作用。

我还应该注意连接,几个线程同时运行做某种任务,如果一个线程仍然活着或没有被中断并且它已经超过了完成任务的分配时间,那么,我,这表明存在连接问题。

以下是我所做工作的要点:

        public void run(){
            super.run();

            try {

                    Timer timer = new Timer(true);
                    TimerTask task = new TimerTask(){

                        @Override
                        public void run() {


                            if(isAlive() || interrupted()){//there is a problem with the connection, but I never enter this case...

                                interrupt();
                                timer.cancel();
                                //end the connection and restart it all over again
                                try {
                                    conn.close();
                                    restartConnection();
                                } catch (SQLException e) {
                                    e.printStackTrace();
                                }

                            }
                            else{//there is no problem with the connection

                            }
                        }
                    };
                    timer.schedule(task, 5000);//each task should not take anywhere near 5 secs, if it does, then I THINK there is a connection problem
                    timer.cancel();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

【问题讨论】:

    标签: java multithreading connection database-connection


    【解决方案1】:

    大多数数据库连接冻结发生在有表锁时。该连接将由该特定线程持有,直到该锁被释放。因此,下一个线程将等待来自连接池的连接,该连接池现在为空。请查看是否有任何桌锁。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-07-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多