Understanding and coding of Hibrenate transactions, and writing of hibrenate transactions

Source: Internet
Author: User

Understanding and coding of Hibrenate transactions, and writing of hibrenate transactions

3Transaction Concept

Transaction (Transaction) Is the unit of concurrency control.Is a sequence of user-defined operations. These operations are either done or not done. They are an inseparable unit of work. Through transactions, SQL ServerYou can bind a set of logical operations to the server to maintain data integrity.

(1) Four features of transactions

I,Atomicity)

A transaction is the logical unit of work of the database. All operations included in the transaction are either done or not done.

II,Consistency)

The result of transaction execution must be that the database changes from one consistent state to another consistent state. Consistency is closely related to atomicity. (All operations in the transaction either succeed or fail)

III,Isolation)

The execution of a transaction cannot be disturbed by other transactions.

IV,Continuity/Durability)

Once a transaction is committed, its changes to the data in the database should be permanent.

Public class UserDao {Session session = null; Transaction tr = null; public static void saveuser () {try {// 1 get the session and establish a connection session = HibernateUtil. getsession (); // 2 enable transaction tr = session. beginTransaction (); // 3 create Object Class Object User user = new User (); user. setName ("admin"); user. setPwd ("123"); // 4 Use the session addition method save (write data to the database) session. save (user); // 5 submit the transaction tr. commit ();} catch (Exception e) {e. printStackTrace (); // 6 roll back the transaction tr. rollback ();} finally {// 7 close the session and disconnect HibernateUtil. closesession (session );}}

 

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.