2.2.1事务

事务就是以可控的方式对数据资源进行访问的一组操作。

事务本身持有4个限定属性(简称ACID属性),来保证事务前后,系统始终处于“正确”的状态:

  1. 原子性(Atomicity)

  2. 一致性(Consistency)

  3. 隔离性(Isolation)

    隔离级别:

    • Read Uncommitted(对应Spring的ISOLATION_READ_INCOMMITTED)

    • Read Committed(对应Spring的ISOLATION_READ_COMMITTED)

    • Repeatable Read(对应Spring的ISOLATION_REPEATABLE_READ)

    • Serializable(对应Spring的ISOLATION_SERIALIZABLE)

    • Spring还有一个ISOLATION_DEFAULT,意思是使用数据库定义的事务隔离级别

      相关问题:

    • 脏读(dirty Read)

    • 不可重复读(Non-Repeatable Read)

    • 幻读(Phantom Read)

  4. 持久性(Durability)

事务还有其传播行为

Spring在TransactionDefinition定义了如下种传播行为:

  • PROPAGATION_REQUIRED

  • PROPAGATION_SUPPORTS

  • PROPAGATION_MANDATORY

  • PROPAGATION_REQUIRES_NEW

  • PROPAGATION_NOT_SUPPORTED

  • PROPAGATION_NEVER

  • PROPAGATION_NESTED

  • ISOLATION_DEFAULT

TransactionDefinition API Doc中有如下表述:

Note that isolation level and timeout settings will not get applied unless an actual new transaction gets started. As only {@link #PROPAGATION_REQUIRED},{@link #PROPAGATION_REQUIRES_NEW} and {@link #PROPAGATION_NESTED} can cause that, it usually doesn't make sense to specify those settings in other cases. Furthermore, be aware that not all transaction managers will support those advanced features and thus might throw corresponding exceptions when given non-default values.

除PROPAGATION_REQUIRED,PROPAGATION_REQUIRES_NEW, PROPAGATION_NESTED之外的传播行为都会使隔离级别和timeout设定无效化。 而且,以上特性不是所有事务管理器都支持。当某些事务管理器不支持时,将会抛出相应异常。

results matching ""

    No results matching ""