Hibernate notes transactions

Source: Internet
Author: User

The concept of a transaction: The work of a transaction value in a unit, which is either completed or not completed.

ACID properties of transactions: atomicity, consistency, isolation, and Durability

Simple understanding:

1. either completed or not completed

2. All data has a consistent state

3. One transaction cannot view the data being modified by another transaction

4. Write to database

Hibernate encapsulates the underlying jdbctransaction and jtatransaction, and then overwrites the transaction and session shells on the outside. In fact, the underlying JDBC or JTA

JDBC is used by default.

Transaction TRAN = session. begintransaction ();

...

Tran. Commit ();/tran. rollback ();

JTA

Occasion:

1. A Query calls several databases and requires a Distributed Transaction

2. Long transactions across sessions

Usage:

1. In the hibernate. properties File

Hibernate. transaction. factory_class org. hibernate. transaction. jtatransactionfactory

Or in the hibernate. cfg. xml file.

<Session-factory>

<Property name = "hibernate. transaction. factory_class">

Org. hibernate. transaction. jtatransactionfactory

</Property>

</Session-factory>

2.

Application Interface: usertransaction, transactionmanager, transaction

javax.transaction.UserTransaction tx = null;tx = new initialContext().lookup(” javax.transaction.UserTransaction ”) ;tx.begin();Session s1 = sf.openSession(); ……s1.flush();  s1.close();Session s2 = sf.openSession();……s2.flush();  s2.close();tx.commit();

When talking about transactions, concurrency control is essential !!!

Four concurrency problems:

1. Update loss (read together and change each)

A and B are reading a document, but a has modified the document and B wants to modify the document, so a's modifications will be lost,

Therefore, B must reload the modification.

2. Dirty read (one read, one change)

When one transaction selects the row in which another transaction is being updated,

Therefore, before modifying the data, make sure that no one can read the changed data.

3. Non-repeated read (read)

After the author completes editing, the editor can read the document.

4. Phantom read (one change, one read)

When you delete an inert row, the row belongs to the row being read by a transaction.

Alleviate concurrency problems:

I. hibernate isolation level

<Session-factory>

<Property name = "hibernate. Connection. isolaction"> 1 </property>

</Session-factory>

Prevent

1. Lost and new

2 Dirty read + lost and new

3 Non-repeated read + 2

4 1 + 2 + 3

Ii. set several small locks

Lockmode. None checks the cache by default.

Lockmode. Read: select the database directly and check the version.

Lockmode. upgrade is the same as above. However, if the database supports pessimistic locks, select for update is used.

Lockmode. upgrade_nowait is the same as above. However, if the database supports pessimistic locks, select for update Nowait.

Lockmode. Write automatic usage

Lockmode. Force forces the version value to be added to records using version.

Usage:

Row: Session. Load (..)

Object: Session. Load (); Session. Lock (object, lock)

Query: Session. createquery (). setlockmode (). List ();

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.