数据库事务的坑:@Transactional注解的隐藏陷阱
一、问题现场还原那是一个月黑风高的夜晚小王正准备下班突然运营群里炸了【运营】重大bug用户下单成功了但没扣库存 【运营】已有多名用户反馈... 【运维】涉及金额已达12,580...小王赶紧打开代码Service public class OrderService { Autowired private OrderMapper orderMapper; Autowired private InventoryService inventoryService; Transactional(rollbackFor Exception.class) public void createOrder(OrderDTO order) { // 1. 创建订单 Order orderEntity new Order(); orderEntity.setUserId(order.getUserId()); orderEntity.setAmount(order.getAmount()); orderMapper.insert(orderEntity); // 插入成功 // 2. 扣减库存 inventoryService.decreaseStock(order.getSkuId(), order.getQuantity()); // ❌ 如果这里抛异常订单已经插入了但库存没扣 // 3. 发送消息 messageService.sendOrderCreatedMessage(orderEntity.getId()); } } Service public class InventoryService { public void decreaseStock(String skuId, Integer quantity) { // 扣减库存逻辑 // ... if (库存不足) { throw new RuntimeException(库存不足); } } }问题分析虽然createOrder加了Transactional但inventoryService.decreaseStock()是this调用自调用不走Spring的代理所以事务根本没有生效二、原因剖析Spring事务的代理机制2.1 Spring事务基于AOP代理┌─────────────────────────────────────────────────────────┐ │ Spring IOC容器 │ ├─────────────────────────────────────────────────────────┤ │ │ │ ┌──────────────────┐ ┌──────────────────┐ │ │ │ OrderService │ │ InventoryService│ │ │ │ ┌────────────┐ │ │ │ │ │ │ │ createOrder │ │ │ │ │ │ │ │ Transactional│ │ │ │ │ │ └─────┬──────┘ │ │ │ │ │ │ │ │ │ │ │ │ │ ▼ │ │ │ │ │ │ ┌────────────┐ │ │ │ │ │ │ │ 事务代理 │ │ │ │ │ │ │ │ (AOP) │ │ │ │ │ │ │ └────────────┘ │ │ │ │ │ └────────┬─────────┘ └──────────────────┘ │ │ │ │ │ ▼ │ │ ┌──────────────────┐ │ │ │ decreaseStock() │ ← this调用不经过代理 │ │ └──────────────────┘ │ │ │ └─────────────────────────────────────────────────────────┘2.2 自调用失效的原因当我们在同一个类中调用另一个方法时public void methodA() { this.methodB(); // this.methodB() 不会走代理 }Spring的事务是通过AOP代理实现的只有外部调用才会经过代理内部调用自调用会直接跳过代理。2.3 Transactional失效的场景汇总场景示例是否生效自调用this.method()❌ 不生效private方法Transactional private method()❌ 不生效异常被catchtry { } catch { }❌ 不生效非RuntimeExceptionthrow new Exception()❌ 不生效默认只回滚RuntimeException多数据源未指定两个DataSource⚠️ 需要指定transactionManager三、解决方案让事务”生效”方案一注入自身推荐Service public class OrderService { Autowired private OrderService self; // 注入自身 Transactional(rollbackFor Exception.class) public void createOrder(OrderDTO order) { // 1. 创建订单 orderMapper.insert(orderEntity); // 2. 扣减库存 - 通过代理调用 self.decreaseStockInTransaction(order.getSkuId(), order.getQuantity()); // 3. 发送消息 messageService.sendOrderCreatedMessage(orderEntity.getId()); } Transactional(rollbackFor Exception.class) public void decreaseStockInTransaction(String skuId, Integer quantity) { // 扣减库存逻辑 inventoryMapper.decreaseStock(skuId, quantity); } }方案二使用TransactionTemplateService public class OrderService { Autowired private TransactionTemplate transactionTemplate; public void createOrder(OrderDTO order) { transactionTemplate.executeWithoutResult(status - { // 1. 创建订单 orderMapper.insert(orderEntity); // 2. 扣减库存 inventoryService.decreaseStock(skuId, quantity); // 3. 发送消息 messageService.sendOrderCreatedMessage(orderEntity.getId()); }); } }方案三使用AopContext.currentProxy()SpringBootApplication(exposeProxy true) // 需要开启暴露代理 EnableAspectJAutoProxy(exposeProxy true) public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } } Service public class OrderService { public void createOrder(OrderDTO order) { ((OrderService) AopContext.currentProxy()) .decreaseStockInTransaction(skuId, quantity); } Transactional(rollbackFor Exception.class) public void decreaseStockInTransaction(String skuId, Integer quantity) { // ... } }方案四确保异常能被正确感知Transactional(rollbackFor Exception.class) // 明确指定回滚条件 public void createOrder(OrderDTO order) { try { inventoryService.decreaseStock(skuId, quantity); } catch (Exception e) { log.error(扣减库存失败, e); // 不要吞掉异常否则事务不会回滚 throw e; // 重新抛出或者不catch } }四、事务传播行为详解4.1 七种传播行为public enum Propagation { REQUIRED, // 如果当前有事务加入该事务默认 REQUIRES_NEW, // 开启新事务挂起当前事务 SUPPORTS, // 如果有事务加入事务没有则以非事务执行 NOT_SUPPORTED, // 以非事务执行挂起当前事务 MANDATORY, // 必须在事务中执行否则抛异常 NEVER, // 必须在非事务中执行否则抛异常 NESTED // 嵌套事务 Savepoint }4.2 常见场景选择Service public class UserService { Autowired private AccountService accountService; Transactional public void registerUser(User user) { // 1. 创建用户 - 使用当前事务 userMapper.insert(user); // 2. 初始化账户 - 单独事务失败不影响用户创建 accountService.initAccountWithNewTransaction(user.getId()); // 3. 发送欢迎邮件 - 非事务失败不影响主流程 emailService.sendWelcomeEmail(user.getEmail()); // NOT_SUPPORTED } } Service public class AccountService { Transactional(propagation Propagation.REQUIRES_NEW) public void initAccountWithNewTransaction(Long userId) { // 这个方法会开启新事务 // 即使这里失败UserService的事务也不会回滚 accountMapper.initAccount(userId); } } Service public class EmailService { Transactional(propagation Propagation.NOT_SUPPORTED) public void sendWelcomeEmail(String email) { // 以非事务执行失败不影响主流程 } }五、排查工具事务Debug5.1 开启事务日志# application.yml logging: level: org.springframework.orm.jpa: DEBUG org.springframework.transaction: DEBUG5.2 使用Transactional注解剖析Configuration public class TransactionAspectConfig { Bean public BeanFactoryTransactionAnnotationParser transactionAnnotationParser() { return new BeanFactoryTransactionAnnotationParser(); } public boolean isTransactional(Method method) { Transactional tx method.getAnnotation(Transactional.class); return tx ! null; } }5.3 事务超时配置Transactional(timeout 30) // 30秒超时 public void createOrder(OrderDTO order) { // 如果超过30秒自动回滚 }六、预防措施最佳实践清单6.1 事务使用检查表✅ Transactional使用检查 ├── 1. 是否在public方法上private方法不生效 ├── 2. 是否是外部调用自调用需要通过代理 ├── 3. 异常是否被catch吞掉 ├── 4. 是否指定了rollbackFor默认只回滚RuntimeException ├── 5. 是否有多个数据源是否指定了transactionManager ├── 6. 是否需要配置事务超时 └── 7. 传播行为是否正确6.2 编码规范建议// 建议1不要在事务方法中进行远程调用 Transactional public void createOrder(OrderDTO order) { // ❌ 不好远程调用在事务中事务时间过长 remoteService.call(); // ✅ 好先完成本地事务再异步调用远程 } // 建议2大事务拆分 Transactional public void createOrder(OrderDTO order) { // 保持事务简短 orderMapper.insert(order); } // 异步执行其他操作 Async public void afterOrderCreated(OrderDTO order) { messageService.sendMessage(order); inventoryService.decreaseStock(order.getSkuId(), order.getQuantity()); }七、总结今天我们学到了要点说明问题本质Spring事务基于代理自调用不走代理失效场景private方法、自调用、异常被catch、非RuntimeException解决方案注入自身、TransactionTemplate、AopContext.currentProxy()传播行为REQUIRED默认、REQUIRES_NEW开启新事务最佳实践保持事务简短、避免远程调用在事务中彩蛋小王最后用了”注入自身”的方案修复了bug。他在周会上分享经验时说道“Spring的事务就像高考监考——只有从外部监考老师看过去才是有效的。你自己看着自己考试那不就作弊了吗”
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/2503661.html
如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!