Spring Transaction Management

Source: Internet
Author: User

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

    1. Dirty Reads Dirty Reading
    2. Default (Defaults)
    3. Read_uncommitted (read not submitted)
    4. Read_committed (read submitted)
    5. Repeatable_read (repeatable reading)
    6. 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

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.