Java Mysql 简明教程
Java & MySQL - SavePoint
新的 JDBC 3.0 Savepoint 接口为您提供了额外的交易控制。大多数现代 DBMS 都在其环境中支持保存点,例如 Oracle 的 PL/SQL。
The new JDBC 3.0 Savepoint interface gives you the additional transactional control. Most modern DBMS, support savepoints within their environments such as Oracle’s PL/SQL.
当你设置保存点时,你要在事务中定义一个逻辑回滚点。如果在保存点之后发生错误,则可以使用回滚方法来撤消所有更改或仅撤消保存点之后作出的更改。
When you set a savepoint you define a logical rollback point within a transaction. If an error occurs past a savepoint, you can use the rollback method to undo either all the changes or only the changes made after the savepoint.
Connection 对象有两个可帮助你管理保存点的新方法:
The Connection object has two new methods that help you manage savepoints −
-
setSavepoint(String savepointName) − Defines a new savepoint. It also returns a Savepoint object.
-
releaseSavepoint(Savepoint savepointName) − Deletes a savepoint. Notice that it requires a Savepoint object as a parameter. This object is usually a savepoint generated by the setSavepoint() method.
有一个 rollback (String savepointName) 方法,它将工作回滚到指定的保存点。
There is one rollback (String savepointName) method, which rolls back work to the specified savepoint.
以下示例说明了 Savepoint 对象的使用 −
The following example illustrates the use of a Savepoint object −
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);
}
在这种情况下,上述 INSERT 语句都不会成功,并且所有内容都将被回滚。
In this case, none of the above INSERT statement would success and everything would be rolled back.
以下示例利用了所述 setSavepoint 和 rollback 。
Following is the example, which makes use of setSavepoint and rollback described.
此示例代码基于先前章节中完成的环境和数据库设置编写。
This sample code has been written based on the environment and database setup done in the previous chapters.
将以下示例复制并粘贴到 TestApplication.java 中,如下进行编译和运行:
Copy and paste the following example in TestApplication.java, compile and run as follows −
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Savepoint;
import java.sql.Statement;
public class TestApplication {
static final String DB_URL = "jdbc:mysql://localhost/TUTORIALSPOINT";
static final String USER = "guest";
static final String PASS = "guest123";
static final String QUERY = "SELECT id, first, last, age FROM Employees";
static final String DELETE_QUERY = "DELETE FROM Employees WHERE ID = 8";
static final String DELETE_QUERY_1 = "DELETE FROM Employees WHERE ID = 9";
public static void printResultSet(ResultSet rs) throws SQLException{
// Ensure we start with first row
rs.beforeFirst();
while(rs.next()){
// Display values
System.out.print("ID: " + rs.getInt("id"));
System.out.print(", Age: " + rs.getInt("age"));
System.out.print(", First: " + rs.getString("first"));
System.out.println(", Last: " + rs.getString("last"));
}
System.out.println();
}
public static void main(String[] args) {
// Open a connection
try(Connection conn = DriverManager.getConnection(DB_URL, USER, PASS);
Statement stmt = conn.createStatement(
ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_UPDATABLE);
) {
conn.setAutoCommit(false);
ResultSet rs = stmt.executeQuery(QUERY);
System.out.println("List result set for reference....");
printResultSet(rs);
// delete row having ID = 8
// But save point before doing so.
Savepoint savepoint1 = conn.setSavepoint("ROWS_DELETED_1");
System.out.println("Deleting row....");
stmt.executeUpdate(DELETE_QUERY);
// Rollback the changes after save point 1.
conn.rollback(savepoint1);
// delete rows having ID = 9
// But save point before doing so.
conn.setSavepoint("ROWS_DELETED_2");
System.out.println("Deleting row....");
stmt.executeUpdate(DELETE_QUERY_1);
rs = stmt.executeQuery(QUERY);
System.out.println("List result set for reference....");
printResultSet(rs);
// Clean-up environment
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
现在,让我们编译上述示例,如下 −
Now, let us compile the above example as follows −
C:\>javac TestApplication.java
C:\>
运行 TestApplication 时,它会生成以下结果:
When you run TestApplication, it produces the following result −
C:\>java TestApplication
List result set for reference....
ID: 1, Age: 23, First: Zara, Last: Ali
ID: 2, Age: 30, First: Mahnaz, Last: Fatma
ID: 3, Age: 35, First: Zaid, Last: Khan
ID: 4, Age: 33, First: Sumit, Last: Mittal
ID: 5, Age: 40, First: John, Last: Paul
ID: 7, Age: 20, First: Sita, Last: Singh
ID: 8, Age: 20, First: Rita, Last: Tez
ID: 9, Age: 20, First: Sita, Last: Singh
Deleting row....
Deleting row....
List result set for reference....
ID: 1, Age: 23, First: Zara, Last: Ali
ID: 2, Age: 30, First: Mahnaz, Last: Fatma
ID: 3, Age: 35, First: Zaid, Last: Khan
ID: 4, Age: 33, First: Sumit, Last: Mittal
ID: 5, Age: 40, First: John, Last: Paul
ID: 7, Age: 20, First: Sita, Last: Singh
ID: 8, Age: 20, First: Rita, Last: Tez
C:\>