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 acknowledgment of write operations with various options.

w

wtimeout how long to wait for slaves before failing

Other options:

See Also:
Serialized Form
MongoDB Doc Links

Nested Class Summary
static class WriteConcern.Majority
           
 
Field Summary
static WriteConcern ACKNOWLEDGED
          Write operations that use this write concern will wait for acknowledgement from the primary server before returning.
static WriteConcern ERRORS_IGNORED
          No exceptions are raised, even for network issues.
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 FSYNCED
          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 JOURNALED
          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
          Write operations that use this write concern will return as soon as the message is written to the socket.
static WriteConcern REPLICA_ACKNOWLEDGED
          Exceptions are raised for network issues, and server errors; waits for at least 2 servers for the write operation.
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
          Write operations that use this write concern will wait for acknowledgement from the primary server before returning.
static WriteConcern UNACKNOWLEDGED
          Write operations that use this write concern will return as soon as the message is written to the socket.
 
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()
          Gets the getlasterror command for this write concern.
 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

ERRORS_IGNORED

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


ACKNOWLEDGED

public static final WriteConcern ACKNOWLEDGED
Write operations that use this write concern will wait for acknowledgement from the primary server before returning. Exceptions are raised for network issues, and server errors.

Since:
2.10.0

UNACKNOWLEDGED

public static final WriteConcern UNACKNOWLEDGED
Write operations that use this write concern will return as soon as the message is written to the socket. Exceptions are raised for network issues, but not server errors.

Since:
2.10.0

FSYNCED

public static final WriteConcern FSYNCED
Exceptions are raised for network issues, and server errors; the write operation waits for the server to flush the data to disk.


JOURNALED

public static final WriteConcern JOURNALED
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.


REPLICA_ACKNOWLEDGED

public static final WriteConcern REPLICA_ACKNOWLEDGED
Exceptions are raised for network issues, and server errors; waits for at least 2 servers for the write operation.


NONE

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

This field has been superseded by WriteConcern.ERRORS_IGNORED, and may be deprecated in a future release.

See Also:
ERRORS_IGNORED

NORMAL

public static final WriteConcern NORMAL
Write operations that use this write concern will return as soon as the message is written to the socket. Exceptions are raised for network issues, but not server errors.

This field has been superseded by WriteConcern.UNACKNOWLEDGED, and may be deprecated in a future release.

See Also:
UNACKNOWLEDGED

SAFE

public static final WriteConcern SAFE
Write operations that use this write concern will wait for acknowledgement from the primary server before returning. Exceptions are raised for network issues, and server errors.

This field has been superseded by WriteConcern.ACKNOWLEDGED, and may be deprecated in a future release.

See Also:
ACKNOWLEDGED

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.

This field has been superseded by WriteConcern.FSYNCED, and may be deprecated in a future release.

See Also:
FSYNCED

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.

This field has been superseded by WriteConcern.JOURNALED, and may be deprecated in a future release.

See Also:
JOURNALED

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.

This field has been superseded by WriteConcern.REPLICA_ACKNOWLEDGED, and may be deprecated in a future release.

See Also:
REPLICA_ACKNOWLEDGED
Constructor Detail

WriteConcern

public WriteConcern()
Default constructor keeping all options as default. Be careful using this constructor, as it's equivalent to WriteConcern.UNACKNOWLEDGED, so writes may be lost without any errors being reported.

See Also:
UNACKNOWLEDGED

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()
Gets the getlasterror command for this write concern.

Returns:
getlasterror command, even if w <= 0

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:
w as a string

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