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