spring事务的传播性和隔离性及使用场景

springboot开启事务管理:

package com.userModule.application;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.transaction.annotation.EnableTransactionManagement;

/**
 * @EnableTransactionManagement
 * 开启事务支持
 * @author 梦天涯
 */

@EnableTransactionManagement
@SpringBootApplication
@EnableEurekaClient
@ComponentScan(basePackages = {"com.userModule"})
//mapper的扫描路径
@MapperScan("com.userModule.mapper")
public class UserModuleApplication {
	public static void main(String[] args) {
		SpringApplication.run(UserModuleApplication.class, args);
	}
}
package com.userModule.serviceimpl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.cloud.Model.Unpay;
import com.cloud.util.PageView;
import com.cloud.util.ServiceRsObjModel;
import com.userModule.mapper.UnpayMapper;
import com.userModule.service.UnpayService;

public class UnpaySeviceImpl implements UnpayService{

	@Autowired
	UnpayMapper unpayMapper;
	
	@Override
	/**
	 *	事物传播行为介绍: 
		@Transactional(propagation=Propagation.REQUIRED) 
		如果有事务, 那么加入事务, 没有的话新建一个事务(默认情况下)
		@Transactional(propagation=Propagation.NOT_SUPPORTED) 
		容器不为这个方法开启事务
		@Transactional(propagation=Propagation.REQUIRES_NEW) 
		不管是否存在事务,都创建一个新的事务,原来的挂起,新的执行完毕,继续执行老的事务
		@Transactional(propagation=Propagation.MANDATORY) 
		必须在一个已有的事务中执行,否则抛出异常
		@Transactional(propagation=Propagation.NEVER) 
		必须在一个没有的事务中执行,否则抛出异常(与Propagation.MANDATORY相反)
		@Transactional(propagation=Propagation.SUPPORTS) 
		如果其他bean调用这个方法,在其他bean中声明事务,那就用事务.如果其他bean没有声明事务,那就不用事务.
		
		事物超时设置:
		@Transactional(timeout=30) //默认是30秒
		
		事务隔离级别:
		@Transactional(isolation = Isolation.READ_UNCOMMITTED)
		读取未提交数据(会出现脏读, 不可重复读) 基本不使用
		@Transactional(isolation = Isolation.READ_COMMITTED)
		读取已提交数据(会出现不可重复读和幻读)
		@Transactional(isolation = Isolation.REPEATABLE_READ)
		可重复读(会出现幻读)
		@Transactional(isolation = Isolation.SERIALIZABLE)
		串行化
	 */
	
	@Transactional(propagation=Propagation.REQUIRED,rollbackFor=Exception.class,isolation=Isolation.READ_COMMITTED)
	public ServiceRsObjModel addUnpay(Unpay unpay) {
		ServiceRsObjModel result=new ServiceRsObjModel();
		try {
			Integer add=unpayMapper.addUnpay(unpay);
			if(add>0) {
				result.setSuccess(true);
			}else {
				result.setSuccess(false);
			}
		}catch(Exception e) {
			result.setSuccess(false);
			result.setMessage(e.getMessage());
		}
		return result;
	}

	@Override
	public ServiceRsObjModel delUnpay(Unpay unpay) {
		ServiceRsObjModel result=new ServiceRsObjModel();
		try {
			Integer del=unpayMapper.delUnpay(unpay);
			if(del>0) {
				result.setSuccess(true);
			}else {
				result.setSuccess(false);
			}
		}catch(Exception e) {
			result.setSuccess(false);
			result.setMessage(e.getMessage());
		}
		return result;
	}

	@Override
	public ServiceRsObjModel updateUnpay(Unpay unpay) {
		ServiceRsObjModel result=new ServiceRsObjModel();
		try {
			Integer update=unpayMapper.updateUnpay(unpay);
			if(update>0) {
				result.setSuccess(true);
			}else {
				result.setSuccess(false);
			}
		}catch(Exception e) {
			result.setSuccess(false);
			result.setMessage(e.getMessage());
		}
		return result;
	}

	@Override
	public ServiceRsObjModel<List<Unpay>> queryUnpay(Unpay unpay) {
		ServiceRsObjModel result=new ServiceRsObjModel();
		try {
			List<Unpay> listUnpay=unpayMapper.queryUnpay(unpay);
			result.setRsData(listUnpay);
		}catch(Exception e) {
			result.setSuccess(false);
			result.setMessage(e.getMessage());
		}
		return result;
	}

	@Override
	public ServiceRsObjModel<PageView<Unpay>> queryUnpayPage(PageView<Unpay> pageView,Unpay unpay) {
		ServiceRsObjModel<PageView<Unpay>> result=new ServiceRsObjModel<PageView<Unpay>>();
		try {
			List<Unpay> listUnpayPage=unpayMapper.queryUnpayPage(unpay);
			pageView.setResult(listUnpayPage);
			result.setRsData(pageView);
		}catch(Exception e) {
			result.setSuccess(false);
			result.setMessage(e.getMessage());
		}
		return result;
	}

}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值