Connection: rollback(Savepoint savepoint) : Connection « java.sql « Java by API






Connection: rollback(Savepoint savepoint)

  
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Savepoint;
import java.sql.Statement;

public class MainClass {
  public static void main(String[] args) {
    Connection connection = null;
    Statement statement = null;
    try {
      Class.forName("org.hsqldb.jdbcDriver").newInstance();
      String url = "jdbc:hsqldb:hsqldb\\demoDatabase";
      connection = DriverManager.getConnection(url, "username", "password");
      connection.setAutoCommit(false);

      statement = connection.createStatement();

      String update1 = "UPDATE employees SET email = 'a@b.com' WHERE email = 'a@a.com'";
      statement.executeUpdate(update1);
      Savepoint savepoint1 = connection.setSavepoint("savepoint1");

      String update2 = "UPDATE employees SET email = 'b@b.com' WHERE email = 'b@c.com'";
      statement.executeUpdate(update2);
      Savepoint savepoint2 = connection.setSavepoint("savepoint2");

      String update3 = "UPDATE employees SET email = 'c@c.com' WHERE email = 'c@d.com'";
      statement.executeUpdate(update3);
      Savepoint savepoint3 = connection.setSavepoint("savepoint3");

      String update4 = "UPDATE employees SET email = 'd@d.com' WHERE email = 'd@e.com'";
      statement.executeUpdate(update4);
      Savepoint savepoint4 = connection.setSavepoint("savepoint4");

      String update5 = "UPDATE employees SET email = 'e@e.com' WHERE email = 'e@f.com'";
      statement.executeUpdate(update5);
      Savepoint savepoint5 = connection.setSavepoint("savepoint5");

      connection.rollback(savepoint3);
      connection.commit();
    } catch (Exception e) {
      e.printStackTrace();
    } finally {
      if (statement != null) {
        try {
          statement.close();
        } catch (SQLException e) {
        } // nothing we can do
      }
      if (connection != null) {
        try {
          connection.close();
        } catch (SQLException e) {
        } // nothing we can do
      }
    }
  }
}

           
         
    
  








Related examples in the same category

1.Connection.TRANSACTION_NONE
2.Connection.TRANSACTION_READ_COMMITTED
3.Connection.TRANSACTION_READ_UNCOMMITTED
4.Connection.TRANSACTION_REPEATABLE_READ
5.Connection.TRANSACTION_SERIALIZABLE
6.Connection: close()
7.Connection: commit()
8.Connection: createStatement()
9.Connection: createStatement(int resultSetType, int resultSetConcurrency)
10.Connection: getAutoCommit()
11.Connection: getCatalog()
12.Connection: getMetaData()
13.Connection: getTransactionIsolation()
14.Connection: getWarnings()
15.Connection: nativeSQL(String sql)
16.Connection: prepareCall(String sql)
17.Connection: prepareStatement(String sql)
18.Connection: rollback()
19.Connection: setAutoCommit(boolean autoCommit)
20.Connection: setSavepoint(String name)
21.Connection: setTransactionIsolation(int level)