日期:2014-05-18  浏览次数:20575 次

网络断了,怎样重复连接数据库
public   class   JdbcUtil   {
private   static   Log   log   =   LogFactory.getLog(JdbcUtil.class);
private   static   BasicDataSource   dsdb2   =   null;

static   void   initDS()   {
log.info( "db   init   start ");
LoadProperty.readProperty();//这句是读配置文件
dsdb2   =   new   BasicDataSource();
dsdb2.setMaxActive(4);
dsdb2.setMaxIdle(2);
        dsdb2.setUrl(LoadProperty.db2url);
dsdb2.setDriverClassName(LoadProperty.db2driver);
dsdb2.setUsername(LoadProperty.db2user);
dsdb2.setPassword(LoadProperty.db2password);

}

public   synchronized   static   Connection   getConnection()   {
if   (dsdb2   ==   null)   {
initDS();
}
Connection   con=null;
try   {
con=   dsdb2.getConnection();         //这句连不上就会抛异常不会执行下面的
if(con.isClosed()){
log.info( "   DPCA   db   connection   interrupt   ");
initDS();
con   =   dsdb2.getConnection();
}

}   catch   (SQLException   e)   {
log.info( "   connection   DPCA   db   error ");
e.printStackTrace();

}
return   con;
}




------解决方案--------------------
这段时间刚学一个quartz-1.6.0.jar的东西,网断了以后,看看能不能用这个东西做一个代码,然他来监控如果恢复连接后,在执行下面的程序