請讓我知道我們什麼時候需要調用方法connection.rollback();什麼時候應該調用connection.rollback()方法?
try{
connection = getConnection();
connection.setAutoCommit(false);
pstmt1 = connection.preparedstatement (...);
...
pstt1.executeUpdate();
pstmt2 = connection.preparedstatement (...);
...
pstt2.executeUpdate();
connection.commit();
}catch (Exception sqe) { sqe.printStacktrace();
}finally {
closeQuitely (pstmt1);
closeQuitely (pstmt2);
closeQuitely (connection);
}
在上面的代碼中,我們沒有使用connection.rollback(),但如果出現一些異常,甚至那麼一切都將正常工作[我猜],COS連接在自動提交=虛假模式已設置。
那麼當我們需要使用這種方法時可能會出現什麼情況。請張貼示例。
簡單的回答:程序員不應該手動調用rollback()或commit()。 – SteveD 2009-09-02 10:14:15
除了學習JDBC,編寫庫/框架或者做一些關鍵性能,我不會推薦使用原始JDBC--使用Spring,JPA或其他框架來自動處理事務和連接。這太容易搞砸了互動,並有大量的樣板代碼。 – SteveD 2009-09-02 10:17:43