Hibernate transaction Processing

Source: Internet
Author: User

Hibernate transaction Processing:
Transaction:
* A transaction is a logical set of operations, either all succeed or all fail!!!

Transaction characteristics:
* atomicity: Transaction A set of operations is inseparable.
* Consistency: Data integrity must be consistent before and after the execution of a transaction.
* Isolation: A transaction should not be interfered with by other transactions during execution.
* Persistence: Once the transaction is complete, the data is persisted to the database.

Some security issues are raised if the isolation of the transaction is not considered:
* 5 Types of questions: Class 3 read Problem 2 class write problem.
* Read questions:
* Dirty read: One transaction reads uncommitted data to another transaction.
* Non-REPEATABLE READ: One transaction reads to another transaction has committed data (update), resulting in inconsistent query results.
* Virtual read: One transaction reads data that has been committed by another transaction (insert), resulting in inconsistent query results

* Avoid three types of reading problems:
* Set the isolation level of the transaction:
* Non-submitted read: The above three kinds of reading problems can occur.
* Read-committed: avoid dirty reads, but non-repeatable reads and virtual reads can occur.
* Repeat read: Avoid dirty reads and non-repeatable reads, but virtual reading is possible.
* Serial: Can avoid the above three kinds of reading problems.

* Set the isolation level of the transaction in hibernate:
* In the core configuration file:
<property name= "Hibernate.connection.isolation" >4</property>

Write Problem: Missing update
Solution
* Pessimistic Lock: When a transaction is in progress, other transactions do not allow the data to be modified.

@Test/** Use pessimistic locks to resolve lost updates*/ Public voidDemo1 () {Session session=hibernateutils.opensession (); Transaction TX=session.begintransaction (); //Use pessimistic lock (pat him lock)Customer customer = (customer) session.get (customer).class, 3, Lockmode.upgrade); Customer.setage (32);    Tx.commit ();    Session.close ();} @Test/** Use pessimistic locks to resolve lost updates*/ Public voidDemo2 () {Session session=hibernateutils.opensession (); Transaction TX=session.begintransaction (); //Use pessimistic lock (pat him lock)Customer customer = (customer) session.get (customer).class, 3, Lockmode.upgrade); Customer.setcname ("Old Tom");    Tx.commit (); Session.close ();}


* Optimistic Lock:

Add a sentence <version name= "version"/> in the mapping file;

Add an attribute version to the Customer entity class

The label version is fixed, and the attribute in name can be arbitrarily named, and version is better for easy differentiation.

@Test/** Use optimistic locks to resolve lost updates*/ Public voidDemo3 () {Session session=hibernateutils.opensession (); Transaction TX=session.begintransaction (); Customer Customer= (customer) Session.get (customer.class, 3); Customer.setage (26);    Tx.commit (); Session.close ();} @Test/** Use optimistic locks to resolve lost updates*/ Public voidDemo4 () {Session session=hibernateutils.opensession (); Transaction TX=session.begintransaction (); Customer Customer= (customer) Session.get (customer.class, 3); Customer.setcname ("Old Tom");    Tx.commit (); Session.close ();}

A clearer story about pessimistic and optimistic locks:

Pessimistic lock (pessimistic lock), as the name implies, is very pessimistic, every time to take the data when they think others will change, so every time when the data are locked, so that others want to take this data will block until it gets the lock. Traditional relational database in the use of a lot of this locking mechanism, such as row locks, table locks, read locks, write locks, etc., are in operation before the lock.

Optimistic lock (optimistic lock), as the name implies, is very optimistic, every time to take the data when they think others will not be modified, so will not be locked, but in the update will be judged in the period when others have to update this data, you can use the version number and other mechanisms. Optimistic locking is useful for multi-read application types, which can improve throughput, such as the fact that a database provides an optimistic lock similar to the write_condition mechanism.

The two kinds of locks have advantages and disadvantages, not to think of one better than the other, like the optimistic lock for less write, that is, the conflict really rarely occurs, this can save the lock overhead, increase the overall system throughput. However, if there is frequent conflict, the upper application will continue to retry, which is to reduce the performance, so in this case, pessimistic locking is more appropriate.

Original :http://blog.csdn.net/hongchangfirst/article/details/26004335

In addition to the level two cache,

Hibernate transaction Processing

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.