今天发现服务器上的一个服务程序出现问题,软件抛出:Connection reset by peer: socket write error

无法正常提供服务,找了一下原因,原来是因为数据库服务器重启,连接池里的数据库连接connection无法创建新的Statement,导致无法提供正常服务。

到百度搜索了一下,看到一篇文章与我遇到的问题类似,解决办法虽然看上去不是很好的办法,但也实用:

解决思路:在使用前创建一个Statement,如果抛出异常,说明connection已断开,重新连接。

if (conn == null || conn.isClosed()) 
{
    conn=addConnetion()
}
else
{
    Statement st= null;
    ResultSet rs=null;
    try 
    {
        st= conn.createStatement(); //到这一步还不能确定
        rs=st.executeQuery("select 0"); //到这一步才能确定
    } 
    catch (SQLException e) 
    {
        conn=addConnetion();
    } 
    finally 
    {
        try 
        {

            if(rs!=null)
            {
                rs.close();
                rs=null;
            }

            if (st != null) 
            {
                st.close();
                st=null;
            }
        } 
        catch (SQLException e) 
        {
            e.printStackTrace();
        }
    }
}

2008-11-26

相关文章:

  • 2021-04-19
  • 2021-09-17
  • 2022-12-23
  • 2022-12-23
  • 2021-11-19
  • 2021-11-29
猜你喜欢
  • 2022-01-22
  • 2022-03-08
  • 2022-12-23
  • 2022-02-07
  • 2022-12-23
  • 2021-11-08
相关资源
相似解决方案