dao层互相调用事物_Spring嵌套事物的简单测试

该博客通过实例分析了Spring Boot和Mybatis集成下,Service外层与内层嵌套调用时的事务处理。当内外层方法都无try-catch、都包含try-catch、外层有try-catch、内层有try-catch四种情况时,异常如何影响事务的回滚。总结得出:内外层无try-catch时,内外部异常都会导致事务回滚;内外层都有try-catch时,外部异常可能导致事务失败,内部异常则会导致事务回滚或失败;外层有try-catch、内层无try-catch时,内部异常会回滚,外部异常不会;外层无try-catch、内层有try-catch时,内外部异常都会导致事务失败。
摘要由CSDN通过智能技术生成

整体项目很简单,为了方便,用了Spring Boot和Mybatis集成的方式对数据库进行操作,事物的传播属性是默认的PROPAGATION_REQUIRED.这里最主要是Service外层和Service内层的嵌套,大概看看都能看懂(注:for循环从0开始了,没做修改,忽略数据库id不能为0的无用数据,操作过程中已删除)。

下面分情况介绍异常:

Application层(没用这个,直接用了Junit测试类)

---------------------------------------------------------------------------------------------------------------------------------

package com.sh.springboot;

import org.mybatis.spring.annotation.MapperScan;

import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;

/**

* Created by sh on 18/4/28.

*/

@SpringBootApplication

@MapperScan("com.sh.springboot.dao")

public class
SpringbootApplication
{

public static void main(String[] args) {

SpringApplication.run(SpringbootApplication.class, args);

}

/*

* @Autowired private ServiceWaiCeng s;

*

* @RequestMapping("/") String home()
throws Exception {

* System.out.println("controller 正常执行"); s.insertWaiCeng();;

*

* return " 正常返回Hello World!"; }

*/

}

Test测试类

---------------------------------------------------------------------------------------------------------------------------------

package com.sh.springboot;

import org.junit.Test;

import org.junit.runner.RunWith;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.boot.test.context.SpringBootTest;

import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import com.sh.springboot.servicewaiceng.ServiceWaiCeng;

/**

* Created by sh on 18/4/28.

*/

@RunWith(SpringJUnit4ClassRunner.class)

@SpringBootTest(classes = SpringbootApplication.class)

public class
PersonDaoTest {

@Autowired

private ServiceWaiCeng servicewaiceng;

@Test

public void test() {

servicewaiceng.insertWaiCeng();

System.out.println("Controller正常执行");

}

}

---------------------------------------------------------------------------------------------------------------------------------

Service外层

---------------------------------------------------------------------------------------------------------------------------------

package com.sh.springboot.servicewaiceng.impl;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Component;

import org.springframework.transaction.annotation.Transactional;

import com.sh.springboot.serviceneiceng.ServiceNeiCeng;

import com.sh.springboot.servicewaiceng.ServiceWaiCeng;

/**

* Created by sh on 18/4/28.

*/

@Component

public class
ServiceWaiCengImpl implements ServiceWaiCeng {

@Autowired

private ServiceNeiCeng snc;

@Transactional

public void insertWaiCeng() {

for (int j = 0; j < 8; j++) {

snc.insertNeiCeng(j, j + "姓名");

}

System.out.println("外层正常执行");

}

}

---------------------------------------------------------------------------------------------------------------------------------

Service内层

---------------------------------------------------------------------------------------------------------------------------------

package com.sh.springboot.serviceneiceng.impl;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;

import org.springframework.transaction.annotation.Transactional;

import com.sh.springboot.bean.Person;

import com.sh.springboot.dao.PersonDao;

import com.sh.springboot.serviceneiceng.ServiceNeiCeng;

/**

* Created by sh on 18/4/28.

*/

@Service

public class
ServiceNeiCengImpl implements ServiceNeiCeng {

@Autowired

PersonDao
persondao;

@Transactional

public void insertNeiCeng(int id, String name) {

Person
person
= new
Person();

person.setId(id);

person.setName(name);

persondao.insertPerson(person);

System.out.println(id + "内层正常执行");

}

}

---------------------------------------------------------------------------------------------------------------------------------

一、内外都无Try Catch

1.外部有异常,内部无异常

修改Service外层代码如下:

---------------------------------------------------------------------------------------------------------------------------------

package com.sh.springboot.servicewaiceng.impl;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Component;

import org.springframework.transaction.annotation.Transactional;

import com.sh.springboot.serviceneiceng.ServiceNeiCeng;

import com.sh.springboot.servicewaiceng.ServiceWaiCeng;

/**

* Created by sh on 18/4/28.

*/

@Component

public class
ServiceWaiCengImpl implements ServiceWaiCeng {

@Autowired

private ServiceNeiCeng snc;

@Transactional

public void insertWaiCeng() {

for (int j = 0; j < 8; j++) {

snc.insertNeiCeng(j, j +

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值