【发布时间】: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