Java 为什么@transactional即使在运行新事务时也会回滚?

Java 为什么@transactional即使在运行新事务时也会回滚?,java,spring-boot,Java,Spring Boot,我正在学习spring事务传播。所以我在组织服务中有三个内部服务 组织服务 @Service public class OrganizationServiceImpl implements OrganizationService { @Autowired EmployeeService employeeService; @Autowired HealthIsuranceService healthInsuranceService; @Autowire

我正在学习spring事务传播。所以我在组织服务中有三个内部服务

组织服务

@Service
public class OrganizationServiceImpl implements OrganizationService {

    @Autowired
    EmployeeService employeeService;

    @Autowired
    HealthIsuranceService healthInsuranceService;

    @Autowired
    InsuranceLogService insuranceLogService;

    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public void joinOrganization(Employee employee, EmployeeHealthInsurance employeeHealthInsurance) {
        employeeService.insertEmployee(employee);

        healthInsuranceService.registerEmployeeHealthInsurance(employeeHealthInsurance);

        InsuranceLog insuranceLog = new InsuranceLog();
        insuranceLog.setInformation("new log information");
        insuranceLogService.logIt(insuranceLog);

    }

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

    }
}
@Service
public class HealthInsuranceServiceImpl implements HealthIsuranceService{

    @Autowired
    HealthInsuranceDao healthInsuranceDao;

    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public void registerEmployeeHealthInsurance(EmployeeHealthInsurance employeeHealthInsurance) {
        healthInsuranceDao.registerEmployeeHealthInsurance(employeeHealthInsurance);
    }

    @Override
    public void deleteEmployeeHealthInsuranceById(String empid) {
        healthInsuranceDao.deleteEmployeeHealthInsuranceById(empid);
    }
}
@Service
public class InsuranceLogServiceImpl implements InsuranceLogService{

    @Autowired
    private InsuranceLogDao insuranceLogDao;

    @Override
    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public void logIt(InsuranceLog logInfo){
            int i=0;

            int j =0;
            int result = i/j;
            insuranceLogDao.logIt(logInfo);
    }


}
健康保险服务

@Service
public class OrganizationServiceImpl implements OrganizationService {

    @Autowired
    EmployeeService employeeService;

    @Autowired
    HealthIsuranceService healthInsuranceService;

    @Autowired
    InsuranceLogService insuranceLogService;

    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public void joinOrganization(Employee employee, EmployeeHealthInsurance employeeHealthInsurance) {
        employeeService.insertEmployee(employee);

        healthInsuranceService.registerEmployeeHealthInsurance(employeeHealthInsurance);

        InsuranceLog insuranceLog = new InsuranceLog();
        insuranceLog.setInformation("new log information");
        insuranceLogService.logIt(insuranceLog);

    }

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

    }
}
@Service
public class HealthInsuranceServiceImpl implements HealthIsuranceService{

    @Autowired
    HealthInsuranceDao healthInsuranceDao;

    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public void registerEmployeeHealthInsurance(EmployeeHealthInsurance employeeHealthInsurance) {
        healthInsuranceDao.registerEmployeeHealthInsurance(employeeHealthInsurance);
    }

    @Override
    public void deleteEmployeeHealthInsuranceById(String empid) {
        healthInsuranceDao.deleteEmployeeHealthInsuranceById(empid);
    }
}
@Service
public class InsuranceLogServiceImpl implements InsuranceLogService{

    @Autowired
    private InsuranceLogDao insuranceLogDao;

    @Override
    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public void logIt(InsuranceLog logInfo){
            int i=0;

            int j =0;
            int result = i/j;
            insuranceLogDao.logIt(logInfo);
    }


}
员工服务

@Service
public class EmployeeServiceImpl implements EmployeeService {

    @Autowired
    EmployeeDao employeeDao;

    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public void insertEmployee(Employee employee) {
        employeeDao.insertEmployee(employee);
    }

    @Override
    public void deleteEmployeeById(String empid) {
        employeeDao.deleteEmployeeById(empid);
    }
}
员工日志服务

@Service
public class OrganizationServiceImpl implements OrganizationService {

    @Autowired
    EmployeeService employeeService;

    @Autowired
    HealthIsuranceService healthInsuranceService;

    @Autowired
    InsuranceLogService insuranceLogService;

    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public void joinOrganization(Employee employee, EmployeeHealthInsurance employeeHealthInsurance) {
        employeeService.insertEmployee(employee);

        healthInsuranceService.registerEmployeeHealthInsurance(employeeHealthInsurance);

        InsuranceLog insuranceLog = new InsuranceLog();
        insuranceLog.setInformation("new log information");
        insuranceLogService.logIt(insuranceLog);

    }

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

    }
}
@Service
public class HealthInsuranceServiceImpl implements HealthIsuranceService{

    @Autowired
    HealthInsuranceDao healthInsuranceDao;

    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public void registerEmployeeHealthInsurance(EmployeeHealthInsurance employeeHealthInsurance) {
        healthInsuranceDao.registerEmployeeHealthInsurance(employeeHealthInsurance);
    }

    @Override
    public void deleteEmployeeHealthInsuranceById(String empid) {
        healthInsuranceDao.deleteEmployeeHealthInsuranceById(empid);
    }
}
@Service
public class InsuranceLogServiceImpl implements InsuranceLogService{

    @Autowired
    private InsuranceLogDao insuranceLogDao;

    @Override
    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public void logIt(InsuranceLog logInfo){
            int i=0;

            int j =0;
            int result = i/j;
            insuranceLogDao.logIt(logInfo);
    }


}
以下是我的设想:-

  • insertEmployee(employee)
    registereemployeehealthsurance(employeehealthsurance)
    应在同一事务中运行,以便它们中的任何一个一起提交或一起回滚<当提交这两个事务时,code>logIt(保险日志)是可选的。记录失败不应回滚到上述事务

  • logIt(insuranceLog)
    当插入员工(employee),
    注册员工健康保险(employeeHealthInsurance)
    中的任何一个未能提交时,也应全部回滚


  • 如何实现这一点?

    您可以从
    joinOrganization
    方法中删除
    logIt
    方法,并创建一个包装器方法,该方法首先执行
    joinOrganization
    。只要
    joinOrganization
    成功提交,您就可以运行
    logIt
    。如果
    joinOrganization
    失败,不要运行
    logIt
    。我认为@pero\u hero是对的,最好简化事务使用。