To ensure the safty, do I need to write all the database accessing code like this:
public void updateOrder(OrderData order) throw Exception {
....
coon.setAutoCommit(false);
Statement stmt = conn.createStatement();
String sql = "update order set .... where ";
try {
stmt.executeUpdate(sql);
conn.commit();
} catch (Exception e) {
conn.rollback();
} finally {
}
}
In this example, I have only access to one table, do I still need to treat it as transaction? Do I need to write all the database access as transaction like this?
That depends if there are multiple sql updates and inserts or not. If you are executing a single update or insert it will implicitly run within a transaction.