①、WriteConcern中的几个安全级别:
/** No exceptions are raised, even for network issues */
public final static WriteConcern NONE = new WriteConcern(-1);
/** Exceptions are raised for network issues, but not server errors */
public final static WriteConcern NORMAL = new WriteConcern(0);
/** Exceptions are raised for network issues, and server errors; waits on a server for the write operation */
public final static WriteConcern SAFE = new WriteConcern(1);
/** Exceptions are raised for network issues, and server errors; waits on a majority of servers for the write operation ,waitsfor more than 50%of the configured nodes to acknowledge the write (until replication is applied to the point of that write)
*/
public final static WriteConcern MAJORITY = new Majority();
/** Exceptions are raised for network issues, and server errors; the write operation waits for the server to flush the data to disk*/
public final static WriteConcern FSYNC_SAFE = new WriteConcern(true);
/** 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 final static WriteConcern JOURNAL_SAFE = new WriteConcern( 1, 0, false, true );
/** Exceptions are raised for network issues, and server errors; waits for at least 2 servers for the write operation*/
public final static WriteConcern REPLICAS_SAFE = new WriteConcern(2);
NONE的级别最低,不管出了啥事儿,客户端一股脑的往mongo插入,连网络断了都不管。REPLICAS_SAFE的级别最高,他要等从库都同步完才返回给客户端插入成功,复制集的话要至少两台同步完才行,分布式事务啊,牛叉,但是,它没有事务。。。开玩喜了。
三、MongoOption的使用:
MongoOptions op = new MongoOptions();
op.safe=true;
op.connctionPerHost=50;
op.connctionTimeout=120000;
....
//list是serverAddress的列表
Mongo m = new Mongo(list,op);
WriteConcern设置好之后用