2013-05-31 17 views
0

當我運行我的網站它在玻璃魚服務器一切正常,但一段時間後停止響應,我需要重新啓動玻璃魚..我認爲它的原因我不關閉連接。有人能告訴我這是否是問題嗎?如果是的話如何關閉它?這是我的一個功能。我需要重新啓動玻璃魚使用一段時間後我的網站

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

添加'finally'條款關閉連接。更好的是,使用嘗試。 – 2013-05-31 15:05:54

+0

不確定這是否因爲你沒有關閉連接(作爲唯一的原因),但是肯定會產生很多開銷,特別是如果你的'getCon()。getConnection()'使用'Class.forName(「 ...「); Driver.getConnection()'而不是從數據庫連接池中檢索數據庫連接。 –

+0

你能告訴我如何使用我的函數來解決它嗎? – a1204773

回答

1

Assumming在應用程序中唯一的錯誤是使用後不關閉的資源,你的代碼更改爲:

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