Home >Database >Mysql Tutorial >What are savepoints in JDBC? explain?

What are savepoints in JDBC? explain?

WBOY
WBOYforward
2023-09-08 21:29:061352browse

JDBC 中的保存点是什么?解释?

The Savepoint interface gives you additional transaction control. Most modern DBMSs support savepoints in their environment, such as Oracle's PL/SQL.

When you set a savepoint, you define a logical rollback point in the transaction. If an error occurs after a savepoint, you can use the rollback method to undo all changes or only the changes made after the savepoint.

The Connection object has two new methods to help you manage savepoints -

  • setSavepoint(String savepointName): Define a new savepoint . It also returns a Savepoint object.

  • releaseSavepoint(Savepoint savepointName): Delete a Savepoint. Note that it requires a Savepoint object as parameter. This object is usually a savepoint generated by the setSavepoint() method.

There is a rollback(String savepointName) method, which is used to roll back the work to the specified savepoint.

Example

try {
   //Assume a valid connection object conn
   conn.setAutoCommit(false);
   Statement stmt = conn.createStatement();
   //set a Savepoint
   Savepoint savepoint1 = conn.setSavepoint("Savepoint1");
   String SQL = "INSERT INTO Employees " + "VALUES (106, 20, 'Rita', 'Tez')";
   stmt.executeUpdate(SQL);
   //Submit a malformed SQL statement that breaks
   String SQL = "INSERTED IN Employees " + "VALUES (107, 22, 'Sita', 'Tez')";
   stmt.executeUpdate(SQL);
   // If there is no error, commit the changes.
   conn.commit();
} catch(SQLException se){
   // If there is any error.
   conn.rollback(savepoint1);
}

The above is the detailed content of What are savepoints in JDBC? explain?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete