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 );}}