0

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 .

ashutosh
  • 81
  • 1
  • 1
  • 9

1 Answers1

0

If you want to ensure atomicity between 'user' and 'school' collections, then there is no way to ensure that in MongoDB, as it does not support transactions. You need to rethink of your mongo collections design and embed school object inside your user object, as MongoDB ensures atomicity at document level. Something like this:

{"userName":"xyz@abc.com","school":{"name":"xyz","city":"ny"}}

Or MongoDB offers "transaction like" semantics using two phase commits: https://docs.mongodb.com/manual/tutorial/perform-two-phase-commits/

Uma Madhavi
  • 4,851
  • 5
  • 38
  • 73
adi
  • 304
  • 2
  • 11
  • i can not embed the school object because user have different roles .user can be a teacher or student i was thinking about changing the signup page so at first i only get user collection related data and after signup depending on the roles get other data is it right approach to do this ? – ashutosh Apr 11 '17 at 17:30