Transaction and Concurrency Control

Source: Internet
Author: User

1. Define transactions
 
Physical transactions that can reference databases
Logical transactions related to a persistent context can be referenced.
You can reference the unit of work of an application as defined by the prototype.

2. Physical transactions

Hibernate uses the persistence of JDBC APIs. in Java, there are two well-defined mechanisms for processing transactions: JDBC and JTA.
Hibernate supports combining transaction mechanisms to allow applications to manage physical transactions.

Two main functions in org. hibernate. Engine. transaction. SPI. transactionfactory are as follows:
It allows hibernate to understand the transaction semantics of the environment. Is it JTA? Is the physical transaction currently active? And so on.
As an org. hibernate. Transaction instance factory. Used to allow applications to manage and check the transaction status,
Org. hibernate. transaction is the concept of hibernate logical transactions. In JPA, the javax. Persistence. entitytransaction interface is also similar.

Physical Transaction-JDBC
Using java. SQL. Connection. Commit () and Java. SQL. Connection. rollback () for JDBC transaction management (),
JDBC does not define an explicit method to start a transaction.
In hibernate, this method is represented by the org. hibernate. Engine. transaction. Internal. JDBC. jdbctransactionfactory class.

Physical Transaction-JTA
The JTA-based transaction method is to obtain the javax. transaction. usertransaction interface from the org. hibernate. Service. JTA. Platform. SPI. jtaplatform API.
Represented by the org. hibernate. Engine. transaction. Internal. JTA. jtatransactionfactory class

Physical Transaction-CMT
Another JTA-based transaction method is to obtain the javax. transaction. transactionmanager interface from the org. hibernate. Service. JTA. Platform. SPI. jtaplatform API.
Represented by the org. hibernate. Engine. transaction. Internal. JTA. cmttransactionfactory class
In the real Jee CMT environment, access to javax. transaction. usertransaction is restricted.

Physical Transaction-custom
You can implement org. hibernate. Engine. transaction. SPI. transactionfactory to insert custom transaction methods.
By default, services start with built-in support for custom transactions with names specified by hibernate. transaction. factory_class:
Use the org. hibernate. Engine. transaction. SPI. transactionfactory instance
Use the name of the org. hibernate. Engine. transaction. SPI. transactionfactory class to implement the class and provide a no-argument constructor.

Physical Transaction-Legacy
During Development 4.0, most of the class names were moved to the new package to facilitate upgrade. hibernate also left a name for a while.
Org. hibernate. transaction. jdbctransactionfactory org. hibernate. Engine. transaction. Internal. JDBC. jdbctransactionfactory
Org. hibernate. transaction. jtatransactionfactory org. hibernate. Engine. transaction. Internal. JTA. jtatransactionfactory
Org. hibernate. transaction. cmttransactionfactory org. hibernate. Engine. transaction. Internal. JTA. cmttransactionfactory

3. Use of hibernate transactions
Hibernate uses direct JDBC connection and JTA resources without adding any additional locking behavior.
Familiar with JDBC, ansi SQL, and transaction isolation. Your database management system is very important.
Hibernate will not lock objects in the memory. When you use hibernate, the behaviors defined at the transaction isolation level of your database will not change.
Org. hibernate. Session is used as a transaction scope cache to provide a result set that can be re-read and loaded by the identifier.

In order to reduce the usage of locks in the database, physical database transactions must be as short as possible. Long database transactions prevent your applications from being extended to high-concurrency loads,
Transactions that do not hold a database are opened at the end user level, but are opened after end user level work is completed. It is called the write-behind of the transaction.

4. transaction mode (reverse mode)
Session-per-Operation Anti-Pattern
In a single thread, the anti-pattern of opening and closing a session for each database call is also an anti-Pattern Database Transaction.
Group your database calls to a scheduler sequence. Similarly, every SQL statement in the application is not automatically submitted.
Disable hibernate or disable the automatic submission mode on the application server.
Database transactions are never optional. All Database communications must be encapsulated by one transaction.
Avoid automatic commit when reading data, because many small transactions are unlikely to perform better than a well-defined unit of work and are difficult to maintain and expand.

Transaction and Concurrency Control

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.