I have the following two method to store the single user and multiple users in mongodb using Spring data JPA's MongoRepository. If any exception happened in the method it is not rollbacking. I can able to find the data in database.
@Override
@Transactional(propagation = Propagation.REQUIRED)
public UserRequest save(UserRequest userRequest) throws UserException {
logger.info("Saving the user information {} ", userRequest);
User user = new User(sequenceService.getNextSequenceFor(SequenceNames.USER));
if("admin".equals(userRequest.getFirstName())) {
throw new UserException("Resvered name canot be used : " + userRequest.getFirstName());
}
BeanUtils.copyProperties(userRequest, user,"id");
user = userRepository.save(user);
userRequest.setId(user.getId());
return userRequest;
}
@Override
@Transactional
public List<UserRequest> save(List<UserRequest> users) throws UserException {
for (UserRequest userRequest : users) {
save(userRequest);
}
return users;
}
@Repository
public interface UserRepository extends MongoRepository<User, String> {
}
User Request :
[
{
"firstName": "John",
"lastName": "A",
"mobileNo": "9898989892",
"emailId": "john.a@gmail.com"
},
{
"firstName": "admin",
"lastName": "A",
"mobileNo": "9898989892",
"emailId": "admin.a@gmail.com"
}
]
This code saves the John's data in database. It is rollback only for the user admin. It doesn't rollback for all the user data. Partial roolback is not happening.