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;
}
添加'finally'條款關閉連接。更好的是,使用嘗試。 – 2013-05-31 15:05:54
不確定這是否因爲你沒有關閉連接(作爲唯一的原因),但是肯定會產生很多開銷,特別是如果你的'getCon()。getConnection()'使用'Class.forName(「 ...「); Driver.getConnection()'而不是從數據庫連接池中檢索數據庫連接。 –
你能告訴我如何使用我的函數來解決它嗎? – a1204773