Flexible rollback transaction

zhaozj2021-02-16  45

Flexible rollback transaction

A coolest function of JDBC 3.0 is that it can create and use SavePoints in a transaction. SavePoints - a function of SQL for a long time - you can refine a JDBC database transaction by tagging the transaction.

So why do you want to do this? Let's take a typical booking issue. Women Ivana wants to vacation from Boston to Cancun and come back. Her booking ticket is from Boston to New York, then go to Cancun, then return, because there is no direct flight from Boston to CANCUN. The following is the step of the entire transaction: 1. Start transaction 2. Booking flight from Boston to NY 3. Booking from NY to Cancun 4. Booking from CANCun to NY 5. Book from NY to Boston's Returns 6. Submitted Transaction 7. If there is an abnormality or error, rollback transaction

But in this example, if the ticket is sold, the ticket is sold, and the entire trip will be canceled, and the rollback makes the database to the status of the first step. However, no matter how Ivana may still need some tickets, because she can take another route while returning. You can use JDBC 3.0 SavePoints to help Ivana, the whole business is as follows: 1. Start transaction 2. Booking flight from Boston to NY 3. Book a flight from NY to Cancun 4. Create SavePoint 5. Book from CANCun to NY Roide 6. Returns from NY to Boston 7. When an exception or error occurs, if SavePoint is set, roll back the transaction to the SavePoint 8 in step 4. Submit a transaction

Simply put, a SavePoint is a transaction on a special time point and provides refinement of work that completed a subset of the SQL statement in the transaction. With SavePoint, you will not roll back to a transaction's starting state, but roll back to SavePoint. You can use multiple SavePoints in a single transaction to release the Connection.releaseSesavePoint (SavePoint) method, or roll back to the entire transaction by clearing the transaction, rollback throughout the transaction. Once a SavePoint is released, try back to roll back to it will throw a SQLException exception.

To use SavePoints in your Java code, you must use JDBC 3.0, this is: • You must use JDK 1.4 because it is a core API. · Your JDBC driver must be compatible with JDBC 3.0. You can find a list of JDBC drivers in the Sun JDBC driver database of http://indunts.java.sun.com/products/jdbc/drivers.

The results of the sample code show how SavePoint in the JDBC corresponds to SQL SavePoint, and how transaction rolls back to SavePoint (see Listet 1 and Figure 1). Of course, you can use other methods to solve the booking problem, such as dividing the transactions into multiple transactions. But SavePoints is a good way to make you don't have to deal with many different transactions.

转载请注明原文地址:https://www.9cbs.com/read-22755.html

New Post(0)