如何在Spring Boot中的事务回滚启用类中禁用方法的事务回滚。

huangapple 未分类评论43阅读模式
英文:

How to disable transaction rollback a method in transaction rollback enable class in spring boot

问题

我有两个方法分别命名为saveNewCustormerDetails和storeFailCustomerSaveDetails。如果在saveNewCustormerDetails方法内部出现任何异常,事务将会回滚,当发生异常时,我需要将异常消息与客户手机号一起存储在数据库中。为了实现这一点,我使用storeFailCustomerSaveDetails方法。但是它不起作用,因为回滚还会影响存储失败的客户保存细节。所以,我需要仅针对storeFailCustomerSaveDetails方法禁用回滚过程。有没有办法实现这一点?

@Service
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
public class CustomerServiceImpl implements CustormerService {

    private final CustomerRepository customerRepository;
    private final CustomerSaveFailsRepository customerSaveFailsRepository;

    @Autowired
    public CustomerServiceImpl(CustomerRepository customerRepository, CustomerSaveFailsRepository customerSaveFailsRepository) {
        this.customerRepository = customerRepository;
        this.customerSaveFailsRepository = customerSaveFailsRepository;
    }

    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    @Override
    public void saveNewCustormerDetails(CustomerDTO customerDTO) {
        try {
            CustomerEntity customerEntity = new CustomerEntity();
            BeanUtils.copyProperties(customerDTO, customerEntity);
            customerRepository.save(customerEntity);
        } catch (Exception e) {
            LOGGER.error("Method saveNewCustormerDetails : " + e.getMessage(), e);
            storeFailCustomerSaveDetails(customerDTO, e);
            throw e;
        }
    }

    private void storeFailCustomerSaveDetails(CustomerDTO customerDTO, Exception ex) {
        try {
            CustomerSaveFailsEntity customerSaveFailsEntity = new CustomerSaveFailsEntity(customerDTO.getContact(), ex.getMessage());
            customerSaveFailsRepository.save(customerSaveFailsEntity);
        } catch (Exception e) {
            LOGGER.error("Method storeFailCustomerSaveDetails : " + e.getMessage(), e);
        }
    }
}
英文:

I have two methods named saveNewCustormerDetails and storeFailCustomerSaveDetails. If any exception jump inside saveNewCustormerDetails method then transaction will be the rollback, when an exception occurs, I need to store the message of exception in the database with customer mobile number. To do it, I use the storeFailCustomerSaveDetails method. but It does not work because also rollback store fails customers saving details. So, I need to disable the rollback process only for the storeFailCustomerSaveDetails method. does there any way to do it?

@Service
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
public class CustomerServiceImpl implements CustormerService {

		private final CustomerRepository customerRepository;
		private final CustomerSaveFailsRepository customerSaveFailsRepository;


	@Autowired
	public CustomerServiceImpl(CustomerRepository customerRepository, CustomerSaveFailsRepository customerSaveFailsRepository){
			this.customerRepository = customerRepository; 
			this.customerSaveFailsRepository = customerSaveFailsRepository;
	} 

	@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    	@Override
    	public void saveNewCustormerDetails(CustomerDTO customerDTO) {
		try{

			CustomerEntity customerEntity = new CustomerEntity();
			BeanUtils.copyProperties(customerDTO, customerEntity);	
			customerRepository.save(customerEntity);
			
		}catch(Exception e){
			
			LOGGER.error("Method saveNewCustormerDetails : " + e.getMessage(), e);
			storeFailCustomerSaveDetails(customerDTO, e);
                        throw e;	
		
		}
	}

	private void storeFailCustomerSaveDetails(CustomerDTO customerDTO, Exception ex){
		try{
				CustomerSaveFailsEntity customerSaveFailsEntity = new CustomerSaveFailsEntity(customerDTO.getContact(), e.getMessage()); 
				customerSaveFailsRepository.save(customerSaveFailsEntity);

			}catch(Exception e){
			
			LOGGER.error("Method storeFailCustomerSaveDetails : " + e.getMessage() ,e);
		
		}
	}
}


huangapple
  • 本文由 发表于 2020年4月8日 14:11:06
  • 转载请务必保留本文链接:https://java.coder-hub.com/61094308.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定