i my application when user try to signup the data he enter from form is saved into two different document
public Result schoolSignUp(FormSchoolSignUp signUpForm){
User userEntered=null;
if(signUpForm.getEmail()!=null){
User user=this.userService.getUser(signUpForm.getEmail());
// user null means there is no user in data base
if(user==null){
List<String> roles=new ArrayList<>();
roles.add("ROLE_SCHOOL");
// data is assigned to user
this.user.setUserName(signUpForm.getEmail());
this.user.setPassword(signUpForm.getPassword());
this.user.setRoles(roles);
//user collection data is stored in the data base
userEntered=this.userService.saveUser(this.user); // first
write operation
}
else{
this.result.setResult(false);
this.result.setMessage("User Already Exist");
}
}
else{
this.result.setResult(false);
this.result.setMessage("User Name is not entered");
}
if(userEntered!=null){
// data is assigned to school
this.school.setName(signUpForm.getName());
this.school.setUserId(signUpForm.getEmail());
this.school.setUserId(userEntered.getUserName());
this.school.setAddress(signUpForm.getAddress());
this.school.setState(signUpForm.getState());
this.school.setCity(signUpForm.getCity());
//school collection is stored in the data base
this.schoolRepository.insert(this.school);//second write
operation
this.result.setResult(true);
this.result.setMessage("Success");
}
return this.result;
}
my problem is if something went wrong between first write and the second write it is possible to have data entered in the first document and the second document is empty so is this situation will considered as transaction if so how should i avoid i am thinking about changing the signup process or should i consider some other option like two phase commit .