猿问

Spring boot 中事务注解@Transactional 回滚失效问题

Spring boot 中

@Transactional

注解,没有进行有效回滚

代码如下

package com.imooc;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.beans.Expression;

@Service

public class GirlService {

    @Autowired
    private GirlRepository girlRepository;

    @Transactional
    public void insertTwo(){
        Girl girlA=new Girl();
        girlA.setCapSize("A");
        girlA.setAge(28);
        girlRepository.save(girlA);

        Girl girlB=new Girl();
        girlB.setCapSize("BGG");
        girlB.setAge(28);
        girlRepository.save(girlB);

    }
}

检查过MySQL的引擎是InnoDB ,支持事务。

其中数据库表Girl的capSize字段长度为1 ,这样第二笔插入会报错,应该回滚第一笔插入也取消。但是实际执行结果是girlA仍然插入成功了。为什么会这样啊?求解

慕前端9553269
浏览 4896回答 3
3回答

慕的地8582982

UGTJNTUMINAIXXDGTXLASFVZVPCZZSBVJVFEXBBBYHVGOBYVTCPGFPWHWVZUUXWPZOORUXHIOVHLFDPMKAJDDAXQNTTSISHIYFPSCWZWHGAWJTVHILUCLLBAJBTZQMGDAMTDDDGHHBLLYYLXTXHSFBZEIIAGAKUMMTPAAYLYSSCCOEYKSEBILPSIRSVQWIQAYHBNHLRAQUKAEEUAXYIKORURAPMIGFSMBAYCUBCNRTGWQSUCZTPOTWTNZXCXIMHTEAJSVCCNCHUQAVTLNLTWDJTFDPKPLWXYRFYHHGTXQELJQLQQLVLWHDRHQCGUZAZVWDVIFHJGHQKFBGTWVNBQAGCISDZQSZVGAHZRBZNDRQLIISIBBFJPCSRYVNLQVUOVFVLVPWMNLHOSWTWAKIOOOMQHNQUFPMMMQITTDXRDAWGXRKBOIWZPJZQPTQTMXQTKQJXCLOHUEBPEYPCGZRAAJCVCIMCBMQJMSRIPCYAKURWGLVSIELFJMGNCFILGKENHHDMTJIEAATBOHWTDQMSWDTAMJDARGULVECZYYVVVZTTJTKZWWYKRKEQGWCEUUQQTLUUYXHOZNTDNROETTDFBUYWJZXWNVLIVZJSSTMZHGLBDWAGTSMTRUGTNQWZXOBRTEXVUXPOLBEQDXEGSGWWWNTVKEYIWLFGRNPHVVEKKBBCOIPVLWTWJQQCFVBLRXGRIWSUJHUQHQJNGLEIWAXKTTTRYASSRMSYPVVQRKKWDKCJQMRUYJCGGVSWNZJUDEUUULOMKUDQTFJENRIEUDOEUIRLFPJCPCCPXQJSEQAUUEEUKHNEYXXIVZAXUELYSOYOSYEIJQMJCWVVNMGQTFLDWAPYOFCRNMTUADDCQGVHEEXKNGCNG

慕前端9553269

非常感谢。还是我粗心了。原来JPA自动创建的表的引擎默认是MyISAM。修改成InnoDB 就正常回滚了
随时随地看视频慕课网APP

相关分类

Java
我要回答