Transaction Management
1.4. Declarative transaction management
1.4.1. Understanding the Spring Framework’s Declarative Transaction Implementation
Spring的事务管理机制是基于AOP的,看过之前的CORE部分文档可以知道AOP的一些知识,例如Advice、Advisor。如果需要被事务处理的类有接口,那么使用的就是JDK代理;如果没有接口,那么使用的就是CGLIB代理等等。
1.4.2. Example of Declarative Transaction Implementation
示例代码chapter1_4.Demo#testXML
1.4.6. Using @Transactional
示例代码chapter1_4.Demo#testAnno
1.4.7. Transaction Propagation
Understanding PROPAGATION_REQUIRED
示例代码chapter1_4.Demo#testPropagationRequire
简单说就是所有逻辑事务都在同一个物理事务之中,只要一个回滚,那就都回滚;内部事务的配置从外部事务进行继承。
注意第一段try catch的结果有两个异常
第一个是我定义的自定义运行期异常,在chapter1_4.Demo#testPropagationRequire
中被外部事务捕获
第二个是UnexpectedRollbackException
,在chapter1_4.Demo#testPropagationRequire
的第一个catch处被捕获
这是因为外部事务里对内部事务抛出的异常进行了捕获,导致外部事务无法知道内部事务是否发生了异常,从而不知道是否该提交。所以spring框架帮我们抛出了这个异常,告诉事务管理器这个事务必须回滚
Understanding PROPAGATION_REQUIRED_new
示例代码位于chapter1_4.Demo#testPropagationRequiresNew
,内外部事务独立;
Understanding PROPAGATION_NESTED
示例代码位于chapter1_4.Demo#testPropagationNested
,内部事务属于外部事务的子事务,外部会影响内部,内部不会影响外部。
1.4.8. Advising Transactional Operations
这个就是说如何在事务配置之后再配置其他切面,在AOP那一章节里提到过