I have three methods that, together, save a node to the database. Currently, this does not achieve the desired effect of not committing the changes unless all the writes are successful.
conn.setAutoCommit(false);
writeNodeTable(node, newNodeNID);
writeContentTypeBoutTable(node, newNodeNID);
writeTerms(node, newNodeNID);
conn.commit();
If there is a problem (such as an uncaught exception is thrown) in the second or third method, I’d like to roll back all the changes. Is there some way I can use transactions to do this?
This is one such DB writing function:
private void writeTerms(DrupalNode node, int newNodeNID) throws SQLException {
String sql = "INSERT INTO term_node (nid, vid, tid) VALUES (?, ?, ?)";
PreparedStatement prep = conn.prepareStatement(sql);
prep.setInt(1, newNodeNID);
prep.setInt(2, newNodeNID);
String schoolName = termHelp.schoolOfAgainst(node.get(BoutField.against));
prep.setString(3, schoolName);
prep.execute();
String fencerName = termHelp.fencerOfAgainst(node.get(BoutField.against));
prep.setString(3, fencerName);
prep.execute();
prep.close();
}
You can use
rollbackmethod inConnectioninterface.http://java.sun.com/j2se/1.5.0/docs/api/java/sql/Connection.html#rollback()
Also you have to set
setAutoCommittofalseand callcommitandrollbackmethods as appropriately.