再谈远程数据库连接超时问题解决
2014-05-13 08:51
253 查看
经过前期的代码完善,远程数据一旦超时,就要及时释放数据库连接而且要及时关闭.对代码进行了优化把所有关闭及解锁都放在finally块里.代码如下/**
* 更新时间(sms_sent)
* @param applicationId
* @throws SQLException
*/
public synchronized void updateStatusTime(String applicationId) throws SQLException {
this.send_lock.writeLock().lock();
ExecutorService exeServ=Executors.newFixedThreadPool(1, new CustomThreadFactory("TaskEngine-timeout-scheduler", true));
Future future=null;
String sql = "update sms_sent set STATUSTIME=REQUESTTIME where MASSMSID=?";
Connection con = null;
PreparedStatement pstmt = null;
try {
con= ConnectionManager.getConnection();
pstmt = con.prepareStatement(sql);
pstmt.setString(1, applicationId);
future=exeServ.submit(new ExecuteUpdate(pstmt));
future.get(timeout,TimeUnit.MILLISECONDS);
} catch (Exception e) {
throw new SQLException(e);
} finally {
try { pstmt.close(); }
catch (Exception e) { e.printStackTrace(); }
try { con.close(); }
catch (Exception e) { e.printStackTrace(); }
exeServ.shutdown();
this.send_lock.writeLock().unlock();
}
}
下面是实现Callable的类
private class ExecuteUpdate implements Callable{
private PreparedStatement pstmt;
public ExecuteUpdate(PreparedStatement pstmt){
this.pstmt=pstmt;
}
public Integer call() throws Exception {
return pstmt.executeUpdate();
}
}
* 更新时间(sms_sent)
* @param applicationId
* @throws SQLException
*/
public synchronized void updateStatusTime(String applicationId) throws SQLException {
this.send_lock.writeLock().lock();
ExecutorService exeServ=Executors.newFixedThreadPool(1, new CustomThreadFactory("TaskEngine-timeout-scheduler", true));
Future future=null;
String sql = "update sms_sent set STATUSTIME=REQUESTTIME where MASSMSID=?";
Connection con = null;
PreparedStatement pstmt = null;
try {
con= ConnectionManager.getConnection();
pstmt = con.prepareStatement(sql);
pstmt.setString(1, applicationId);
future=exeServ.submit(new ExecuteUpdate(pstmt));
future.get(timeout,TimeUnit.MILLISECONDS);
} catch (Exception e) {
throw new SQLException(e);
} finally {
try { pstmt.close(); }
catch (Exception e) { e.printStackTrace(); }
try { con.close(); }
catch (Exception e) { e.printStackTrace(); }
exeServ.shutdown();
this.send_lock.writeLock().unlock();
}
}
下面是实现Callable的类
private class ExecuteUpdate implements Callable{
private PreparedStatement pstmt;
public ExecuteUpdate(PreparedStatement pstmt){
this.pstmt=pstmt;
}
public Integer call() throws Exception {
return pstmt.executeUpdate();
}
}
相关文章推荐
- java的远程数据库连接,超时问题解决(转载请注明原处)
- 解决Silverlight连接数据库超时并报错问题
- jconsole远程连接超时问题解决方法
- 解决SQL Server管理器无法连接远程数据库的问题
- SpagoBI数据库连接超时问题解决
- 【数据库】LNMP一键包安装后解决MySQL无法远程连接问题
- 数据库远程连接错误26的问题解决,终于搭好工作环境了
- 解决SQL Server管理器无法连接远程数据库的问题
- 解决MySQL 5数据库连接超时问题
- jconsole远程连接超时问题解决方法
- 解决ubuntu下mysql不能远程连接数据库的问题
- 解决ubuntu下mysql不能远程连接数据库的问题
- 解决MSSQL2005远程连接sql2000非默认端口数据库的问题
- 【杂谈\Java\数据库】SpringMVC 连接MySQL超时问题解决方案
- 解决Mac下使用iTerm连接远程机器超时自动断开的问题
- 远程数据库连接不通问题解决步骤
- 解决"System.AccessViolationException”类型的未经处理的异常在 未知模块(IIS Worker Process 已停止工作)导致无法连接远程数据库的问题
- 解决ubuntu下mysql不能远程连接数据库的问题
- 解决MSSQL2005远程连接sql2000非默认端口数据库的问题
- 关于在英创em9161板上使用ADO.net连接远程数据库提示 “无法找到 PInvoke dll"dbnetlib.dll"”问题解决方法