Quantcast
Channel: Active questions tagged propagation - Stack Overflow
Viewing all articles
Browse latest Browse all 52

RequiresNew transaction after rolling back still affect entity

$
0
0

Context: spring boot 2.7.0 , @EnableTransactionManagement at main application class

Assumption: the unchecked exception should roll back the internal transaction, but since it is caught, it will not affect the external transaction.

Solution used:

@Serviceclass ExternalService {  final InnerService innerService;  @Transactional(propagation = Propagation.REQUIRED)  public User requiredRequired(User user) {    user = userRepository.save(user);    try {      innerService.doRequired(user);    } catch (RuntimeException e) {     // error handling    }    return user;  }}@Servicepublic class InnerService {  @Transactional(propagation = Propagation.REQUIRES_NEW)  public void doRequiresNew(User user) {    affectUser(user);    // if condition then:    throw new RuntimeException("Rollback this 'doRequiresNew' transaction!");    }    private void affectUser(User user) {    // change something in User .ex:    user.setFirstName(user.getFirstName() +"_affected!");  }}

Input:User(firstName=Lolek, lastName=Bolek)

Result in database:User(id=1, createdAt=2023-11-05T17:03:23.324309Z, firstName=Lolek_affected!, lastName=Bolek)

Question: why results of the new transaction are still persisted?

PS. It works only if method of InnerService has @Transactional (propagation = Propagation.NESTED) annotation


Viewing all articles
Browse latest Browse all 52

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>