Skip to content

Commit cdb25d8

Browse files
committed
JAVA-361: add methods to fsync, lock, unlock the db
1 parent 1312fbb commit cdb25d8

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed

src/main/com/mongodb/Mongo.java

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -556,6 +556,55 @@ protected PoolOutputBuffer createNew(){
556556

557557
};
558558

559+
/**
560+
* Forces the master server to fsync the RAM data to disk
561+
* This is done automatically by the server at intervals, but can be forced for better reliability.
562+
* @param async if true, the fsync will be done asynchronously on the server.
563+
* @return
564+
*/
565+
public CommandResult fsync(boolean async) {
566+
DBObject cmd = new BasicDBObject("fsync", 1);
567+
if (async) {
568+
cmd.put("async", 1);
569+
}
570+
return getDB("admin").command(cmd);
571+
}
572+
573+
/**
574+
* Forces the master server to fsync the RAM data to disk, then lock all writes.
575+
* The database will be read-only after this command returns.
576+
* @return
577+
*/
578+
public CommandResult fsyncAndLock() {
579+
DBObject cmd = new BasicDBObject("fsync", 1);
580+
cmd.put("lock", 1);
581+
return getDB("admin").command(cmd);
582+
}
583+
584+
/**
585+
* Unlocks the database, allowing the write operations to go through.
586+
* This command may be asynchronous on the server, which means there may be a small delay before the database becomes writable.
587+
* @return
588+
*/
589+
public DBObject unlock() {
590+
DB db = getDB("admin");
591+
DBCollection col = db.getCollection("$cmd.sys.unlock");
592+
return col.findOne();
593+
}
594+
595+
/**
596+
* Returns true if the database is locked (read-only), false otherwise.
597+
* @return
598+
*/
599+
public boolean isLocked() {
600+
DB db = getDB("admin");
601+
DBCollection col = db.getCollection("$cmd.sys.inprog");
602+
BasicDBObject res = (BasicDBObject) col.findOne();
603+
if (res.containsField("fsyncLock")) {
604+
return res.getInt("fsyncLock") == 1;
605+
}
606+
return false;
607+
}
559608

560609
// -------
561610

0 commit comments

Comments
 (0)