com.mongodb
Class WriteConcern

java.lang.Object
  extended by com.mongodb.WriteConcern
All Implemented Interfaces:
Serializable
Direct Known Subclasses:
WriteConcern.Majority

public class WriteConcern
extends Object
implements Serializable

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

See Also:
Serialized Form
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 continueOnInsertError)
          Creates a WriteConcern object.
WriteConcern(String w)
          Tag based Write Concern with wtimeout=0, fsync=false, and j=false
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.
 
Method Summary
 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.
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, 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(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 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:

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

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 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:

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

getCommand

public BasicDBObject getCommand()

setWObject

public void setWObject(Object w)
Sets the w value (the write strategy)

Parameters:
w -

getWObject

public Object getWObject()
Gets the w value (the write strategy)

Returns:

getW

public int getW()
Gets the w parameter (the write strategy)

Returns:

getWString

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

Returns:

getWtimeout

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

Returns:

getFsync

public boolean getFsync()
Gets the fsync flag (fsync to disk on the server)

Returns:

fsync

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

Returns:

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

hashCode

public int hashCode()
Overrides:
hashCode in class Object

getJ

public boolean getJ()
Gets the j parameter (journal syncing)

Returns:

continueOnErrorForInsert

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

Parameters:
continueOnErrorForInsert -

getContinueOnErrorForInsert

public boolean getContinueOnErrorForInsert()
Gets the "continue inserts on error" mode

Returns:

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