• @Transactional 的使用


    @Transactional 使用注意点

    • @Transactional 注解只能应用到 public 方法或者类上才有效

    • 首先开启 @EnableTransactionManagement,但是Spring Boot已经默认开启 @EnableTransactionManagement 。

    Spring Boot 如何默认开启@EnableTransactionManagement

    查看类 TransactionAutoConfiguration

    org.springframework.boot.autoconfigure.transaction.TransactionAutoConfiguration
    1. //
    2. // Source code recreated from a .class file by IntelliJ IDEA
    3. // (powered by FernFlower decompiler)
    4. //
    5. package org.springframework.boot.autoconfigure.transaction;
    6. import java.util.Collection;
    7. import java.util.stream.Collectors;
    8. import org.springframework.beans.factory.ObjectProvider;
    9. import org.springframework.boot.autoconfigure.AutoConfigureAfter;
    10. import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
    11. import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
    12. import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
    13. import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
    14. import org.springframework.boot.autoconfigure.condition.ConditionalOnSingleCandidate;
    15. import org.springframework.boot.autoconfigure.data.neo4j.Neo4jDataAutoConfiguration;
    16. ...
    17. @Configuration
    18. @ConditionalOnClass({PlatformTransactionManager.class})
    19. @AutoConfigureAfter({JtaAutoConfiguration.class, HibernateJpaAutoConfiguration.class, DataSourceTransactionManagerAutoConfiguration.class, Neo4jDataAutoConfiguration.class})
    20. @EnableConfigurationProperties({TransactionProperties.class})
    21. public class TransactionAutoConfiguration {
    22. public TransactionAutoConfiguration() {
    23. }
    24. ...
    25. @Configuration
    26. @ConditionalOnBean({PlatformTransactionManager.class})
    27. @ConditionalOnMissingBean({AbstractTransactionManagementConfiguration.class})
    28. public static class EnableTransactionManagementConfiguration {
    29. public EnableTransactionManagementConfiguration() {
    30. }
    31. @Configuration
    32. @EnableTransactionManagement(
    33. proxyTargetClass = true
    34. )
    35. @ConditionalOnProperty(
    36. prefix = "spring.aop",
    37. name = {"proxy-target-class"},
    38. havingValue = "true",
    39. matchIfMissing = true
    40. )
    41. public static class CglibAutoProxyConfiguration {
    42. public CglibAutoProxyConfiguration() {
    43. }
    44. }
    45. @Configuration
    46. @EnableTransactionManagement(
    47. proxyTargetClass = false
    48. )
    49. @ConditionalOnProperty(
    50. prefix = "spring.aop",
    51. name = {"proxy-target-class"},
    52. havingValue = "false",
    53. matchIfMissing = false
    54. )
    55. public static class JdkDynamicAutoProxyConfiguration {
    56. public JdkDynamicAutoProxyConfiguration() {
    57. }
    58. }
    59. }
    60. ...
    61. }

    Spring Boot 引入了一个事务自动配置类 TransactionAutoConfiguration。该类已经默认添加EnableTransactionManagement

    @Transactional 的简单使用

    1) 只需在方法加上 @Transactional 注解

    1. @Override
    2. @Transactional
    3. public String testTransactional(boolean flag){
    4. testMapper.insert(Test);
    5. if (flag) {
    6. throw new RuntimeException("run time error");
    7. }
    8. return "成功";
    9. }

    flag = true。该事务没有提交 commit,因为遇到 RuntimeException 异常该事务进行了回滚,数据库中插入数据失败。

    2) 结合propagation

    1. @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    2. @Override
    3. public String save(ProductModuleConfig productModuleConfig){
    4. testMapper.insert(Test);
    5. try {
    6. String a = null;
    7. boolean equals = a.equals("2");
    8. } catch (Exception e) {
    9. e.printStackTrace();
    10. }
    11. return "成功";
    12. }
    • save 方法无 @Transactional 注解

            空指针异常没有被 try catch:插入数据库操作成功
            空指针异常被 try catch:插入数据库操作成功

    • save 方法有 @Transactional 注解

            空指针异常没有被 try catch:插入数据库操作失败,回滚成功
            空指针异常被 try catch:插入数据库操作成功,回滚失败

    【Java 基础】SpringBoot 中 @Transactional 注解的使用与实践_致最长的电影的博客-CSDN博客_springboot transactional注解

  • 相关阅读:
    卡特兰数
    四、看看 CSS 创建
    埃菲尔之月手表口红礼盒情迷法式浪漫
    SpringSecurity系列 - 11 前后端分离表单认证:自定义过滤器替换 UsernamePasswordAuthenticationFilter
    Python自动化测试详解
    单节点伪分布式Hadoop部署笔记
    【QT】QT自定义C++类
    Python报错ImportError: cannot import name ‘CREATE_NO_WINDOW‘
    博客系统(页面)
    MATLAB计算极限和微积分
  • 原文地址:https://blog.csdn.net/A_bad_horse/article/details/126903524