public Response call(DB db, DBCollection coll, OutMessage m, int retries) throws MongoException { final MyPort mp = _threadPort.get(); final DBPort port = mp.get(false); port.checkAuth(db); Response res = null; try { res = port.call(m, coll); mp.done(port); } catch (IOException ioe) { mp.error(ioe); if (_error(ioe) && retries > 0) { return call(db, coll, m, retries - 1); } throw new MongoException.Network("can't call something", ioe); } catch (RuntimeException re) { mp.error(re); throw re; } ServerError err = res.getError(); if (err != null && err.isNotMasterError()) { _pickCurrent(); if (retries <= 0) { throw new MongoException("not talking to master and retries used up"); } return call(db, coll, m, retries - 1); } return res; }
public WriteResult say(DB db, OutMessage m, DB.WriteConcern concern) throws MongoException { MyPort mp = _threadPort.get(); DBPort port = mp.get(true); port.checkAuth(db); try { port.say(m); if (concern == DB.WriteConcern.STRICT) { return _checkWriteError(mp, port); } else { mp.done(port); return new WriteResult(db, port); } } catch (IOException ioe) { mp.error(ioe); _error(ioe); if (concern == DB.WriteConcern.NONE) { CommandResult res = new CommandResult(); res.put("ok", false); res.put("$err", "NETWORK ERROR"); return new WriteResult(res); } throw new MongoException.Network("can't say something", ioe); } catch (MongoException me) { throw me; } catch (RuntimeException re) { mp.error(re); throw re; } }
WriteResult _checkWriteError(MyPort mp, DBPort port) throws MongoException { CommandResult e = _mongo.getDB("admin").getLastError(); mp.done(port); Object foo = e.get("err"); if (foo == null) return new WriteResult(e); int code = -1; if (e.get("code") instanceof Number) code = ((Number) e.get("code")).intValue(); String s = foo.toString(); if (code == 11000 || code == 11001 || s.startsWith("E11000") || s.startsWith("E11001")) throw new MongoException.DuplicateKey(code, s); throw new MongoException(code, s); }