6. The Advanced Stuff
6.6. Write Queries Flags
6.5. Data Provider Object
« Previous
6.7. Document Partial Updates
Next »

6.6. Write Queries Flags

AuthorDariusz Górecki

Quote from PHP Manual:

"safe"

Can be a boolean or integer, defaults to FALSE. If FALSE, the program continues executing without waiting for a database response. If TRUE, the program will wait for the database response and throw a MongoCursorException if the write operation did not succeed.

If you are using replication and the master has changed, using "safe" will make the driver disconnect from the master, throw and exception, and attempt to find a new master on the next operation (your application must decide whether or not to retry the operation on the new master).

If you do not use "safe" with a replica set and the master changes, there will be no way for the driver to know about the change so it will continuously and silently fail to write.

If safe is an integer, will replicate the insert to that many machines before returning success (or throw an exception if the replication times out, see wtimeout). This overrides the w variable set on the collection.

"fsync"

Boolean, defaults to FALSE. Forces the insert to be synced to disk before returning success. If TRUE, a safe insert is implied and will override setting safe to FALSE.

We now can set value for write queries flags FSync and/or Safe on a different scopes:

State of write queries flags is connected

Setting FSync to TRUE, will set Safe flag to TRUE automatically

Now, we may want all DB operations to be synced to disk, and we set global FSync flag in MongoDB component config, but we feagure out that, for ie. massive temporary models like DB logging objects are not important, and for performance reasons, we whant to disable FSync for this models only.

We may do so: LogModelClass::model()->setFsyncFlag(false);

From now on, all LogModelClass objects will be written to DB without waiting to filesystem sync.

For user comfort, FSync and Safe flags may be set just for single model instance by, $singleModelInstance->setFsyncFlag(false), this will not affect any other model instance.

6.6. Write Queries Flags
6. The Advanced Stuff
« Previous
6.5. Data Provider Object
Next »
6.7. Document Partial Updates