Spring事务当中propagation=“REQUIRED“和PROPAGATION=“REQUIRES_NEW“的区别

2023-11-02

3.propagation="REQUIRED"和PROPAGATION="REQUIRES_NEW"的区别  

(官方)PROPAGATION_REQUIRED--支持当前事务,如果当前没有事务,就新建一个事务。这是最常见的选择。 
PROPAGATION_REQUIRES_NEW--新建事务,如果当前存在事务,把当前事务挂起。

马克-to-win:当两个不同的pointcut之间涉及调用方法时,就涉及到事务传播属性。比如上面的方法updateRegister,如果它的属性是PROPAGATION_REQUIRED,则它会和调用它的方法login同属一个事务,如果是REQUIRES_NEW,则在不同的事务。当updateRegister已经提交之后updateRegisterWrong发生错误的时候,如果是REQUIRES_NEW,则updateRegister不会回滚,而如果是REQUIRED,则会回滚。注意下例,我们把两个方法故意放在两个pointcut里。看一下程序把REQUIRED变成REQUIRES_NEW以后的区别。

例 2.3.1

package service;
import java.sql.Types;
import javax.annotation.Resource;
import org.springframework.jdbc.core.JdbcTemplate;
import com.INiutDAO;
import service.interfac.ILoginService;

public class LoginServiceImpl implements ILoginService {
    @Resource
    private JdbcTemplate jt;
    private INiutDAO iNiutDAO;
    public void setINiutDAO(INiutDAO iNiutDAO) {
        this.iNiutDAO = iNiutDAO;
    }
    public void login() {
        iNiutDAO.daoUpdateRegister(13,1);
        System.out.println("successfully update 1");
        updateRegisterWrong(19,2);
        System.out.println("successfully update 2");
    }    
    public void updateRegisterWrong(int age,int id) {
        String sql = "UPDATE register SET age=? WWWWWWW id=?";
        Object params[] = new Object[] { new Integer(age),
                new Integer(id) };
        int type[] = new int[] { Types.INTEGER , Types.INTEGER };
        jt.update(sql, params, type);
    }
}



<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-3.0.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd"
       >
 
    <context:component-scan
        base-package="com" />
    <context:component-scan
        base-package="service" />
    <bean id="viewResolver"
        class="org.springframework.web.servlet.view.UrlBasedViewResolver">
        <property name="viewClass"
            value="org.springframework.web.servlet.view.JstlView" />
        <property name="prefix" value="/" />
        <property name="suffix" value=".jsp" />
    </bean>
 <bean id="NiutDAO" class="com.NiutDAO">
    </bean>
    <bean id="loginService" class="service.LoginServiceImpl" >
        <property name="iNiutDAO">
            <ref bean="NiutDAO" />
        </property>
    </bean>
   
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="login*" propagation="REQUIRED" />
            <tx:method name="daoUpdateRegister" propagation="REQUIRED" />        
        </tx:attributes>
    </tx:advice>
    <!-- 配置切面 这种写法也正确"execution(* service.*.*(..))"-->
    <aop:config>
        <aop:pointcut id="myPointcut" expression="execution(* service.LoginServiceImpl.*(..))" />
        <aop:pointcut id="myDaoPointcut" expression="execution(* com.NiutDAO.*(..))" />
        <aop:advisor advice-ref="txAdvice" pointcut-ref="myPointcut"/>
        <aop:advisor advice-ref="txAdvice" pointcut-ref="myDaoPointcut"/>        
    </aop:config> 
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" >
         <property name="driverClassName"
                      value="com.mysql.jdbc.Driver"></property>
         <property name="url" 
                      value="jdbc:mysql://localhost:3306/test"></property>
         <property name="username"
                      value="root"></property>
         <property name="password" 
                      value="1234"></property>
    </bean>
    <bean id="jt" class="org.springframework.jdbc.core.JdbcTemplate">
        <constructor-arg ref="dataSource" />
    </bean>
    <bean id="transactionManager"
        class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource" />
    </bean>
</beans>



package com;
import java.sql.Types;
import javax.annotation.Resource;
import org.springframework.jdbc.core.JdbcTemplate;
public class NiutDAO implements INiutDAO {
    @Resource
    private JdbcTemplate jt;
    public void daoUpdateRegister(int age,int id) {
        String sql = "UPDATE register SET age=? WHERE id=?";
        Object params[] = new Object[] { new Integer(age),
                 new Integer(id) };
        int type[] = new int[] { Types.INTEGER , Types.INTEGER };
        jt.update(sql, params, type);
    }
}


package com;
public interface INiutDAO {
    public void daoUpdateRegister(int age,int id);
}

更多请见下节:https://blog.csdn.net/qq_44591615/article/details/109206459

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Spring事务当中propagation=“REQUIRED“和PROPAGATION=“REQUIRES_NEW“的区别 的相关文章

随机推荐

  • QQGuild删除

    整理C盘用户数据时发现了这玩意 输入路径 C Users Dell AppData Local Tencent 筛选最小输出文件大小 20 QQBrowser 大小 246 52 M QQGuild 大小 306 37 M 明明把Tence
  • encoder-decoder

    Encoder decoder框架 Encoder decoder框架最抽象的一种表示 即由一个句子生成另一个句子的通过处理模型 对于句子对
  • vulhub打靶第二周

    第二周 信息收集 主机发现 arping 和arp scan 都可以使用 arping 在大多Linux 发行版都默认包含 但是arping无法扫一个网段 可以用shell脚本补足这个缺陷 for i in seq 1 254 do sud
  • nginx 握手失败SSL_do_handshake() failed (SSL: error:1408A0C1:SSL routines:SSL3_GET_CLIENT_HELLO:no share...

    SSL do handshake failed SSL error 1408A0C1 SSL routines SSL3 GET CLIENT HELLO no shared cipher 因为nginx不支持客户端的算法套件 1 可更新n
  • Python 数据采集-爬取学校官网新闻标题与链接(进阶)

    Python 爬虫爬取学校官网新闻标题与链接 进阶 前言 一 拼接路径 二 存储 三 读取翻页数据 四 完整代码展示 五 小结 前言 本文基于学校的课程内容进行总结 所爬取的数据均为学习使用 请勿用于其他用途 准备工作 爬取地址 https
  • mysql bitmap redis_Redis 中 BitMap 的使用场景

    BitMap 原本的含义是用一个比特位来映射某个元素的状态 由于一个比特位只能表示 0 和 1 两种状态 所以 BitMap 能映射的状态有限 但是使用比特位的优势是能大量的节省内存空间 在 Redis 中 可以把 Bitmaps 想象成一
  • C语言文件操作之fgets()

    来说一说fgets 函数 原型 char fgets char s int n FILE stream 参数 s 字符型指针 指向存储读入数据的缓冲区的地址 n 从流中读入n 1个字符 stream 指向读取的流 返回值 1 当n lt 0
  • 2023华为OD机试真题Python实现【食堂供餐/二分法】

    题目内容 某公司员工食堂以盒饭方式供餐 为将员工取餐排队时间降低为0 食堂的供餐速度必须要足够快 现在需要根据以往员工取餐的统计信息 计算出一个刚好能达成排队时间为0的最低供餐速度 即 食堂在每个单位时间内必须至少做出多少份盒饭才能满足要求
  • 使用easyExcel导入大批量数据

    常用来导入excel的工具有poi 但笔者实测中发现 poi导入的excel数据少于5000条时是没有任何问题的 当导入excel里的数据大于5000条时 内存会被占满 从而导致解析错误 导入失败 这种情况俗称 OOM Out Of Mem
  • 震撼!国产自研多环境开发软件 CEC-IDE 问世!遥遥领先!!

    程序员的成长之路 互联网 程序员 技术 资料共享 关注 阅读本文大概需要 2 8 分钟 来自 网络 侵删 震撼到了 厉害 继国产自研浏览器 国产自研操作系统 国产自研手机系统后的全新力作 国产自研 IDE 它就是 CEC IDE 一款由数字
  • Scanvenger游戏制作笔记(一)Unity3D状态机转换

    Scanvenger游戏制作笔记 一 Unity3D状态机转换 前言 一 打开Controller 二 选择 Parameters 创建新的trigger 三 选择状态转换线 四 返回到原状态 选择返回的剪头 系列链接 前言 本文章是我学习
  • 使用yum 源安装nginx

    执行以下命令 添加Nginx到yum源 sudo rpm Uvh http nginx org packages centos 7 noarch RPMS nginx release centos 7 0 el7 ngx noarch rp
  • 树模型-决策树

    树模型 1 决策树 ID3 C4 5 CART 2 随机森林RF 3 Adaboost 4 GBDT 5 XGboost 6 孤立森林 异常检测 一 决策树 决策树是一种基本的分类和回归方法 用于分类主要借助每一个叶子节点对应一种属性判定
  • torchtext 教程 超详细

    torchtext 教程超详细 https www jianshu com p da3a5d5ed2ba
  • 数字电路实验(02)小规模组合逻辑电路实验1:交通灯状态

    数字电路实验 02 小规模组合逻辑电路实验1 交通灯状态 2020 5 11 一 实验要求 1 1 实验目的 1 认识解决实际组合逻辑问题的一般方法和过程 2 熟悉基本逻辑门的使用 1 2 实验器材 1 2输入与门 2 3输入与门 3 4输
  • Java用类实现结构体的功能

    我们都知道C C 里面的结构体在储存数据的时候很方便 但是在Java中没有Struct 但是我们可以用类来实现Struct的功能 与Struct声明功能一样的类的定义 public class platform private int x
  • C++类模板特化全总结

    基础模板一 template
  • CTR预估的几种方式

    1 CTR预估 CTR预估是计算广告中最核心的算法之一 那么CTR预估是指什么呢 简单来说 CTR预估是对每次广告的点击情况做出预测 预测用户是点击还是不点击 具体定义可以参考 CTR CTR预估和很多因素相关 比如历史点击率 广告位置 时
  • 解决unity5.6之前打包的apk在android8.0真机上运行黑屏的bug

    我的项目使用unity5 4 1开发 在Android8 0的时候启动会出现黑屏 同时发现日志中有这一句日志Unable to query for permission Fragment null must be a public stat
  • Spring事务当中propagation=“REQUIRED“和PROPAGATION=“REQUIRES_NEW“的区别

    3 propagation REQUIRED 和PROPAGATION REQUIRES NEW 的区别 官方 PROPAGATION REQUIRED 支持当前事务 如果当前没有事务 就新建一个事务 这是最常见的选择 PROPAGATIO