Use the writeconcern. Safe parameter of MongoDB in proper places

Source: Internet
Author: User

First, list the level parameters of WriteConcern that throw exceptions:

  • WriteConcern. NONE: No exception is thrown.
  • WriteConcern. NORMAL: only throw network error exception, no server error exception
  • WriteConcern. SAFE: throws a network error or server error exception, and waits for the server to complete the write operation.
  • WriteConcern. MAJORITY: throws a network error or server error exception, and waits for a master server to complete the write operation.
  • WriteConcern. FSYNC_SAFE: throw a network error or server error exception. write operation waits for the server to refresh the data to the disk.
  • WriteConcern. JOURNAL_SAFE: throw a network error or server error exception. write operation waits for the log file submitted to the disk by the server.
  • WriteConcern. REPLICAS_SAFE: throw a network error or server error exception. Wait for at least two servers to complete the write operation.

 

When we perform the following operations (Set "age" with "name" as "lily" to 20 ):

db.update({"name":"lily"},{"$set":{"age":20}})

By default, this operation uses WriteConcern. NORMAL (an exception is thrown only when a network error occurs), which is equivalent:

db.update({"name":"lily"},{"$set":{"age":20}},WriteConcern.NORMAL)

Using NORMAL mode parameters can greatly improve the write efficiency. However, if a server error occurs at this time, no error is returned to the client, and the client mistakenly believes that the operation is successful.

Therefore, the WriteConcern. SAFE mode must be used in many important write operations to ensure that this error can be detected and that the client and server have consistent awareness of the correctness of an operation.

(According to the author's test, if the server loses power, the client will still not receive the operation error returned at that time, please pay special attention)

In addition, in many cases, we need to know whether the write operation is successful (or how many objects are affected by this update operation). At this time, we need:

WriteResult ret = db. update ({"name": "lily" },{ "$ set": {"age": 20 }});
If (ret. getN ()> 0) // number of objects affected by the operation
Return true;
Else
Return false;

Or:

WriteResult ret = db.update({"name":"lily"},{"$set":{"age":20}});
if(ret.getLastError() == null)
return true;
else
return false;

In this case, getLastError () checks whether the result of the last operation has an error.

Further steps

GetLastError () needs to obtain the connection from the connection pool again because mongodb uses the connection pool, which slows down the process. You can do this:

db.requestStart();
WriteResult ret = db.update({"name":"lily"},{"$set":{"age":20}});
if(ret.getLastError() == null)
return true;
else
return false;
db.requestDone();

This ensures that the update operation and getLastError () use the same connection, and reduces the memory/fetch connection process.

 

There is another way

You can also use the WriteConcern. SAFE parameter:

WriteResult ret = db.update({"name":"lily"},{"$set":{"age":20}}, WriteConcern.SAFE);
if(ret.getLastError() == null)
return true;
else
return false;

// is equivalent to
db.requestStart();
WriteResult ret = db.update({"name":"lily"},{"$set":{"age":20}});
if(ret.getLastError() == null)
return true;
else
return false;
db.requestDone();

This is also the recommended method, so that you can efficiently get the returned results and detect server errors.

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.