mysql innodb过程在事务中只激活一次(调用serveral次)

1mrurvl1  于 2021-06-20  发布在  Mysql
关注(0)|答案(1)|浏览(285)

首先,我做了一个mysql过程,每次增加一个数字[+1](里面没有事务),我在一个spring事务中调用了这个过程n次(n>1),得到了一个相同的数字,最后这个数字是+1(+n)
其次,在procedure中添加transaction和commit-atfer+1,得到与上面相同的结果;
第三,我在方法a(a calls procedure)上添加了@transaction(rollbackor=exception.class,propagation=propagation.requires\u new),在方法b中多次调用方法a,方法b被@transactional注解,得到了与上面相同的结果;
有人帮忙吗?你能告诉我怎么处理吗?
加:mysql中的表

CREATE TABLE `SEQUENCE` (
`ID`  bigint(10) NOT NULL ,
`COUNT`  int(11) NOT NULL ,
`CUR_DATE`  date NOT NULL ,
`READ_ME`  varchar(20) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL ,
PRIMARY KEY (`ID`)
);

mysql中的过程

CREATE DEFINER="root"@"%" PROCEDURE "SEQUENCE_PROCEDURE"(IN _id bigint)
BEGIN
    UPDATE `SEQUENCE` SET `COUNT`=-1,CUR_DATE=now() where `ID`=_id and TIMESTAMPDIFF(DAY,CUR_DATE,now())>0 and _id=1;
    UPDATE `SEQUENCE` SET `COUNT`=-1,CUR_DATE=now() where `ID`=_id and TIMESTAMPDIFF(MONTH,CUR_DATE,now())>0 and _id=2;
    UPDATE `SEQUENCE` SET `COUNT`=`COUNT`+1 where `ID`=_id;
    SELECT * FROM `SEQUENCE` where `ID`=_id;
END

mybatis中的sql

<select id="getSequence" parameterType="java.lang.Long" resultMap="baseResult" statementType="CALLABLE">
    {call SEQUENCE_PROCEDURE(#{id,jdbcType=BIGINT,mode=IN})}
</select>

项目中的测试

@Test
@Transactional
public void testSequence() {
    System.out.println(sequenceService.getId(2L));
    System.out.println(sequenceService.getId(2L));
    System.out.println(sequenceService.getId(2L));
}

哪里

public String getId(Long id) {
    Sequence sequence = sequenceMapper.getSequence(id);
    String temp='000000000000'+sequence.getCount();
    return temp.substring(temp.length-12);
}

试验结果

000000000000 000000000000 000000000000

预期结果

000000000000 000000000001 000000000002

在过程中添加启动事务和提交不起作用!

mitkmikd

mitkmikd1#

在您的过程中,第1行和第2行对id 1和2有特定的查询(为什么?),并对其进行双重比较 _id 变量。第三个查询也在另一个表上运行 COUNT ,可能是错误。
我更改了假设记录已经存在的过程,并将第三个查询更改为 SEQUENCE 表格:

CREATE PROCEDURE SEQUENCE_PROCEDURE(IN _id bigint)
BEGIN

    UPDATE `SEQUENCE` SET `COUNT`=-1,CUR_DATE=now() 
    WHERE `ID`=_id and TIMESTAMPDIFF(DAY,CUR_DATE,now())>0;

    UPDATE `SEQUENCE` SET `COUNT`=`COUNT`+1 where `ID`=_id;

    SELECT * FROM `SEQUENCE` where `ID`=_id;
END

相关问题