当前位置:网站首页>[difficult and miscellaneous diseases] @transitional failure summary

[difficult and miscellaneous diseases] @transitional failure summary

2022-06-26 10:41:00 My name is 985

In daily development , I met a very difficult problem ! The transaction does not take effect ! The pseudocode is shown below

public interface PersonService{
    
	public batchAddPerson();
}

@Service("personService")
puiblic class PersonServiceImpl implements PersonService {
    
	
	@Transational // If comments are normal here 
	@Override
	public batchAddPerson(List<PersonBo> personList){
    
		for(PersonBo person : personList){
    
			this.addPerson(person );
		}
	}
	
	// If @Transational Note: it is found that no transaction is opened here , So there is no rollback ;
	// If so batchAddPerson and addPerson If you add business , that addPerson There are open transactions 
	@Transational 
	public void addPerson(){
    
		dao.addPerson();
		throw new RuntimeException();
	}
}

By looking up relevant information , Summarize the following situations that can lead to @Transational invalid

1. In the same class , One nan-transactional To call transactional Methods , The transaction will fail

@Transational Separate notes in addPerson The reason for the invalid method is :addPerson Be being batchAddPerson Called , and batchAddPerson There is no transaction open .
@Transational Separate notes in batchAddPerson The reason for the entry into force is :batchAddPerson Started the business , and batchAddPerson Called addPerson, The default transaction propagation behavior is if the sub method does not start the transaction , Create a new transaction for the sub method
@Transational Annotations in batchAddPerson、addPerson The reason why the two methods work : They all started the business .( doubt : But I don't know addPerson Will there be a new transaction )

[email protected] By default, only those deriving from RuntimeException perhaps Error It's abnormal

@Transational By default, only those deriving from RuntimeException perhaps Error It's abnormal , Only when these exceptions are found will the rollback be performed . If the business throws out Exception The latter custom exception , Need to pass through @Transational Of rollbackFor Property must be specified to take effect

3. stay private Label on method transactional, Invalid transaction

@Transational Can only be marked on public On

Reference blog :https://www.cnblogs.com/milton/p/6046699.html

原网站

版权声明
本文为[My name is 985]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202170529365591.html