University of Cincinnati logo and link  
Transaction Details
 
  UC ingot Assume conn is an object of type Connection, stmt is an object of type Statement, and sqlStatement is a SQL update String.
 
  • To turn off autocommit:

  • conn.setAutoCommit(false);
  • Then, use your statement to execute the updates in a group.

  • stmt.executeUpdate(sqlStatement);
  • To commit:

  • conn.commit();
  • To rollback in a catch block:

  • } catch (SQLException e) {
        conn.rollback();
    }
     
  • Now that you know about SQL, try it out.
    • Add metadata to our previously developed classes.
    • Attempt transaction management.  Have an input parameter that determines whether to throw an Exception or not.  Example:
    if (request.getParameter("exception")
    .equalsIgnoreCase("true")) {
         throw new Exception ("Testing Rollback");
    }