MongoDB update failure Solution

Source: Internet
Author: User
Tags mongodb update

Symptom:

Writeresult res = your template. updatefirst (query, updateobj, "servertoagentreq_sms ");

When obtaining the res. getn () return value, it is found that the return value is 0 occasionally, indicating that the update operation has not updated any data. In addition, the probability of failure is greatly increased if multiple threads are concurrently updated.

The official website says the update operation is not guaranteed to be successful ....

Solution:

After one failure, another thread tries multiple times.

 

  Private Threadpoolexecutor exec = New Threadpoolexecutor (2, 10, 3, timeunit. Seconds, New Arrayblockingqueue <runnable> (10 ), New  Threadpoolexecutor. callerrunspolicy (); mainCode: Writeresult res = Jsontemplate. updatefirst (query, updateobj, "servertoagentreq_sms");  //  If the update fails, enter another thread to retry the update.              If (Res. getn () = 0 ) {Exec.exe cute (  New  Runnable () {@ override  Public   Void  Run (){  Int Num = 0 ; Writeresult res = Jsontemplate. updatefirst (query, updateobj, "servertoagentreq_sms");  While (Res. getn () = 0 & num <100 ) {Res = Jsontemplate. updatefirst (query, updateobj, "servertoagentreq_sms" ); Num ++ ;  Try  {Thread. Sleep ( 300 );}  Catch  (Interruptedexception e) {logger. Error ( "Failed to respond to update {}", E );}}  If (Res. getn () = 0 ) {Logger. Error ( "An error occurred while responding to the update !!! Gwmsgid :{}, masmsgid :{}" , Gwmsgid, masmsgid );}}}); 

The failure rate is greatly reduced.

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.