Hibernate common face question

Source: Internet
Author: User

1. What is hibernate's concurrency mechanism? How to deal with concurrency problems?

Hibernate concurrency mechanism:

A, Hibernate session object is non-thread-safe, for a single request, a single session, a single unit of work (that is, a single transaction, a single thread), it is usually used only once, and then discarded.

If a session instance allows sharing, those that support concurrent runs, such as HTTP request,session beans, will cause resource contention.

If you have hibernate session in the HTTP session, you may have synchronous access to the HTTP session. As long as the user clicks on the browser's "refresh" quickly enough, it causes two concurrently running threads to use the same session.
b, multiple transactions concurrently access the same piece of resources, may throw the first kind of lost updates, dirty read, Phantom Read, non-repeatable read, the second class lost update a series of problems.

Solution: Set the transaction isolation level.
Serializable: Serialization. Highest Isolation Level
REPEATABLE READ: Repeatable reading
Read Committed: Committed data read
READ UNCOMMITTED: Uncommitted data reads. Worst Isolation Level
Set locks: optimistic and pessimistic locks.
Optimistic Lock: Use version number or timestamp to detect update loss, set optimistic-lock= "All" in the map to implement version checking without a version or timestamp attribute mapping, hibernate compares the status of each field of a row of records Row-Level pessimistic locks: hibernate always uses the locking mechanism of the database, never locking objects in memory! Just specify the isolation level for the JDBC connection, and then let the database take care of everything. The class Lockmode defines the different locking levels required for hibernate: Lockmode.upgrade,lockmode.upgrade_nowait,lockmode.read;

2. What is the difference between update and saveorupdate?

Update () and saveorupdate () are used for state management of the PO that crosses the session.
The object that the update () method operates on must be a persisted object. That is, the update () method cannot be used if the object does not exist in the database.
The Saveorupdate () method operates on objects that can either be persisted or made non-persisted. If the object that is persisted is called saveorupdate (), the object in the database is updated, and if this method is used by an object that is not persisted, save to the database.

3. How to convert between three states of Hibernate
When the object is a save () by a transient state (Transient), it becomes a persistent state;
When we store objects in the session, we actually save a copy of the session's map, that is, it caches a copy, and then, to the database to save a copy, in the cache this part is called persistent object (persistent). Session a close (), its cache is also closed, the entire session is invalidated, this time, the object becomes a Free State (Detached), but the database still exists.
When the Free State (Detached) is update (), it becomes a persistent state (persistent).
When the persistent state (persistent) Delete () changes to a transient state (Transient), there is no record in the database corresponding to it.

See also: http://itindex.net/detail/40384-hibernate-%E9%9D%A2%E8%AF%95

Hibernate common face question

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.