- Spring boot中,无需单独设置@EnableTransactionManagement,已自动配置
- Spring仅会为标识了@Transactional注解的方法或类进行事务设置,过程为
- org.springframework.aop.framework.autoproxy.AbstractAutoProxyCreator#wrapIfNecessary中,对所有bean进行match(org.springframework.transaction.interceptor.TransactionAttributeSourcePointcut),规则为是否是带注解@Transactional
- 匹配成功,则生成org.springframework.transaction.interceptor.BeanFactoryTransactionAttributeSourceAdvisor,并开始为该bean生成新的代理(为方法或类中方法添加advisor,进而应用pointcut,将method的Advice设置为TransactionInterceptor)
- 说明:这些方法会应用如下块进行包裹:
try { connection.begin() // 执行被包裹对象 } catch (Exception e) { connection.rollback() } finally { connection.commit() }
- 对于select ... forupdate,需要在事务块中进行。所以,如果需要应用该行级锁,其所在方法块应该标识注解 @Transactional,否则不会有作用
Spring: Transactional记录
最新推荐文章于 2024-04-29 17:13:56 发布