# Typical Workflow for Using Transaction
__*Use Java to control transactions*__
+ use `try-catch`  
  + `try` 
    + Disable `autocommit`  
    + Execute `DML`  
    + Execute `commit` 
    + Enable `autocommit`  
  + `catch` 
    + `try` 
      + Do something after DML fails
      + Execute `rollback` 
      + Enable `autocommit`  
    + `catch` 
      + Failed to process rollback

------------------------------------------------------------
Class demo scripts:

For macOS and Linux: https://github.com/pingcap/tidb-course-201-lab/blob/master/scripts/09-demo-jdbc-execute-update-tx-01-show.sh

For Windows: https://github.com/pingcap/tidb-course-201-lab/blob/master/scripts/09-demo-jdbc-execute-update-tx-01-show.bat


We starts a transaction by disabling autocommit, using setAutoCommit(false).

The program executes DML. If they are successful, the changes are persisted to the database by the commit() method followed.

If the DML statements in the first try block fails, the program raises an exception, which is caught in catch block.

Then, the program should do anything necessary to handle the DML failure in business logic, or just simply displays a message and attempts a rollback using the rollback() method. It also re-enables autocommit mode.

The rollback itself is performed within the second try block so that if it fails, an exception is raised and caught again.


```
try {
 connection.setAutoCommit (false);
 Statement s = conn.createStatement ();
 s.executeUpdate ("DML ...");
 s.executeUpdate ("DML ...");
  s.close ();
 conn.commit ();
 conn.setAutoCommit (true);
}
catch (SQLException e) {
 // TODO custom function 
 try	{
   conn.rollback ();
   conn.setAutoCommit (true);
 }
 catch (SQLException er) {
   // TODO custom function 
 }
}
```
