0

当我在 glassfish 服务器上运行我的网站时,一切正常,但一段时间后它停止响应,我需要重新启动 glassfish。我认为这是我没有关闭连接的原因。有人可以告诉我这是否是问题所在吗?如果是,如何关闭它?这是我的功能之一。

public Album get_album (String title)
{
    try{
        //creates a connection to the server
        Connection cn = getCon().getConnection();
        //prepare my sql string
        String sql = "SELECT * FROM albums WHERE Title = ?";
        //create prepared statement
        PreparedStatement pst = cn.prepareStatement(sql);
        //set sql parameters
        pst.setString(1, title);
        //call the statement and retrieve results
        ResultSet rs = pst.executeQuery();
        if(rs.next()) {
            Album a = new Album();
            a.setIdAlbum(rs.getInt("idAlbum"));
            a.setTitle(rs.getString("Title"));
            a.setYear(rs.getInt("Year"));
            a.setIdArtist(rs.getInt("idArtist"));
            a.setIdUser(rs.getInt("idUser"));
            a.setLike(rs.getInt("Like"));
            a.setDislike(rs.getInt("Dislike"));
            a.setNeutral(rs.getInt("Neutral"));
            a.setViews(rs.getInt("Views"));
            return a;
        }
    }
    catch (Exception e) {
        String msg = e.getMessage();
    }
    return null;
}
4

1 回答 1

1

假设您的应用程序中的唯一错误是在使用资源后未关闭资源,您的代码应更改为:

public Album get_album (String title) {
    Connection cn = null;
    PreparedStatement pst = null;
    ResultSet rs = null;
    Album a = null;
    try{
        //creates a connection to the server
        cn = getCon().getConnection();
        //prepare my sql string
        String sql = "SELECT * FROM albums WHERE Title = ?";
        //create prepared statement
        pst = cn.prepareStatement(sql);
        //set sql parameters
        pst.setString(1, title);
        //call the statement and retrieve results
        rs = pst.executeQuery();
        if (rs.next()) {
            a = new Album();
            a.setIdAlbum(rs.getInt("idAlbum"));
            a.setTitle(rs.getString("Title"));
            a.setYear(rs.getInt("Year"));
            a.setIdArtist(rs.getInt("idArtist"));
            a.setIdUser(rs.getInt("idUser"));
            a.setLike(rs.getInt("Like"));
            a.setDislike(rs.getInt("Dislike"));
            a.setNeutral(rs.getInt("Neutral"));
            a.setViews(rs.getInt("Views"));
            //don't return inside try/catch
            //return a;
        }
    } catch (Exception e) {
        String msg = e.getMessage();
        //handle your exceptions
        //e.g. show them in a logger at least
        e.printStacktrace(); //this is not the best way
        //this will do it if you have configured a logger for your app
        //logger.error("Error when retrieving album.", e);
    } finally {
        closeResultSet(rs);
        closeStatement(pst);
        closeConnection(cn);
    }
    return a;
}

public void closeConnection(Connection con) {
    if (con != null) {
        try {
            con.close();
        } catch (SQLException e) {
            //handle the exception...
        }
    }
}

public void closeStatement(Statement st) {
    if (st!= null) {
        try {
            st.close();
        } catch (SQLException e) {
            //handle the exception...
        }
    }
}

public void closeResultSet(ResultSet rs) {
    if (rs!= null) {
        try {
            rs.close();
        } catch (SQLException e) {
            //handle the exception...
        }
    }
}
于 2013-05-31T15:28:02.997 回答