Siege Lion on the Way (one) Hibernate (17)---hibernate concurrency processing problem

Source: Internet
Author: User

concurrency problems for one or more transactions Concurrency Runtime :
Summarizes the first category of missing updates, dirty reads, virtual reads, non-repeatable reads, and second-class missing updates.
1, the first category of missing updates: When a transaction is revoked, the other transaction has committed the updated data overwritten.
2, Dirty read: One transaction read to another transaction uncommitted update data.
3, Virtual read (Phantom Read): One transaction read to another transaction has committed the newly inserted data.
4, non-repeatable READ: One transaction reads the updated data that has been committed by another transaction.
5, the second category of missing updates: This is a special case of non-repeatable read, one transaction overwrites the updated data submitted by another transaction.
Second, the basic principle of the database system lock :
The database system adopts the lock to realize the isolation of the transaction.
1, the basic principle of the lock is as follows :
A, when a transaction accesses a database resource, if a SELECT statement is executed, a shared lock must be obtained first, and an exclusive lock must be obtained if an INSERT, update, or DELETE statement is executed.
B, when the second transaction also accesses the same resource, if a SELECT statement is executed, a shared lock must be obtained first, and an exclusive lock must be obtained if the inert, update, or DELETE statements are executed. At this point, depending on the type of lock already placed on the resource, you decide whether the second transaction should wait or get the lock immediately.
A description of the second transaction acquiring a lock:

The already placed lock on the resource second transaction read operation second transaction for update operation
get shared lock now get an exclusive lock now
shared lock Get a shared lock now wait for the first transaction to unlock the shared lock
exclusive lock wait for first transaction to unlock exclusive lock Wait for the first transaction to unlock the exclusive lock


2, lock Multi-granularity and automatic lock upgrade :
The resources that the database system can lock include: database, table, region, page, key value (refers to row data with index), row.
Depending on the granularity of the locked resource, the lock can be divided into the following types:
A, database-level Lock: Locks the entire database.
B, table-level Lock: Locks a database table.
C, Zone-level lock: Locks a specific area of the database.
D, page-level lock: Locks a specific page of the database.
E, Key-level lock: Locks a row of records with indexes in the database table.
F, row-level lock: Locks a single row of records in a database table.
The greater the granularity of locks, the higher the isolation between transactions, and the lower the concurrency performance between transactions.
Lock escalation refers to adjusting the granularity of locks and replacing multiple low-granularity locks with a few higher-granularity locks to reduce system load.
3, the type of lock and compatibility :
A, shared Lock:
Shared locks are used for read data operations, which are non-exclusive, allowing other transactions to read their locked resources at the same time, but do not allow other transactions to update it.
B, exclusive Lock:
An exclusive lock is also called an exclusive lock, which is suitable for modifying data. The resources it locks, other transactions cannot be read, and cannot be modified.
C, update the Lock:
Update locks are used during the initialization phase of an update operation to lock resources that might be modified, which avoids deadlocks caused by shared locks.
Summary of features for shared, exclusive, and update locks:

Feature items Shared locks Exclusive lock Update lock
Lock condition When a transaction executes a SELECT statement, the database assigns a shared lock to the transaction to lock the queried data. When a transaction executes a inert, update, DELETE statement, an exclusive lock is assigned. An update lock is assigned when a transaction executes an UPDATE statement
Unlock condition After the general data is read, the database system immediately unlocks the shared lock. When querying multiple records, it is also a one-lock-unlock. Exclusive locks are not lifted until the end of the transaction. After the data has been read, the update lock is upgraded to an exclusive lock when the update operation is performed.
Compatibility with other locks If a shared lock is placed on the data resource, the shared and update locks can be placed again. Cannot be compatible with other locks. Compatible with shared locks, but can only have one update lock, which avoids deadlocks
Concurrency performance has good concurrency performance Poor concurrency Performance


4. deadlock and its prevention and cure methods :
In a database system, a deadlock is one in which multiple transactions lock a resource and try to request a lock on a resource that is already locked, resulting in a lock request loop that causes multiple transactions to wait for the other party to release the state of the locked resource.
Many database systems can automatically periodically search for and handle deadlock problems, and when a lock request ring is detected, the system ends the transaction with the lowest deadlock priority and revokes the transaction.
Ways to avoid deadlocks:
A, reasonable arrangement of table access order.
B, use short transactions.
C, if the consistency of the data requirements are not very high, you can allow dirty read.
D, if possible, stagger the time at which multiple transactions access the same data resource to prevent lock collisions.
E, use the lowest possible transaction isolation level.

Third, the isolation level of the database :
In general, you should first consider having your database system automatically manage locks.
The database provides 4 levels of transaction isolation for users to choose from:

Isolation level Is there a first class of missing updates is dirty read Whether to read the virtual is not repeatable read is the second category missing updates
Serializable Whether Whether Whether Whether Whether
Repeatable Read Whether Whether Is Whether Whether
Read commited Whether Whether Is Is Is
Read uncommited Whether Is Is Is Is


1. set the relevant statement for the isolation level in Mysql.exe :

SELECT @ @tx_isolation; set TRANSACTION ISOLATION LEVEL read Commited;set global transaction isolation level read commited;

2. set the isolation level in the application :
An explicit configuration can be configured in the Hibernate configuration file:
hibernate.connection.isolation=2
1:read uncommited; 2:read commited; 4:repeatable Read; 8:serializabel

Iv. using pessimistic locks in your application :
1, from the application point of view, the lock can be divided into 2 categories :
A, pessimistic Lock: Refers to the application in the explicit lock for data resources. can affect concurrency performance.
B, optimistic Lock: Optimistic lock assuming the current transaction operations data resource, no other transaction accesses the data resource at the same time, so it relies entirely on the isolation level of the database to automatically manage locks.
2, pessimistic lock realization Way :
A, the display in the application specifies that the data source is locked with an exclusive lock on the database system.
B. Determine whether a transaction can be accessed by adding a tag field to the database table.
3, using the database system of the exclusive lock to achieve pessimistic lock :
A, SQL statement control:

Select: for update;

B, hibernate when using the Get () or Load () method:
Account Account = (account) Session.get (account.class,new Long (1), lockmode.upgrade);
Lockmode no more in-depth introduction.

use Hibernate version control to achieve optimistic locking :
The basic implementation logic is to define a field in the database that represents the version, or a timestamp field, and then configure a <version> element in the mapping file, or a <timestamp> element.
For the time being do not do in-depth understanding.

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.