com.mongodb
Class WriteConcern

java.lang.Object
  extended by com.mongodb.WriteConcern
Direct Known Subclasses:
WriteConcern.Majority

public class WriteConcern
extends Object

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

w

wtimeout how long to wait for slaves before failing

fsync force fsync to disk


MongoDB Doc Links

Nested Class Summary
static class WriteConcern.Majority
           
 
Field Summary
static 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
static 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
static WriteConcern MAJORITY
          Exceptions are raised for network issues, and server errors; waits on a majority of servers for the write operation
static WriteConcern NONE
          No exceptions are raised, even for network issues
static WriteConcern NORMAL
          Exceptions are raised for network issues, but not server errors
static WriteConcern REPLICAS_SAFE
          Exceptions are raised for network issues, and server errors; waits for at least 2 servers for the write operation
static WriteConcern SAFE
          Exceptions are raised for network issues, and server errors; waits on a server for the write operation
 
Constructor Summary
WriteConcern()
          Default constructor keeping all options as default
WriteConcern(boolean fsync)
          Calls WriteConcern(int, int, boolean) with w=1 and wtimeout=0
WriteConcern(int w)
          Calls WriteConcern(int, int, boolean) with wtimeout=0 and fsync=false
WriteConcern(int w, int wtimeout)
          Calls WriteConcern(int, int, boolean) with fsync=false
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 continueInsertOnError)
          Creates a WriteConcern object.
WriteConcern(String w)
          Tag based Write Concern with wtimeout=0, fsync=false, and j=false
WriteConcern(String w, boolean j)
          Tag based Write Concern with configgable j and wtimeout=0, fsync=false
WriteConcern(String w, int wtimeout, boolean fsync, boolean j)
          Creates a WriteConcern object.
WriteConcern(String w, int wtimeout, boolean fsync, boolean j, Boolean continueInsertOnError)
          Creates a WriteConcern object.
 
Method Summary
protected  WriteConcern _instance(Object wValue, int wtimeout, boolean fsync, boolean j, boolean cont)
           
 boolean callGetLastError()
          Returns whether "getlasterror" should be called (w > 0)
 boolean continueOnErrorForInsert()
          Returns whether (batch) inserts will continue if an error occurs before the end
 boolean equals(Object o)
           
 boolean fsync()
          Returns whether writes wait for files to be synced to disk
 BasicDBObject getCommand()
          Gets the object representing the "getlasterror" command
 int getW()
          Gets the number of servers to write to if W is not a string value, returns -999.
 String getWString()
           
 int getWtimeout()
          Gets the write timeout (in milliseconds)
 Object getWValue()
          Gets the w value (either int or string)
 boolean j()
          Returns whether writes will await a group commit to the journal.
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)
 String toString()
           
static WriteConcern valueOf(String name)
          Gets the WriteConcern constants by name: NONE, NORMAL, SAFE, FSYNC_SAFE, REPLICA_SAFE.
 WriteConcern withContinueOnErrorForInsert(boolean cont)
          Clones this WriteConcern with a new boolean mode for "continue inserts on error" (WriteConcerns are immutable)
 WriteConcern withFsync(boolean fsync)
          Clones this WriteConcern with a new boolean mode for fsync (WriteConcerns are immutable)
 WriteConcern withJ(boolean j)
          Clones this WriteConcern with a new "j" (WriteConcerns are immutable)
 WriteConcern withW(int w)
          Clones this WriteConcern with a new int mode for "w" (WriteConcerns are immutable)
 WriteConcern withW(String mode)
          Clones this WriteConcern with a new String mode for "w" (WriteConcerns are immutable)
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

NONE

public static final WriteConcern NONE
No exceptions are raised, even for network issues


NORMAL

public static final WriteConcern NORMAL
Exceptions are raised for network issues, but not server errors


SAFE

public static final WriteConcern SAFE
Exceptions are raised for network issues, and server errors; waits on a server for the write operation


MAJORITY

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


FSYNC_SAFE

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


JOURNAL_SAFE

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


REPLICAS_SAFE

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

Constructor Detail

WriteConcern

public WriteConcern()
Default constructor keeping all options as default


WriteConcern

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

Parameters:
w - number of writes

WriteConcern

public WriteConcern(String w)
Tag based Write Concern with wtimeout=0, fsync=false, and j=false

Parameters:
w - Write Concern tag

WriteConcern

public WriteConcern(String w,
                    boolean j)
Tag based Write Concern with configgable j and wtimeout=0, fsync=false

Parameters:
w - Write Concern Tag
j - whether writes should wait for a journaling group commit

WriteConcern

public WriteConcern(int w,
                    int wtimeout)
Calls WriteConcern(int, int, boolean) with fsync=false

Parameters:
w - number of writes
wtimeout - timeout for write operation

WriteConcern

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

Parameters:
fsync - whether or not to fsync

WriteConcern

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:

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

WriteConcern

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:

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

WriteConcern

public WriteConcern(int w,
                    int wtimeout,
                    boolean fsync,
                    boolean j,
                    boolean continueInsertOnError)
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:

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

WriteConcern

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:

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

WriteConcern

public WriteConcern(String w,
                    int wtimeout,
                    boolean fsync,
                    boolean j,
                    Boolean continueInsertOnError)
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:

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
Method Detail

getCommand

public BasicDBObject getCommand()
Gets the object representing the "getlasterror" command

Returns:

getW

public int getW()
Gets the number of servers to write to if W is not a string value, returns -999. You should migrate to using getWValue (returns Object) or getWString (String)

Returns:

getWString

public String getWString()

getWtimeout

public int getWtimeout()
Gets the write timeout (in milliseconds)

Returns:

fsync

public boolean fsync()
Returns whether writes wait for files to be synced to disk

Returns:

j

public boolean j()
Returns whether writes will await a group commit to the journal.

Returns:
boolean

continueOnErrorForInsert

public boolean continueOnErrorForInsert()
Returns whether (batch) inserts will continue if an error occurs before the end

Returns:
boolean

raiseNetworkErrors

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

Returns:

callGetLastError

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

Returns:

valueOf

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

Parameters:
name -
Returns:

toString

public String toString()
Overrides:
toString in class Object

equals

public boolean equals(Object o)
Overrides:
equals in class Object

getWValue

public Object getWValue()
Gets the w value (either int or string)

Returns:

withW

public WriteConcern withW(String mode)
Clones this WriteConcern with a new String mode for "w" (WriteConcerns are immutable)

Parameters:
mode -
Returns:

withW

public WriteConcern withW(int w)
Clones this WriteConcern with a new int mode for "w" (WriteConcerns are immutable)

Parameters:
mode -
w -
Returns:

withJ

public WriteConcern withJ(boolean j)
Clones this WriteConcern with a new "j" (WriteConcerns are immutable)

Parameters:
j -
Returns:

withContinueOnErrorForInsert

public WriteConcern withContinueOnErrorForInsert(boolean cont)
Clones this WriteConcern with a new boolean mode for "continue inserts on error" (WriteConcerns are immutable)

Parameters:
cont -
Returns:

withFsync

public WriteConcern withFsync(boolean fsync)
Clones this WriteConcern with a new boolean mode for fsync (WriteConcerns are immutable)

Parameters:
fsync -
Returns:

_instance

protected WriteConcern _instance(Object wValue,
                                 int wtimeout,
                                 boolean fsync,
                                 boolean j,
                                 boolean cont)

majorityWriteConcern

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