Statement: The material used in this column is written by the VIP students of the Kay Academy, who has the right to be anonymous and has the final right to interpret the article; The Edith Academy is designed to promote students to learn from each other on the basis of public notes.
Spring uses AOP to manage transactions
Configuration steps for a transaction
1, first create a MAVEN spring project, add to the spring and other dependent packages
2. Add the transaction management tag (AOP data source, database driver, and ensure it is configured) in the XML file.
3. Support and package search for annotations written in XML
4. Configure the transaction manager and set the DataSource property
5. Use Tx:annotation-driven to support annotation transaction mode and then associate the transaction administrator
6. Add transaction annotations to the service class @transactional
7. See if an agent is generated
@service Business Layer Annotations
@repository Data Access Layer annotations
@Transactional Transaction Proxy pattern annotations
Experimental cases
We do an ordinary project of adding students and days, through the three layer model, we write @transation transaction annotations on the service layer
With this annotation, the action class we get is a proxy class, which, without this annotation, would be a normal class.
Agent class after transaction management If an exception occurs, we will not be able to insert the data successfully, stating that after the transaction agent mode, we will not need to consider the manual transaction operation, and the commit and rollback are given to spring to manage.
Advanced part of a transaction
Propagation transaction Mode, transaction propagation type.
There are seven types of transaction patterns (we can set them in parentheses in the transaction annotations)
REQUIRED: Supports the current transaction and creates a new transaction if there is no current transaction. This is the most common choice.
SUPPORTS: Supports the current transaction and is executed in a non-transactional manner if no transaction is currently in use.
MANDATORY: Supports the current transaction and throws an exception if there is no current transaction.
Requires_new: Creates a new transaction and suspends the current transaction if a transaction is currently present.
Not_supported: Executes the operation in a non-transactional manner, suspending the current transaction if a transaction is currently present.
Never: Executes in a non-transactional manner and throws an exception if a transaction is currently present.
NESTED: Supports the current transaction, executes a nested transaction if the current transaction exists, and creates a new transaction if there is no current transaction.
Transaction ISOLATION LEVEL
- Dirty Reads Dirty Reading
- Default (Defaults)
- Read_uncommitted (read not submitted)
- Read_committed (read submitted)
- Repeatable_read (repeatable reading)
- SERIALIZABLE (serialization)
Settings for read-only mode:
Readonly=true
Can only read, cannot write
Readonly=false
Can read, can also write
Exception rollback selection:
Rollbackfor={exception}
I'm just going to roll back.
Norollbackfor={exception}
I'm not rolling back.
Generally in the transaction agent mode, we just set what exception I do not rollback on it, because any exception, will automatically give us rollback. Only exceptions that are set to not roll back are valid.
Spring Transaction Management