Skip to content

Commit

Permalink
JAVA-361: add methods to fsync, lock, unlock the db
Browse files Browse the repository at this point in the history
  • Loading branch information
agirbal committed May 31, 2011
1 parent 1312fbb commit cdb25d8
Showing 1 changed file with 49 additions and 0 deletions.
49 changes: 49 additions & 0 deletions src/main/com/mongodb/Mongo.java
Original file line number Diff line number Diff line change
Expand Up @@ -556,6 +556,55 @@ protected PoolOutputBuffer createNew(){

};

/**
* Forces the master server to fsync the RAM data to disk
* This is done automatically by the server at intervals, but can be forced for better reliability.
* @param async if true, the fsync will be done asynchronously on the server.
* @return
*/
public CommandResult fsync(boolean async) {
DBObject cmd = new BasicDBObject("fsync", 1);
if (async) {
cmd.put("async", 1);
}
return getDB("admin").command(cmd);
}

/**
* Forces the master server to fsync the RAM data to disk, then lock all writes.
* The database will be read-only after this command returns.
* @return
*/
public CommandResult fsyncAndLock() {
DBObject cmd = new BasicDBObject("fsync", 1);
cmd.put("lock", 1);
return getDB("admin").command(cmd);
}

/**
* Unlocks the database, allowing the write operations to go through.
* This command may be asynchronous on the server, which means there may be a small delay before the database becomes writable.
* @return
*/
public DBObject unlock() {
DB db = getDB("admin");
DBCollection col = db.getCollection("$cmd.sys.unlock");
return col.findOne();
}

/**
* Returns true if the database is locked (read-only), false otherwise.
* @return
*/
public boolean isLocked() {
DB db = getDB("admin");
DBCollection col = db.getCollection("$cmd.sys.inprog");
BasicDBObject res = (BasicDBObject) col.findOne();
if (res.containsField("fsyncLock")) {
return res.getInt("fsyncLock") == 1;
}
return false;
}

// -------

Expand Down

0 comments on commit cdb25d8

Please sign in to comment.