Created
October 30, 2017 02:11
-
-
Save Jandaes/98287e145f4756292eff759a2de9966f to your computer and use it in GitHub Desktop.
Spring事务回滚代码片段 #Transactional #Spring
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 遇到异常回滚事务 | |
* @param person | |
* @return | |
*/ | |
@Override | |
@Transactional(rollbackFor = {IllegalArgumentException.class}) | |
public Person savePersonWithRollBack(Person person) { | |
Person p=personRepository.save(person); | |
if(person.getName().equals("73")){ | |
throw new IllegalArgumentException("73已存在,。回滚吧,皮卡丘.."); | |
} | |
return p; | |
} | |
/** | |
* 遇到异常不回滚事物 | |
* @param person | |
* @return | |
*/ | |
@Override | |
@Transactional(noRollbackFor = {IllegalArgumentException.class}) | |
public Person savePersonWithoutRollBack(Person person) { | |
Person p=personRepository.save(person); | |
if(person.getName().equals("73")){ | |
throw new IllegalArgumentException("73已存在,。然而并没有什么暖用,不回滚.."); | |
} | |
return p; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment