Spring Transaction Management

Source: Internet
Author: User

Spring provides several classes for transaction processing:

Transactiondefinition//Transaction Property Definition
Transcationstatus//Represents the current transaction, which can be committed and rolled back.
Platformtransactionmanager This is the basic interface that spring provides for managing transactions, under which there is an implementation of the abstract class Abstractplatformtransactionmanager, which we use Transaction Management classes such as Datasourcetransactionmanager are subclasses of this class.

General Transaction definition steps:

Transactiondefinition td = New Transactiondefinition ();
Transactionstatus ts = transactionmanager.gettransaction (TD);
Try
{//do STH
Transactionmanager.commit (TS);
}
catch (Exception e) {transactionmanager.rollback (TS);}

The transaction management that spring provides can be divided into two categories: programmatic and declarative.

Programming, more flexible, but the code is large, there are more duplicate code;

Declarative is more flexible than programmatic.

programming mainly uses transactiontemplate. Omit a partial commit, rollback, a series of transaction object definitions, to be injected into the transaction management object .
void Add ()
{
Transactiontemplate.execute (New Transactioncallback () {
Pulic Object dointransaction (transactionstatus ts)
{//do STH}
}
}

Declarative :

Using Transactionproxyfactorybean:

propagation_required propagation_required propagation_required,readonly

dynamic agents around proxies are able to automatically commit and rollback transactions
Org.springframework.transaction.interceptor.TransactionProxyFactoryBean

propagation_required– supports the current transaction and creates a new transaction if there is no current transaction. This is the most common choice.

propagation_supports– supports the current transaction and is executed in a non-transactional manner if no transaction is currently in use.

The propagation_mandatory– supports the current transaction and throws an exception if there is no current transaction.

propagation_requires_new– a new transaction, suspending the current transaction if a transaction is currently present.

The propagation_not_supported– executes the operation in a non-transactional manner, suspending the current transaction if a transaction is currently present.

The propagation_never– is executed in a non-transactional manner and throws an exception if a transaction is currently present.

propagation_nested– executes within a nested transaction if a transaction is currently present. If there is currently no transaction, do something similar to propagation_required.

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.