No operations allowed after statement closed

不知道我的jsp 程式, 偶爾會掛掉, 錯誤訊息:

SQLException: java.sql.SQLException: No operations allowed after statement closed.

網路上找的解法:
https://stackoverflow.com/questions/23323653/no-operations-allowed-after-statement-closed

Create a Utility class for connection management to manage it at single point in whole application.

Don’t load the DataSource every time you need a new connection.

Sample code:

Connection conn = null;
PreparedStatement stmt = null;
ResultSet rs = null;
try {
    conn = ConnectionUtil.getInstance().getConnection();
    //your-code-here
} finally {
    if (rs != null) {
        rs.close();
    }
    if (stmt != null) {
        stmt.close();
    }
    if (conn != null) {
        ConnectionUtil.getInstance().close(conn);
    }
}

Always close the connection and handle it in try-catch-finally.

ResultSet 的部份, 建議使用 CachedRowSet, 問題會較少。

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *