Spring Transaction Configuration

Source: Internet
Author: User

1, first to configure the Txmanager

The following datasource, which is a data connection pool, configures the corresponding connection pool according to the project needs

<id= "Txmanager"  class= " Org.springframework.jdbc.datasource.DataSourceTransactionManager "><   Name= "DataSource"  ref= "DataSource"/></  Bean>

2, using @transactional annotation way to use the transaction

<transaction-manager= "Txmanager"/>

How to use (Scan bean configuration to turn on):

When marked in front of a class, all methods in the marked class are handled by the object

Example:

@Transactional  Public class Implements Personservice {}

When certain methods in a class do not require things:

@Transactional  Public class Implements testservice {        = propagation.not_supported)    public list< Object> GetAll () {        returnnull;    }    }

3. Using XML-based configuration transactions

<BeanID= "Txmanager"class= "Org.springframework.jdbc.datasource.DataSourceTransactionManager">< Propertyname= "DataSource"ref= "DataSource"/></Bean><Aop:config><Aop:pointcutID= "Transactionpointcut"expression= "Execution (* cn.itcast.service). *.*(..))"/><Aop:advisorAdvice-ref= "Txadvice"Pointcut-ref= "Transactionpointcut"/></Aop:config><Tx:adviceID= "Txadvice"Transaction-manager= "Txmanager"><tx:attributes><Tx:methodname= "get*"read-only= "true"Propagation= "not_supported"/><Tx:methodname="*"/></tx:attributes></Tx:advice>

4. Indicator

  * for any wildcard character

expression= "Execution (* cn.itcast.service). *.*(..))” Represents all methods that act on all classes under Cn.itcast.service packages and sub-packages

expression= "Execution (* cn.itcast.service). * * (java.lang.String,..)) " A method that represents the first parameter of a class that acts under the Cn.itcast.service package and the sub-package is of type string

expression= "Execution (java.lang.String cn.itcast.service. *.*(..))” Represents a method that acts on the return value type string of all classes under the Cn.itcast.service package and the child package

expression= "Execution (!java.lang.string cn.itcast.service. *.*(..))” The return value type of the class that represents the function under the Cn.itcast.service package and the child package is not all methods of string

expression= "Execution (* set** (..))" The expression acts on any method that starts with a set

within-to match a specific type of connection point

Expression= "Winthin (cn.itcast.*)" represents all connection points that are acting under the Cn.itcast package and the sub-package

5. Introduction of the act of spreading things:

  @Transactional (propagation=propagation.required)
If there is a transaction, then join the transaction and create a new one (by default)
@Transactional (propagation=propagation.not_supported)
Container does not open transactions for this method
@Transactional (propagation=propagation.requires_new)
Creates a new transaction regardless of whether a transaction exists, the original hangs, the new execution completes, and the old transaction continues
@Transactional (Propagation=propagation.mandatory)
Must be executed in an existing transaction, or throw an exception
@Transactional (Propagation=propagation.never)
Must be executed in a non-transaction, otherwise throws an exception (as opposed to propagation.mandatory)
@Transactional (Propagation=propagation.supports)
If the other bean calls this method and declares the transaction in another bean, the transaction is used. If the other bean does not declare the transaction, then there is no transaction.

Things timeout settings:
@Transactional (timeout=30)//default is 30 seconds

Transaction ISOLATION Level:
@Transactional (isolation = isolation.read_uncommitted)
READ UNCOMMITTED data (dirty read, non-repeatable read) basic not used
@Transactional (isolation = isolation.read_committed)
Read committed data (non-repeatable read and Phantom reads occur)
@Transactional (isolation = isolation.repeatable_read)
REPEATABLE READ (phantom read occurs)
@Transactional (isolation = isolation.serializable)

Serialization

MYSQL: Default to Repeatable_read level
SQL Server: Default is read_committed

Dirty reads : One transaction reads uncommitted update data to another transaction

non-repeatable reads: In the same transaction, multiple reads of the same data are returned with different results, in other words, subsequent reads can be read to the updated data submitted by another transaction. Conversely, "repeatable read" can guarantee the same read data when read data multiple times in the same transaction, that is, subsequent reads cannot be read to the updated data submitted by another transaction

Phantom reads : One transaction reads the insert data that has been committed by another transaction

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.