public class WriteConcern extends Object implements Serializable
Controls the acknowledgment of write operations with various options.
w
wtimeout
- how long to wait for slaves before failing
Other options:
j
: If true block until write operations have been committed to the journal. Cannot be used in combination with
fsync
. Prior to MongoDB 2.6 this option was ignored if the server was running without journaling. Starting with MongoDB 2.6
write operations will fail with an exception if this option is used when the server is running without journaling.fsync
: If true and the server is running without journaling, blocks until the server has synced all data files to disk.
If the server is running with journaling, this acts the same as the j
option, blocking until write operations have been
committed to the journal. Cannot be used in combination with j
. In almost all cases the j
flag should be used in
preference to this one.Modifier and Type | Class and Description |
---|---|
static class |
WriteConcern.Majority
A write concern that blocks acknowledgement of a write operation until a majority of replica set members have applied it.
|
Modifier and Type | Field and Description |
---|---|
static WriteConcern |
ACKNOWLEDGED
Write operations that use this write concern will wait for acknowledgement from the primary server before returning.
|
static WriteConcern |
ERRORS_IGNORED
Deprecated.
There is no replacement for this write concern. The closest would be to use WriteConcern#UNACKNOWLEDGED,
then catch and ignore any exceptions of type MongoSocketException.
|
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
Deprecated.
|
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 and Description |
---|
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 continueOnError)
Deprecated.
the preferred way to specify continueOnError is to use write methods that explicitly specify the value of this
property
|
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 continueOnError)
Deprecated.
the preferred way to specify continueOnError is to use write methods that explicitly specify the value of this
property
|
Modifier and Type | Method and Description |
---|---|
boolean |
callGetLastError()
Returns whether "getlasterror" should be called (w > 0)
|
WriteConcern |
continueOnError(boolean continueOnError)
Deprecated.
the preferred way to specify this is to use write methods that explicitly specify the value of this property
|
WriteConcern |
continueOnErrorForInsert(boolean continueOnErrorForInsert)
Deprecated.
the preferred way to specify this is to use write methods that explicitly specify the value of this property
|
boolean |
equals(Object o) |
boolean |
fsync()
Gets the fsync flag (fsync to disk on the server)
|
BasicDBObject |
getCommand()
Deprecated.
This will be removed in 3.0
|
boolean |
getContinueOnError()
Deprecated.
the preferred way to specify this is to use write methods that explicitly specify the value of this property
|
boolean |
getContinueOnErrorForInsert()
Deprecated.
the preferred way to specify this is to use write methods that explicitly specify the value of this property
|
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()
Deprecated.
There is no replacement for this method.
|
void |
setWObject(Object w)
Deprecated.
construct a new instance instead. This method will be removed in a future major release, as instances of this class
should really be immutable.
|
String |
toString() |
static WriteConcern |
valueOf(String name)
Gets the WriteConcern constants by name (matching is done case insensitively).
|
@Deprecated public static final WriteConcern ERRORS_IGNORED
UNACKNOWLEDGED
,
MongoSocketException
public static final WriteConcern ACKNOWLEDGED
public static final WriteConcern UNACKNOWLEDGED
public static final WriteConcern FSYNCED
public static final WriteConcern JOURNALED
public static final WriteConcern REPLICA_ACKNOWLEDGED
@Deprecated public static final WriteConcern NONE
This field has been superseded by WriteConcern.ERRORS_IGNORED
, and may be deprecated in a future release.
ERRORS_IGNORED
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.
UNACKNOWLEDGED
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.
ACKNOWLEDGED
public static final WriteConcern MAJORITY
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.
FSYNCED
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.
JOURNALED
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.
REPLICA_ACKNOWLEDGED
public WriteConcern()
WriteConcern.UNACKNOWLEDGED
, so writes may be lost without any errors being reported.UNACKNOWLEDGED
public WriteConcern(int w)
WriteConcern(int, int, boolean)
with wtimeout=0 and fsync=falsew
- number of writespublic WriteConcern(String w)
w
- Write Concern tagpublic WriteConcern(int w, int wtimeout)
WriteConcern(int, int, boolean)
with fsync=falsew
- number of writeswtimeout
- timeout for write operationpublic WriteConcern(boolean fsync)
WriteConcern(int, int, boolean)
with w=1 and wtimeout=0fsync
- whether or not to fsyncpublic 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=0
None, network socket errors raisedw=1
Checks server for errors as well as network socket errors raisedw>1
Checks servers (w) for errors as well as network socket errors raisedw
- number of writeswtimeout
- timeout for write operationfsync
- whether or not to fsyncpublic 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=0
None, network socket errors raisedw=1
Checks server for errors as well as network socket errors raisedw>1
Checks servers (w) for errors as well as network socket errors raisedw
- number of writeswtimeout
- timeout for write operationfsync
- whether or not to fsyncj
- whether writes should wait for a journaling group commit@Deprecated public WriteConcern(int w, int wtimeout, boolean fsync, boolean j, boolean continueOnError)
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=0
None, network socket errors raisedw=1
Checks server for errors as well as network socket errors raisedw>1
Checks servers (w) for errors as well as network socket errors raisedw
- number of writeswtimeout
- timeout for write operationfsync
- whether or not to fsyncj
- whether writes should wait for a journaling group commitcontinueOnError
- if batch writes should continue after the first errorWriteConcern(int, int, boolean)
,
DBCollection.insert(java.util.List, InsertOptions)
,
InsertOptions.continueOnError(boolean)
,
DBCollection.initializeUnorderedBulkOperation()
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=0
None, network socket errors raisedw=1
Checks server for errors as well as network socket errors raisedw>1
Checks servers (w) for errors as well as network socket errors raisedw
- number of writeswtimeout
- timeout for write operationfsync
- whether or not to fsyncj
- whether writes should wait for a journaling group commit@Deprecated public WriteConcern(String w, int wtimeout, boolean fsync, boolean j, boolean continueOnError)
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=0
None, network socket errors raisedw=1
Checks server for errors as well as network socket errors raisedw>1
Checks servers (w) for errors as well as network socket errors raisedw
- number of writeswtimeout
- timeout for write operationfsync
- whether or not to fsyncj
- whether writes should wait for a journaling group commitcontinueOnError
- if batched writes should continue after the first errorWriteConcern(String, int, boolean, boolean)
,
DBCollection.insert(java.util.List, InsertOptions)
,
InsertOptions.continueOnError(boolean)
,
DBCollection.initializeUnorderedBulkOperation()
@Deprecated public BasicDBObject getCommand()
w <= 0
@Deprecated public void setWObject(Object w)
w
- the value of w.public Object getWObject()
public int getW()
ClassCastException
- if w is not an integerpublic String getWString()
ClassCastException
- if w is not a Stringpublic int getWtimeout()
public boolean getFsync()
public boolean fsync()
@Deprecated public boolean raiseNetworkErrors()
ERRORS_IGNORED
public boolean callGetLastError()
public static WriteConcern valueOf(String name)
name
- the name of the WriteConcernWriteConcern instance
public boolean getJ()
@Deprecated public WriteConcern continueOnError(boolean continueOnError)
continueOnError
- whether to continue on error after a failureDBCollection.insert(java.util.List, InsertOptions)
,
InsertOptions.continueOnError(boolean)
,
DBCollection.initializeUnorderedBulkOperation()
@Deprecated public boolean getContinueOnError()
DBCollection.insert(java.util.List, InsertOptions)
,
InsertOptions.isContinueOnError()
,
DBCollection.initializeUnorderedBulkOperation()
@Deprecated public WriteConcern continueOnErrorForInsert(boolean continueOnErrorForInsert)
continueOnErrorForInsert
- whether to continue on error after a failureDBCollection.insert(java.util.List, InsertOptions)
,
InsertOptions.continueOnError(boolean)
,
DBCollection.initializeUnorderedBulkOperation()
@Deprecated public boolean getContinueOnErrorForInsert()
DBCollection.insert(java.util.List, InsertOptions)
,
InsertOptions.isContinueOnError()
,
DBCollection.initializeUnorderedBulkOperation()
public static WriteConcern.Majority majorityWriteConcern(int wtimeout, boolean fsync, boolean j)
wtimeout
- timeout for write operationfsync
- whether or not to fsyncj
- whether writes should wait for a journal group commit