Java--JDBC Learning--Transactions

Source: Internet
Author: User

Database transactions

In the database, the so-called transaction refers to a set of logical operations units that transform the data from one state to another.
To ensure consistency of data in the database, data manipulation should be a discrete group of logical units: when it is complete, the data consistency can be maintained, and when a part of the operation fails, the entire transaction should be treated as an error, all operations from the starting point should be rolled back to the start state.
Transaction operations: First define the start of a transaction, and then modify the data, if committed, these changes are permanently saved, if fallback (ROLLBACK), the database management system will discard all the modifications made to the start of the transaction state.

Acid (acid) properties of a transaction
    1. Atomicity (atomicity) atomicity means that a transaction is an inseparable unit of work, and the operations in the transaction either occur or do not occur.
    2. The consistency (consistency) transaction must transform the database from one consistent state to another.
    3. The isolation of an isolation (isolation) transaction is that the execution of one transaction cannot be disturbed by other transactions, that is, the operations inside a transaction and the data used are isolated from other transactions that are concurrent, and cannot interfere with each other in the execution of the transaction.
    4. Persistence (durability) persistence refers to the fact that once a transaction is committed, its changes to the data in the database are permanent, and the subsequent operations and database failures should not have any effect on it.
JDBC things Processing

Transactions: Refers to the set of operations that make up a single logical unit of work.
Transactions: Ensure that all transactions are executed as a unit of work, even if there is a failure, this execution cannot be changed. When multiple operations are performed in a transaction, either all transactions are committed (commit), or the entire transaction is rolled back (rollback) to its original state.
When a connection object is created, the transaction is automatically committed by default: each time an SQL statement is executed, it is automatically committed to the database and cannot be rolled back if successful.
In order for multiple SQL statements to be executed as a transaction:
Call the Setautocommit (false) of the Connection object; To cancel the automatic submission of transactions;
After all the SQL statements have been executed successfully, a commit () is called; method to commit the transaction;
When an exception occurs, call rollback (); method to roll back the transaction;
If Connection is not turned off at this point, it needs to resume its autocommit status.

About transactions:
1. If there are multiple operations, each operation uses its own separate connection, the transaction cannot be guaranteed.
2. Specific steps:
1. Start transaction before transaction operation: Cancels the default commit behavior of Connection. Connection.setautocommit (FALSE);
2). If the operation of the transaction succeeds, the transaction is committed: Connection.commit ();
3). ROLLBACK TRANSACTION: If an exception occurs, the transaction is rolled back in the catch block:

As an example:

 Public voidtesttransaction () {Connection Connection=NULL; Try{Connection=jdbctools.getconnection ();            System.out.println (Connection.getautocommit ()); //start transaction: Cancels the default commit.Connection.setautocommit (false); String SQL= "UPDATE users SET balance =" + "balance-500 WHERE id = 1";            Update (connection, SQL); inti = 10/0;            System.out.println (i); SQL= "UPDATE users SET balance =" + "balance + WHERE id = 2";            Update (connection, SQL); //Commit a transactionConnection.commit (); } Catch(Exception e) {e.printstacktrace (); //rolling back a transaction            Try{connection.rollback (); } Catch(SQLException E1) {e1.printstacktrace (); }        } finally{jdbctools.releasedb (NULL,NULL, connection); }

Java--JDBC Learning--Transactions

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.