简体   繁体   English

使用我的网站一段时间后,我需要重新启动glassfish

[英]I need to restart glassfish after using for some time my website

When i run my website it on glassfish server everything works fine but after some time its stop responding and I need to restart glassfish.. I think its cause I not closing the connection. 当我在glassfish服务器上运行我的网站时,一切正常,但是一段时间后它停止响应,我需要重新启动glassfish。.我认为其原因是我没有关闭连接。 Can someone tell me if this is the problem? 有人可以告诉我这是否是问题吗? if yes how to close it? 如果是,如何关闭呢? Here is one of my function. 这是我的职责之一。

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;
}

Assumming the unique error in your application is for not closing the resources after using them, your code should change to: 假设应用程序中的唯一错误是使用完资源后没有关闭资源,则代码应更改为:

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...
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 每次使用 sendmail function 后都需要重启 glassfish - Need to restart glassfish after each time sendmail function is used NoClassDefFoundError:Glassfish重新启动后,SupportedEllipticPointFormatsExtension - NoClassDefFoundError: SupportedEllipticPointFormatsExtension after Glassfish restart Glassfish 3.1.2.2会在一段时间后更改其默认登录域 - Glassfish 3.1.2.2 changes it's default login realm after some time 为什么每次更改/编辑 java servlet 代码时都需要重新启动 Tomcat Apache 服务器 - Why I need to restart Tomcat Apache server every time I change/edit my java servlet code 在系统环境的路径变量中设置JAVA后,是否需要重新启动系统? - Do I need to restart my system after setting JAVA in system environment's path variable? 除非关闭浏览器或重新启动我的Glassfish,否则我无法重新登录JSF页面 - I can't re-login to a JSF page unless I close my browser or restart my Glassfish Glassfish 服务器在重新启动后不会启动 - The Glassfish server won't start after a restart 每隔几个小时设置一个 cron 并在一段时间后重新启动 - Set a cron every certain hours and restart after some time 每次更改资源时都需要重新启动服务器吗? - do I need to restart the server every time i change a resource? 数据库重启后Tomcat需要重启 - Tomcat need to restart after database restart
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM