Spring transaction propagation behavior and transaction isolation level

Spring's annotated transactions use:

The <tx:> namespace configuration is introduced into the spring configuration file

<beans xmlns="http://www.springframework.org/schema/beans";
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance";
    xmlns:tx="http://www.springframework.org/schema/tx";
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
    http://www.springframework.org/schema/tx
    http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">;

②spring declarative transaction configuration

<!-- 事务管理器配置 -->
<bean id="defaultTransactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactory" />
</bean>

<!-- 使用annotation定义事务 -->
<tx:annotation-driven transaction-manager="defaultTransactionManager" proxy-target-class="true" />

③ Write @Transactional at the declaration of the interface or class

If it is written in an interface, the implementation class of the interface will inherit the transaction, but it will only take effect when you set up an interface-based proxy, so it is recommended to write it in a specific class when using transactions .

When marked in front of a class, all public methods in the marked class are subject to transaction management, and methods that are not public are marked with annotations but have no transaction function.

 

Thing spread behavior settings:

@Transactional(propagation=Propagation.REQUIRED) (default)

If there is a transaction, join the transaction, if not, create a new one.

@Transactional(propagation=Propagation.NOT_SUPPORTED)

This method does not enable transaction management

@Transactional(propagation=Propagation.REQUIRES_NEW)

Regardless of whether there is a transaction, a new transaction is created, the original is suspended, waiting for the new transaction to complete, and then the old transaction is executed.

@Transactional(propagation=Propagation.MANDATORY) 

Must be executed in an existing transaction, otherwise an exception is thrown.

@Transactional(propagation=Propagation.NEVER) 

Must be executed in a transaction that does not have, otherwise an exception is thrown.

@Transactional(propagation=Propagation.SUPPORTS) 

If other BEANs call this method and declare transactions in other BEANs, use transactions.

If the other bean does not declare a transaction, then no transaction is required.

 

Transaction timeout settings:

@Transactional(timeout=30)

The timeout is 30 seconds

 

Transaction isolation level settings:

@Transactional(isolation = Isolation.READ_UNCOMMITTED)

Read uncommitted data (dirty reads, non-repeatable reads will occur)

@Transactional(isolation = Isolation.READ_COMMITTED) (SQLSERVER defaults to this setting)

Read committed data (non-repeatable reads and phantom reads will occur)

@Transactional(isolation = Isolation.REPEATABLE_READ) (MYSQL defaults to this setting)

Repeatable reading (phantom reading will occur)

@Transactional(isolation = Isolation.SERIALIZABLE)

serialize

 

Glossary:

Dirty read  : One transaction reads uncommitted updated data in another transaction.

Non-repeatable read  : A transaction can read committed updates in another transaction.

Repeatability : Updates committed in one transaction cannot be read in another transaction.

Phantom read  :  In one transaction, you can read the new (insert) data that has been committed in another transaction.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325235528&siteId=291194637