• Spring @Transactional配置知识梳理


    有如下属性:Propagation、Isolation、Rollback Rules、Timeout和Read-Only

    Propagation:事务传播属性

    Propagation.MANDATORY 当前方法必须在已经定义的Transaction中运行,如果没有已定义的Transaction则抛出异常。
    Propagation.NESTED 如果没有已定义的Transaction,当前方法新开一个Transaction并在该Transaction中运行。如果存在已定义的Transaction,当前方法在嵌套事务(Nested Transaction)中运行 — 嵌套事务中可以定义储存点,因此可以独立于外部的Transaction而进行rollback。
    Propagation.NEVER 当前方法不应在Transaction中运行,如果存在已经定义的Transaction则抛出异常。
    Propagation.NOT_SUPPORTED 当前方法不应在Transaction中运行,如果存在已经定义的Transaction,则该Transaction暂停(挂起)直至该方法运行完毕
    Propagation.REQUIRED默认值 当前方法必须在Transaction中运行。如果存在已经定义的Transaction,则该方法在已定义的Transaction中运行;如果不存在已经定义的Transaction,则该方法新开一个Transaction并在其中运行。
    Propagation.REQUIRES_NEW 前方法必须在新开的Transaction中运行。如果存在已经定义的Transaction,则该已定义的Transaction暂停直至新开的Transaction执行完毕。
    Propagation.SUPPORTS 当前方法不需要在Transaction中运行,但如果存在已经定义的Transaction,则该方法也可以在Transaction中正常执行。

     

    Isolation:隔离级别

    READ_UNCOMMITTED 允许事务读取另一个事务尚未提交的数据。 
    将会发生:dirty reads, non-repeatable reads and phantom reads
    READ_COMMITTED 不允许事务读取另一个事务尚未提交的数据。因此 dirty reads将不会发生,但是non-repeatable reads and phantom reads 仍可能发生
    REPEATABLE_READ

    不允许事务读取另一个事务尚未提交的数据。

    不允许一个事务重复读取一行数据时候获取不同的值。

    A constant indicating that dirty reads and non-repeatable reads are
         prevented; phantom reads can occur. This level prohibits a transaction
         from reading a row with uncommitted changes in it, and it also prohibits the situation where one transaction reads a row, a second transaction alters the row, and the first transaction rereads the row, getting different values the second time (a "non-repeatable read")

    SERIALIZABLE

    不允许事务读取另一个事务尚未提交的数据。

    不允许一个事务重复读取一行数据时候获取不同的值。

    不允许每次事务同一个查询时候获取不同的数据集合。

    A constant indicating that dirty reads, non-repeatable reads and phantom reads are prevented. This level includes the prohibitions in
    <code>ISOLATION_REPEATABLE_READ</code> and further prohibits the situation where one transaction reads all rows that satisfy a <code>WHERE</code> condition, a second transaction inserts a row that satisfies that <code>WHERE</code> condition, and the first transaction rereads for the same condition, retrieving the additional "phantom" row in the second read.

    DEFAULT 使用默认database的事务隔离级别。
    Use the default isolation level of the underlying datastore.
    All other levels correspond to the JDBC isolation levels.

    timeout:事务超时时间

    readOnly:

      事务只读属性,默认为false

    This just serves as a hint for the actual transaction subsystem; it will  not necessarily  cause failure of write access attempts.
    A transaction manager which cannot interpret the read-only hint will not throw an exception when asked for a read-only transaction.

    Rollback Rules:

    noRollbackForClassName

    noRollbackFor

    rollbackForClassName

    rollbackFor

     

    二:事务使用

    1) 使用annotation 配置:

    <bean id="transactionManager"
              class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
            <property name="dataSource" ref="dataSource"/>
        </bean>
    
    <!-- Use annotation to define transaction -->
      <tx:annotation-driven transaction-manager="transactionManager"
                            proxy-target-class="true"/>

    2)使用transactionTemplate

    <bean id="transactionTemplate" class="org.springframework.transaction.support.TransactionTemplate">
         <property name="transactionManager" ref="transactionManager"></property>
     </bean>
    
    transactionTemplate.execute(new TransactionCallback<T>()
    {
                @Override
                public T  doInTransaction(TransactionStatus status) {
    
                  
                }
            });

  • 相关阅读:
    Mybatis入门
    Ajax
    产品经理之产品规划
    产品经理之用户研究(下)
    产品经理之用户研究(上)
    Spring Cloud
    Spring MVC
    synchronized
    Spring Boot入门
    Spring
  • 原文地址:https://www.cnblogs.com/lily-tiantian/p/4648713.html
Copyright © 2020-2023  润新知