Spring Boot事务-了解事务传播

Posted 独孤文彬

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Spring Boot事务-了解事务传播相关的知识,希望对你有一定的参考价值。

视频讲解地址:(预留:待上传……)
让我们开始吧:

什么是事务的传播?

任何应用程序都涉及调用其他服务或组件的许多服务或组件。事务传播指示任何组件或服务是否会或不会参与事务,以及调用主叫组件/服务已创建或未创建事务的行为。

我们将利用上一章开发的Spring Boot Transaction项目。它具有组织服务,可以呼叫员工服务和健康保险服务。
同样在前面的示例中,我们仅将事务注释添加到了组织服务中。
但是,假设用户希望通过两种方式调用雇员服务,即

- 使用组织服务通话

- 直接致电员工服务。

由于也可以直接调用Employee Service,因此我们还需要将Transaction批注与Employee Service一起使用。因此,服务-组织服务和员工服务都将使用事务注释。

我们将通过观察组织和员工服务的行为来研究各种传播方案。事务传播有六种类型

  • REQUIRED
  • SUPPORTS
  • NOT_SUPPORTED
  • REQUIRES_NEW
  • NEVER
  • MANDATORY

事务传播- REQUIRED(默认事务传播)


在这里,组织服务和员工服务均具有定义为Required的事务传播。这是默认的事务传播。
下面是代码:
-组织服务如下:

package com.javainuse.service.impl;

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

import com.javainuse.model.Employee;
import com.javainuse.model.EmployeeHealthInsurance;
import com.javainuse.service.EmployeeService;
import com.javainuse.service.HealthInsuranceService;
import com.javainuse.service.OrganizationService;

@Service
@Transactional
public class OrganzationServiceImpl implements OrganizationService 

	@Autowired
	EmployeeService employeeService;

	@Autowired
	HealthInsuranceService healthInsuranceService;

	@Override
	public void joinOrganization(Employee employee, EmployeeHealthInsurance employeeHealthInsurance) 
		employeeService.insertEmployee(employee);
		if (employee.getEmpId().equals("emp1")) 
			throw new RuntimeException("thowing exception to test transaction rollback");
		
		healthInsuranceService.registerEmployeeHealthInsurance(employeeHealthInsurance);
	

	@Override
	public void leaveOrganization(Employee employee, EmployeeHealthInsurance employeeHealthInsurance) 
		employeeService.deleteEmployeeById(employee.getEmpId());
		healthInsuranceService.deleteEmployeeHealthInsuranceById(employeeHealthInsurance.getEmpId());
	

员工服务如下:

package com.javainuse.service.impl;

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

import com.javainuse.dao.EmployeeDao;
import com.javainuse.model.Employee;
import com.javainuse.service.EmployeeService;

@Service
@Transactional
public class EmployeeServiceImpl implements EmployeeService 

	@Autowired
	EmployeeDao employeeDao;

	@Override
	public void insertEmployee(Employee employee) 
		employeeDao.insertEmployee(employee);
	

	@Override
	public void deleteEmployeeById(String empid) 
		employeeDao.deleteEmployeeById(empid);
	


使用OrganizationService调用输出
EmployeeService-


直接调用EmployeeService-

事务传播- Supports


在这里,两个组织服务都将事务传播定义为“ REQUIRED”, 而“员工服务”将事务传播定义为“ Supports”。

代码如下:
-组织服务如下:

 package com.javainuse.service.impl;

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

import com.javainuse.model.Employee;
import com.javainuse.model.EmployeeHealthInsurance;
import com.javainuse.service.EmployeeService;
import com.javainuse.service.HealthInsuranceService;
import com.javainuse.service.OrganizationService;

@Service
@Transactional
public class OrganzationServiceImpl implements OrganizationService 

	@Autowired
	EmployeeService employeeService;

	@Autowired
	HealthInsuranceService healthInsuranceService;

	@Override
	public void joinOrganization(Employee employee, EmployeeHealthInsurance employeeHealthInsurance) 
		employeeService.insertEmployee(employee);
		if (employee.getEmpId().equals("emp1")) 
			throw new RuntimeException("thowing exception to test transaction rollback");
		
		healthInsuranceService.registerEmployeeHealthInsurance(employeeHealthInsurance);
	

	@Override
	public void leaveOrganization(Employee employee, EmployeeHealthInsurance employeeHealthInsurance) 
		employeeService.deleteEmployeeById(employee.getEmpId());
		healthInsuranceService.deleteEmployeeHealthInsuranceById(employeeHealthInsurance.getEmpId());
	


员工服务如下:

package com.javainuse.service.impl;

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

import com.javainuse.dao.EmployeeDao;
import com.javainuse.model.Employee;
import com.javainuse.service.EmployeeService;

@Service
@Transactional(propagation=Propagation.SUPPORTS)
public class EmployeeServiceImpl implements EmployeeService 

	@Autowired
	EmployeeDao employeeDao;

	@Override
	public void insertEmployee(Employee employee) 
		employeeDao.insertEmployee(employee);
	

	@Override
	public void deleteEmployeeById(String empid) 
		employeeDao.deleteEmployeeById(empid);
	


使用OrganizationService调用输出


直接调用EmployeeService-

交易传播-NOT_SUPPORTED


这里的组织服务,我们定义了事务传播为REQUIRED和员工服务都定义为事务传播NOT_SUPPORTED
代码如下:
组织服务如下:

package com.javainuse.service.impl;

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

import com.javainuse.model.Employee;
import com.javainuse.model.EmployeeHealthInsurance;
import com.javainuse.service.EmployeeService;
import com.javainuse.service.HealthInsuranceService;
import com.javainuse.service.OrganizationService;

@Service
@Transactional
public class OrganzationServiceImpl implements OrganizationService 

	@Autowired
	EmployeeService employeeService;

	@Autowired
	HealthInsuranceService healthInsuranceService;

	@Override
	public void joinOrganization(Employee employee, EmployeeHealthInsurance employeeHealthInsurance) 
		employeeService.insertEmployee(employee);
		if (employee.getEmpId().equals("emp1")) 
			throw new RuntimeException("thowing exception to test transaction rollback");
		
		healthInsuranceService.registerEmployeeHealthInsurance(employeeHealthInsurance);
	

	@Override
	public void leaveOrganization(Employee employee, EmployeeHealthInsurance employeeHealthInsurance) 
		employeeService.deleteEmployeeById(employee.getEmpId());
		healthInsuranceService.deleteEmployeeHealthInsuranceById(employeeHealthInsurance.getEmpId());
	

员工服务如下:

package com.javainuse.service.impl;

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

import com.javainuse.dao.EmployeeDao;
import com.javainuse.model.Employee;
import com.javainuse.service.EmployeeService;

@Service
@Transactional(propagation=Propagation.NOT_SUPPORTED)
public class EmployeeServiceImpl implements EmployeeService 

	@Autowired
	EmployeeDao employeeDao;

	@Override
	public void insertEmployee(Employee employee) 
		employeeDao.insertEmployee(employee);
	

	@Override
	public void deleteEmployeeById(String empid) 
		employeeDao.deleteEmployeeById(empid);
	


使用OrganizationService调用输出
EmployeeService-


直接调用EmployeeService-

交易传播-REQUIRES_NEW


在这里,对于组织服务,我们已将事务传播定义为REQUIRED,而员工服务将事务传播定义为REQUIRES_NEW

代码
-组织服务将如下所示:

package com.javainuse.service.impl;

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

import com.javainuse.model.Employee;
import com.javainuse.model.EmployeeHealthInsurance;
import com.javainuse.service.EmployeeService;
import com.javainuse.service.HealthInsuranceService;
import com.javainuse.service.OrganizationService;

@Service
@Transactional
public class OrganzationServiceImpl implements OrganizationService 

	@Autowired
	EmployeeService employeeService;

	@Autowired
	HealthInsuranceService healthInsuranceService;

	@Override
	public void joinOrganization(Employee employee, EmployeeHealthInsurance employeeHealthInsurance) 
		employeeService.insertEmployee(employee);
		if (employee.getEmpId().equals("emp1")) 
			throw new RuntimeException("thowing exception to test transaction rollback");
		
		healthInsuranceService.registerEmployeeHealthInsurance(employeeHealthInsurance);
	

	@Override
	public void leaveOrganization(Employee employee, EmployeeHealthInsurance employeeHealthInsurance) 
		employeeService.deleteEmployeeById(employee.getEmpId());
		healthInsuranceService.deleteEmployeeHealthInsuranceById(employeeHealthInsurance.getEmpId());
	

员工服务如下:

package com.javainuse.service.impl;

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

import com.javainuse.dao.EmployeeDao;
import com.javainuse.model.Employee;
import com.javainuse.service.EmployeeService;

@Service
@Transactional(propagation=Propagation.REQUIRES_NEW)
public class EmployeeServiceImpl implements EmployeeService 

	@Autowired
	EmployeeDao employeeDao;

	@Override
	public void insertEmployee(Employee employee) 
		employeeDao.insertEmployee(employee);
	

	@Override
	public void deleteEmployeeById(String empid) 
		employeeDao.deleteEmployeeById(empid);
	


使用OrganizationService调用输出
EmployeeService


直接调用EmployeeService-

事务传播- Never


在这里,对于组织服务,我们已将事务传播定义为“ REQUIRED”, 而“员工服务”将事务传播定义为“ NEVER s”
代码
-组织服务将如下所示:

package com.javainuse.service.impl;

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

import com.javainuse.model.Employee;
import com.javainuse.model.EmployeeHealthInsurance;
import com.javainuse.service.EmployeeService;
import com.javainuse.service.HealthInsuranceService;
import com.javainuse.service.OrganizationService;

@Service
@Transactional
public class OrganzationServiceImpl implements OrganizationService 

	@Autowired
	EmployeeService employeeService;

	@Autowired
	HealthInsuranceService healthInsuranceService;

	@Override
	public void joinOrganization(Employee employee, EmployeeHealthInsurance employeeHealthInsurance) 
		employeeService.insertEmployee(employee);
		if (employee.getEmpId().equals("emp1")) 
			throw new RuntimeException("thowing exception to test transaction rollback");
		
		healthInsuranceService.registerEmployeeHealthInsurance(employeeHealthInsurance);
	

	@Override
	public void leaveOrganization(Employee employee, EmployeeHealthInsurance employeeHealthInsurance) 
		employeeService.deleteEmployeeById(employee.getEmpId());
		healthInsuranceService.deleteEmployeeHealthInsuranceById(employeeHealthInsurance.getEmpId());
	

员工服务如下:

package com.javainuse.service.impl;

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

import com.javainuse.dao.EmployeeDao;
import com.javainuse.model.Employee;
import com.javainuse.service.EmployeeService;

@Service
@Transactional(propagation=Propagation.NEVER)
public class EmployeeServiceImpl implements EmployeeService 

	@Autowired
	EmployeeDao employeeDao;

	@Override
	public void insertEmployee(Employee employee) 
		employeeDao.insertEmployee(employee);
	

	@Override
	public void deleteEmployeeById(String empid) 
		employeeDao.deleteEmployeeById(empid);
	


使用OrganizationService调用输出
EmployeeService-


直接调用EmployeeService-

事务传播- 强制性


在这里,对于组织服务,我们已将交易传播定义为“ REQUIRED” ,而员工服务的交易传播定义为“ Mandatory”。

组织服务将如下所示:

 package com.javainuse.service.impl;

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

import com.javainuse.model.Employee;
import com.javainuse.model.EmployeeHealthInsurance;
import com.javainuse.service.EmployeeService;
import com.javainuse.service.HealthInsuranceService;
import com.javainuse.service.OrganizationService;

@Service
@Transactional
public class OrganzationServiceImpl implements OrganizationService 

	@Autowired
	EmployeeService employeeService;

	@Autowired
	HealthInsuranceService healthInsuranceService;

	@Override
	public void joinOrganization(Employee employee, EmployeeHealthInsurance employeeHealthInsurance) 
		employeeService.insertEmployee(employee);
		if (employee.getEmpId().equals("emp1")) 
			throw new RuntimeException("thowing exception to test transaction rollback");
		
		healthInsuranceService.registerEmployeeHealthInsurance(employeeHealthInsurance);
	

	@Override
	public void leaveOrganization(Employee employee, EmployeeHealthInsurance employeeHealthInsurance) 
		employeeService.deleteEmployeeById(employee.getEmpId());
		healthInsur

以上是关于Spring Boot事务-了解事务传播的主要内容,如果未能解决你的问题,请参考以下文章

Spring Boot事务和事务传播机制

Spring Boot事务管理-事务回滚示例

Spring Boot事务管理-事务回滚示例

Spring Boot事务和事务传播机制

Spring Boot-事务隔离教程

Spring Boot-事务隔离教程