Spring官网对 spring事务的描述

Spring官网对 spring事务的描述

两点

1.事务传播行为
2.事务隔离级别

事务传播行为

可以根据该文章理解与对比:https://blog.csdn.net/xuan_lu/article/details/106006755
对上述文章中,笔者在验证第七种事务传播行为(PROPAGATION_NESTED)时的疑惑,是因为spring事务失效,其效果与PROPAGATION_REQUIRED传播行为一致;spring中@Transactional是通过动态代理实现的,可参照下面文章的解释https://blog.csdn.net/qq_16268979/article/details/123707823
spring官网对七种事务传播行为的描述:

  1. PROPAGATION_REQUIRED Support a current transaction; create a new one if none exists. Analogous to the EJB transaction attribute of the same name.This is typically the default setting of a transaction definition, and typically defines a transaction synchronization scope.

  2. PROPAGATION_SUPPORTS Support a current transaction; execute non-transactionally if none exists. Analogous to the EJB transaction attribute of the same name.
    NOTE: For transaction managers with transaction synchronization, PROPAGATION_SUPPORTS is slightly different from no transaction at all, as it defines a transaction scope that synchronization might apply to. As a consequence, the same resources (a JDBC Connection, a Hibernate Session, etc) will be shared for the entire specified scope. Note that the exact behavior depends on the actual synchronization configuration of the transaction manager!
    In general, use PROPAGATION_SUPPORTS with care! In particular, do not rely on PROPAGATION_REQUIRED or PROPAGATION_REQUIRES_NEW within a PROPAGATION_SUPPORTS scope (which may lead to synchronization conflicts at runtime). If such nesting is unavoidable, make sure to configure your transaction manager appropriately (typically switching to “synchronization on actual transaction”).

  3. PROPAGATION_MANDATORY Support a current transaction; throw an exception if no current transaction exists. Analogous to the EJB transaction attribute of the same name.
    Note that transaction synchronization within a PROPAGATION_MANDATORY scope will always be driven by the surrounding transaction.

  4. PROPAGATION_REQUIRES_NEW Create a new transaction, suspending the current transaction if one exists. Analogous to the EJB transaction attribute of the same name.
    NOTE: Actual transaction suspension will not work out-of-the-box on all transaction managers. This in particular applies to org.springframework.transaction.jta.JtaTransactionManager, which requires the javax.transaction.TransactionManager to be made available it to it (which is server-specific in standard Java EE).
    A PROPAGATION_REQUIRES_NEW scope always defines its own transaction synchronizations. Existing synchronizations will be suspended and resumed appropriately.

  5. PROPAGATION_NOT_SUPPORTED Do not support a current transaction; rather always execute non-transactionally. Analogous to the EJB transaction attribute of the same name.
    NOTE: Actual transaction suspension will not work out-of-the-box on all transaction managers. This in particular applies to org.springframework.transaction.jta.JtaTransactionManager, which requires the javax.transaction.TransactionManager to be made available it to it (which is server-specific in standard Java EE).
    Note that transaction synchronization is not available within a PROPAGATION_NOT_SUPPORTED scope. Existing synchronizations will be suspended and resumed appropriately.

  6. PROPAGATION_NEVER Do not support a current transaction; throw an exception if a current transaction exists. Analogous to the EJB transaction attribute of the same name.
    Note that transaction synchronization is not available within a PROPAGATION_NEVER scope.

  7. PROPAGATION_NESTED Execute within a nested transaction if a current transaction exists, behave like PROPAGATION_REQUIRED otherwise. There is no analogous feature in EJB.
    NOTE: Actual creation of a nested transaction will only work on specific transaction managers. Out of the box, this only applies to the JDBC org.springframework.jdbc.datasource.DataSourceTransactionManager when working on a JDBC 3.0 driver. Some JTA providers might support nested transactions as well.

事务隔离级别

可以根据该文章理解与对比:https://blog.csdn.net/lamwolf/article/details/128528351
注:不可重复读:是发生在并发修改;幻读:是发生在并发删除或插入;
spring官网对五种事务隔离级别的描述:

  1. ISOLATION_DEFAULT Use the default isolation level of the underlying datastore. All other levels correspond to the JDBC isolation levels.

  2. ISOLATION_READ_UNCOMMITTED Indicates that dirty reads, non-repeatable reads and phantom reads can occur.
    This level allows a row changed by one transaction to be read by another transaction before any changes in that row have been committed (a “dirty read”). If any of the changes are rolled back, the second transaction will have retrieved an invalid row.

  3. ISOLATION_READ_COMMITTED Indicates that dirty reads are prevented; non-repeatable reads and phantom reads can occur.
    This level only prohibits a transaction from reading a row with uncommitted changes in it.

  4. ISOLATION_REPEATABLE_READ Indicates 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 re-reads the row, getting different values the second time (a “non-repeatable read”).

  5. ISOLATION_SERIALIZABLE Indicates that dirty reads, non-repeatable reads and phantom reads are prevented.
    This level includes the prohibitions in ISOLATION_REPEATABLE_READ and further prohibits the situation where one transaction reads all rows that satisfy a WHERE condition, a second transaction inserts a row that satisfies that WHERE condition, and the first transaction re-reads for the same condition, retrieving the additional “phantom” row in the second read.

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值