2017-04-11 4 views
0

私私のアプリケーション、ユーザー、彼は、フォームから入力したデータをサインアップしようとは、2つの異なるドキュメントに保存されたときに何かが最初の書き込み及び第2の書き込みの間で間違っていた場合これはmongodbのトランザクションと見なされますか?

 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; 


} 

私の問題は、入力されたデータを持つことが可能です最初の文書と2番目の文書は空ですので、この状況はトランザクションと見なされます。どうすれば私は申し込みプロセスの変更について考えているのですか、2フェーズコミットのような他のオプションを検討する必要があります。

答えて

0

'user'コレクションと 'school'コレクションの間に原子性を確保したい場合は、MongoDBでトランザクションをサポートしていないので、確実にする方法はありません。 MongoDBはドキュメントレベルでのアトミック性を保証するので、あなたのモンゴーコレクションデザインを再考し、スクールオブジェクトをユーザーオブジェクト内に埋め込む必要があります。このような何か:

{"userName":"[email protected]","school":{"name":"xyz","city":"ny"}} 

それともMongoDBの2つのフェーズを使用して意味論「のような取引」を提供していますがコミット: https://docs.mongodb.com/manual/tutorial/perform-two-phase-commits/

+0

ユーザーは、教師や生徒のIすることができ.user異なる役割を持っているので、私は学校のオブジェクトを埋め込むことはできませんサインアップページを変更することを考えていたので、最初はユーザーコレクション関連のデータしか取得できませんでした。そして、サインアップ後に、他のデータを取得するのが正しいアプローチですか? – ashutosh

関連する問題