Jdbc oracle Transaction Processing
Database transactions:
In a database, transactions refer to a group of logical operation units that transform data from one state to another.
To ensure data consistency in the database, data manipulation should be a discrete logical unit in a group: when it is all
Data Consistency can be maintained upon completion. When some operations in this unit fail, the entire transaction should be fully considered
It is an error. All operations after the starting point should be rolled back to the starting status.
Transaction operation: first define to start a transaction, and then modify the data. At this time, if you COMMIT (COMMIT), these modifications
It will be permanently saved. If ROLLBACK is used, the database management system will discard all repairs.
Returns to the State at the start of the transaction.
ACID properties of transactions
1. Atomicity refers to the transaction being an inseparable unit of work, and operations in the transaction either occur,
Or none.
2. Consistency transactions must transform the database from one consistent state to another.
3. Isolation refers to the Isolation of a transaction, that is, the execution of a transaction cannot be disturbed by other transactions.
Internal operations and data used are isolated from other concurrent transactions.
Transactions cannot interfere with each other.
4. Durability: Once a transaction is committed, its changes to data in the database are permanent,
Other operations and database faults should not have any impact on them.
Transaction: a set of operations that constitute a single logical unit of work.
Transaction Processing: ensure that all transactions are executed as a unit of work. Even if a fault occurs, this execution method cannot be changed.
When multiple operations are performed in a transaction, either all the transactions are committed or the entire transaction is rolled back to the original state.
When a connection object is created, the transaction is automatically committed by default: each time an SQL statement is executed, if the execution is successful
The database is automatically submitted, but cannot be rolled back.
To execute multiple SQL statements as a transaction:
Call setAutoCommit (false) of the Connection object to cancel Automatic transaction commit.
After all SQL statements are successfully executed, call the commit (); Method to submit the transaction.
When an exception occurs, the rollback () method is called to roll back the transaction.
If the Connection is not closed at this time, you need to restore its automatic submission status.
Common Code Structure
Public void test (String SQL, Object... args) {Connection conn = null; PreparedStatement prepareStatement = null; try {conn = getConn (); // before the transaction is processed, the default commit behavior conn for canceling the Connection is canceled. setAutoCommit (false); prepareStatement = conn. prepareStatement (SQL); for (int I = 0; I <args. length; I ++) {prepareStatement. setObject (I + 1, args [I]);} prepareStatement.exe cuteUpdate (); // transaction processing: if the transaction is processed successfully, commit the transaction conn. commit ();} catch (Exception e) {e. printStackTrace (); try {// transaction processing: if an exception occurs, the catch Block rolls back the transaction conn. rollback () ;} catch (SQLException e1) {e1.printStackTrace () ;}} finally {// Release Database Resource releaseSource (prepareStatement, conn, null );}}