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

What are savepoints in JDBC? explain?

WBOY
Release: 2023-09-08 21:29:06
forward
1367 people have browsed it

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);
}
Copy after login

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!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template