public class

WriteConcern

extends Object
implements Serializable
java.lang.Object
   ↳ com.mongodb.WriteConcern
Known Direct Subclasses

Class Overview

WriteConcern control the write behavior for with various options, as well as exception raising on error conditions.

w

  • -1 = don't even report network errors
  • 0 = default, don't call getLastError by default
  • 1 = basic, call getLastError, but don't wait for slaves
  • 2+= wait for slaves
wtimeout how long to wait for slaves before failing
  • 0 = indefinite
  • > 0 = ms to wait

fsync force fsync to disk

Summary

Nested Classes
class WriteConcern.Majority  
Fields
public static final WriteConcern FSYNC_SAFE Exceptions are raised for network issues, and server errors; the write operation waits for the server to flush the data to disk
public static final WriteConcern JOURNAL_SAFE Exceptions are raised for network issues, and server errors; the write operation waits for the server to group commit to the journal file on disk
public static final WriteConcern MAJORITY Exceptions are raised for network issues, and server errors; waits on a majority of servers for the write operation
public static final WriteConcern NONE No exceptions are raised, even for network issues
public static final WriteConcern NORMAL Exceptions are raised for network issues, but not server errors
public static final WriteConcern REPLICAS_SAFE Exceptions are raised for network issues, and server errors; waits for at least 2 servers for the write operation
public static final WriteConcern SAFE Exceptions are raised for network issues, and server errors; waits on a server for the write operation
Public Constructors
WriteConcern()
Default constructor keeping all options as default
WriteConcern(int w)
Calls WriteConcern(int, int, boolean) with wtimeout=0 and fsync=false
WriteConcern(String w)
Tag based Write Concern with wtimeout=0, fsync=false, and j=false
WriteConcern(int w, int wtimeout)
Calls WriteConcern(int, int, boolean) with fsync=false
WriteConcern(boolean fsync)
Calls WriteConcern(int, int, boolean) with w=1 and wtimeout=0
WriteConcern(int w, int wtimeout, boolean fsync)
Creates a WriteConcern object.
WriteConcern(int w, int wtimeout, boolean fsync, boolean j)
Creates a WriteConcern object.
WriteConcern(int w, int wtimeout, boolean fsync, boolean j, boolean continueOnInsertError)
Creates a WriteConcern object.
WriteConcern(String w, int wtimeout, boolean fsync, boolean j)
Creates a WriteConcern object.
WriteConcern(String w, int wtimeout, boolean fsync, boolean j, boolean continueOnInsertError)
Creates a WriteConcern object.
Public Methods
boolean callGetLastError()
Returns whether "getlasterror" should be called (w > 0)
WriteConcern continueOnErrorForInsert(boolean continueOnErrorForInsert)
Toggles the "continue inserts on error" mode.
boolean equals(Object o)
boolean fsync()
Gets the fsync flag (fsync to disk on the server)
BasicDBObject getCommand()
boolean getContinueOnErrorForInsert()
Gets the "continue inserts on error" mode
boolean getFsync()
Gets the fsync flag (fsync to disk on the server)
boolean getJ()
Gets the j parameter (journal syncing)
int getW()
Gets the w parameter (the write strategy)
Object getWObject()
Gets the w value (the write strategy)
String getWString()
Gets the w parameter (the write strategy) in String format
int getWtimeout()
Gets the write timeout (in milliseconds)
int hashCode()
static WriteConcern.Majority majorityWriteConcern(int wtimeout, boolean fsync, boolean j)
Create a Majority Write Concern that requires a majority of servers to acknowledge the write.
boolean raiseNetworkErrors()
Returns whether network error may be raised (w >= 0)
void setWObject(Object w)
Sets the w value (the write strategy)
String toString()
static WriteConcern valueOf(String name)
Gets the WriteConcern constants by name: NONE, NORMAL, SAFE, FSYNC_SAFE, REPLICA_SAFE.
[Expand]
Inherited Methods
From class java.lang.Object

Fields

public static final WriteConcern FSYNC_SAFE

Exceptions are raised for network issues, and server errors; the write operation waits for the server to flush the data to disk

public static final WriteConcern JOURNAL_SAFE

Exceptions are raised for network issues, and server errors; the write operation waits for the server to group commit to the journal file on disk

public static final WriteConcern MAJORITY

Exceptions are raised for network issues, and server errors; waits on a majority of servers for the write operation

public static final WriteConcern NONE

No exceptions are raised, even for network issues

public static final WriteConcern NORMAL

Exceptions are raised for network issues, but not server errors

public static final WriteConcern REPLICAS_SAFE

Exceptions are raised for network issues, and server errors; waits for at least 2 servers for the write operation

public static final WriteConcern SAFE

Exceptions are raised for network issues, and server errors; waits on a server for the write operation

Public Constructors

public WriteConcern ()

Default constructor keeping all options as default

public WriteConcern (int w)

Calls WriteConcern(int, int, boolean) with wtimeout=0 and fsync=false

Parameters
w number of writes

public WriteConcern (String w)

Tag based Write Concern with wtimeout=0, fsync=false, and j=false

Parameters
w Write Concern tag

public WriteConcern (int w, int wtimeout)

Calls WriteConcern(int, int, boolean) with fsync=false

Parameters
w number of writes
wtimeout timeout for write operation

public WriteConcern (boolean fsync)

Calls WriteConcern(int, int, boolean) with w=1 and wtimeout=0

Parameters
fsync whether or not to fsync

public WriteConcern (int w, int wtimeout, boolean fsync)

Creates a WriteConcern object.

Specifies the number of servers to wait for on the write operation, and exception raising behavior

w represents the number of servers:

  • w=-1 None, no checking is done
  • w=0 None, network socket errors raised
  • w=1 Checks server for errors as well as network socket errors raised
  • w>1 Checks servers (w) for errors as well as network socket errors raised

Parameters
w number of writes
wtimeout timeout for write operation
fsync whether or not to fsync

public WriteConcern (int w, int wtimeout, boolean fsync, boolean j)

Creates a WriteConcern object.

Specifies the number of servers to wait for on the write operation, and exception raising behavior

w represents the number of servers:

  • w=-1 None, no checking is done
  • w=0 None, network socket errors raised
  • w=1 Checks server for errors as well as network socket errors raised
  • w>1 Checks servers (w) for errors as well as network socket errors raised

Parameters
w number of writes
wtimeout timeout for write operation
fsync whether or not to fsync
j whether writes should wait for a journaling group commit

public WriteConcern (int w, int wtimeout, boolean fsync, boolean j, boolean continueOnInsertError)

Creates a WriteConcern object.

Specifies the number of servers to wait for on the write operation, and exception raising behavior

w represents the number of servers:

  • w=-1 None, no checking is done
  • w=0 None, network socket errors raised
  • w=1 Checks server for errors as well as network socket errors raised
  • w>1 Checks servers (w) for errors as well as network socket errors raised

Parameters
w number of writes
wtimeout timeout for write operation
fsync whether or not to fsync
j whether writes should wait for a journaling group commit
continueOnInsertError if batch inserts should continue after the first error

public WriteConcern (String w, int wtimeout, boolean fsync, boolean j)

Creates a WriteConcern object.

Specifies the number of servers to wait for on the write operation, and exception raising behavior

w represents the number of servers:

  • w=-1 None, no checking is done
  • w=0 None, network socket errors raised
  • w=1 Checks server for errors as well as network socket errors raised
  • w>1 Checks servers (w) for errors as well as network socket errors raised

Parameters
w number of writes
wtimeout timeout for write operation
fsync whether or not to fsync
j whether writes should wait for a journaling group commit

public WriteConcern (String w, int wtimeout, boolean fsync, boolean j, boolean continueOnInsertError)

Creates a WriteConcern object.

Specifies the number of servers to wait for on the write operation, and exception raising behavior

w represents the number of servers:

  • w=-1 None, no checking is done
  • w=0 None, network socket errors raised
  • w=1 Checks server for errors as well as network socket errors raised
  • w>1 Checks servers (w) for errors as well as network socket errors raised

Parameters
w number of writes
wtimeout timeout for write operation
fsync whether or not to fsync
j whether writes should wait for a journaling group commit
continueOnInsertError if batch inserts should continue after the first error

Public Methods

public boolean callGetLastError ()

Returns whether "getlasterror" should be called (w > 0)

public WriteConcern continueOnErrorForInsert (boolean continueOnErrorForInsert)

Toggles the "continue inserts on error" mode. This only applies to server side errors. If there is a document which does not validate in the client, an exception will still be thrown in the client. This will return a *NEW INSTANCE* of WriteConcern with your preferred continueOnInsert value

public boolean equals (Object o)

public boolean fsync ()

Gets the fsync flag (fsync to disk on the server)

public BasicDBObject getCommand ()

public boolean getContinueOnErrorForInsert ()

Gets the "continue inserts on error" mode

public boolean getFsync ()

Gets the fsync flag (fsync to disk on the server)

public boolean getJ ()

Gets the j parameter (journal syncing)

public int getW ()

Gets the w parameter (the write strategy)

public Object getWObject ()

Gets the w value (the write strategy)

public String getWString ()

Gets the w parameter (the write strategy) in String format

public int getWtimeout ()

Gets the write timeout (in milliseconds)

public int hashCode ()

public static WriteConcern.Majority majorityWriteConcern (int wtimeout, boolean fsync, boolean j)

Create a Majority Write Concern that requires a majority of servers to acknowledge the write.

Parameters
wtimeout timeout for write operation
fsync whether or not to fsync
j whether writes should wait for a journaling group commit

public boolean raiseNetworkErrors ()

Returns whether network error may be raised (w >= 0)

public void setWObject (Object w)

Sets the w value (the write strategy)

public String toString ()

public static WriteConcern valueOf (String name)

Gets the WriteConcern constants by name: NONE, NORMAL, SAFE, FSYNC_SAFE, REPLICA_SAFE. (matching is done case insensitively)