Ensure file insertion finished with GridFS - MongoDB Java API -


i have rest service upload images , main code in charge of registration in mongodb:

public string writefiles(inputstream inputstream, string filename, string contenttype) throws ioexception {     // save file     gridfs gridfs = new gridfs(getdb(), collections.pictures_files.name());      gridfsinputfile gridfsinputfile = gridfs.createfile(inputstream, filename);     gridfsinputfile.setcontenttype(contenttype);     gridfsinputfile.setmetadata(new basicdbobject(original_pict_col, true));      gridfsinputfile.save();     return gridfsinputfile.getid(); } 

the service return file id client 1 can ask , display uploaded image.

the problem large images: sometime while requesting image id right after upload gives http 404 error (due unknown image id on server side, correct behavior). suppose happens because registration time on server side greater time used id , request new image on client side - i.e '.save()' operation async, right ?

my question: how sure save operation has been completed before returning id in given code ? or how obtain result object .insert operation ?

does

    gridfsinputfile.validate(); 

would enought ?

or

  getdb().getlasterror() 

?

i cannot reproduce "bug" ask question in case experience know how solve this. in advance help.

if using recent version of java driver (2.10 or later), try creating instance of mongoclient instead of instance of mongo. default write concern writeconcern.acknowledged instances of mongoclient, save method not complete until write operation has completed.

otherwise, in getdb method (not shown), call method db.setwriteconcern(writeconcern.safe) change default write concern.

the other possibility reading secondary member of replica set. default read primary, if overriding that, reads consistent, , see problem in case well.


Comments

Popular posts from this blog

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -

node.js - Bad Request - node js ajax post -