测试Servicec层调用Service层的事务问题

controller层代码,很简单,调用一个service,其中一个是没有异常的,一个是要手动抛异常模拟程序出现异常的

package com.ky.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.ky.service.TsService;


@RestController
@RequestMapping("/testts")
public class TestTranController {
	
	@Autowired
	private TsService tsService;
	
	private Logger log = LoggerFactory.getLogger(VpnOperateController.class);
	
	@RequestMapping("/save")
	public String save() {
		try {
			tsService.tsInsert();//后续修改service内部方法抛异常
			return "ok";
		} catch (Exception e) {
			log.error("",e);
			return "error";
		}
	}
	
	@RequestMapping("/save2")
	public String save2() {
		try {
			tsService.insert();//不手动抛异常
			return "ok";
		} catch (Exception e) {
			log.error("",e);
			return "error";
		}
	}

}

内部嵌套的service层-----------有两个service实现类,接口省略,mapper是mybatis

package com.ky.service.impl;

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

import com.ky.dao.TspeedLogMapper;
import com.ky.model.TspeedLog;
import com.ky.sdn.common.base.BaseServiceImpl;
import com.ky.service.TtestSpeedLogService;

public class TtestSpeedLogServiceImpl extends BaseServiceImpl<TspeedLog> implements TtestSpeedLogService {
	@Autowired
	private TspeedLogMapper tspeedLogMapper;

	@Override
	public void save(TspeedLog t) {
		tspeedLogMapper.save(t);
	}
	
	@Override
	public void saveT(TspeedLog t) {
		tspeedLogMapper.save(t);
		throw new RuntimeException();
	}
	

}
package com.ky.service.impl;

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

import com.ky.dao.TnoticeMapper;
import com.ky.model.Tnotice;
import com.ky.sdn.common.base.BaseServiceImpl;
import com.ky.service.TtestNoticeService;

public class TtestNoticeServiceImpl extends BaseServiceImpl<Tnotice> implements TtestNoticeService  {
	
	@Autowired
	private TnoticeMapper tnoticeMapper;

	@Override
	public void save(Tnotice t) {
		tnoticeMapper.save(t);
	}

	@Override
	public void saveT(Tnotice t) {
		tnoticeMapper.save(t);
		throw new RuntimeException();
	}

}

1,外面嵌套的service层,调用的内部service,两个都抛出异常

package com.ky.service.impl;

import java.util.Date;

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

import com.ky.model.Tnotice;
import com.ky.model.TspeedLog;
import com.ky.service.TsService;
import com.ky.service.TtestNoticeService;
import com.ky.service.TtestSpeedLogService;

public class TsServiceImpl implements TsService {

	@Autowired
	private TtestNoticeService ttestNoticeService;
	
	@Autowired
	private TtestSpeedLogService ttestSpeedLogService;
	
	@Override
	public void tsInsert() {
		Tnotice t = new Tnotice();
		t.setCompanyId(7l);
		t.setContent("aaaaaaaaaaaaaaaaaaaaaaaa");
		ttestNoticeService.saveT(t);//方法会抛异常
		
		TspeedLog tsl = new TspeedLog();
		tsl.setCompanyId(7l);
		tsl.setOptType(1);
		tsl.setBeginTime(new Date());
		tsl.setFinished(0);
		ttestSpeedLogService.saveT(tsl);//方法会抛异常
	}

	@Override
	public void insert() {
		Tnotice t = new Tnotice();
		t.setCompanyId(7l);
		t.setContent("aaaaaaaaaaaaaaaaaaaaaaaa");//方法不会抛异常
		ttestNoticeService.save(t);
		
        int i = 1/0;//抛异常

		TspeedLog tsl = new TspeedLog();
		tsl.setCompanyId(7l);
		tsl.setOptType(1);
		tsl.setBeginTime(new Date());
		tsl.setFinished(0);
		ttestSpeedLogService.save(tsl);//方法不会抛异常
		
	}
}

测试:

1.1请求   testts/save,可以发现内嵌的service执行完一条插入语句后抛异常,数据库两条数据没有插入;

1.2请求 testts/save2 ,可以发现内嵌的service执行完一条插入语句后,外嵌的service层抛异常,理想状态是这条插入的数据应该回滚,实际上数据库也没有数据进行插入,证明service外嵌套的事务是成功配置的

1.3将 方法insert()里面的int i = 1/0注释,请求testts/save2,发现两条数据插入正常

 

结论:一个外层的service,方法里面调用两个有事务的service(内嵌service),外层service也是也是具有事务的

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值