它會拋出此異常是方法的流程:
template.insert(object)
|
|
templateinsert(Object objectToSave, String collectionName)
|
|
protected <T> void doInsert(String collectionName, T objectToSave, MongoWriter<T> writer)
|
|
protected Object insertDBObject(final String collectionName, final DBObject dbDoc, final Class<?> entityClass)
|
|
這裏是insertDBObject
protected Object insertDBObject(final String collectionName, final DBObject dbDoc, final Class<?> entityClass) {
if(LOGGER.isDebugEnabled()) {
LOGGER.debug("Inserting DBObject containing fields: {} in collection: {}", dbDoc.keySet(), collectionName);
}
return this.execute(collectionName, new CollectionCallback() {
public Object doInCollection(DBCollection collection) throws MongoException, DataAccessException {
MongoAction mongoAction = new MongoAction(MongoTemplate.this.writeConcern, MongoActionOperation.INSERT, collectionName, entityClass, dbDoc, (DBObject)null);
WriteConcern writeConcernToUse = MongoTemplate.this.prepareWriteConcern(mongoAction);
WriteResult writeResult = writeConcernToUse == null?collection.insert(new DBObject[]{dbDoc}):collection.insert(dbDoc, writeConcernToUse);
MongoTemplate.this.handleAnyWriteResultErrors(writeResult, dbDoc, MongoActionOperation.INSERT);
return dbDoc.get("_id");
}
});
}
的源代碼,這將拋出當你寫失敗例外,他們是運行時異常。運行時問題由MongoExceptionTranslator 轉換。因此,任何不會拋出任何異常的插入都會成功。
但文檔不會說它會拋出異常或類似的東西。 – andyPaul
這裏是grepcode的墨水http://grepcode.com/file/repo1.maven.org/maven2/org.springframework.data/spring-data-mongodb/1.7.2.RELEASE/org/springframework/data/mongodb /core/MongoTemplate.java –
有些情況下會讓mongo不寫數據的是 1.磁盤空間已滿,2.切斷3.訪問權限 我們可以很容易地重現1和3.在插入前運行代碼把調試器發生。代碼執行停止。關閉monogo服務器或撤銷用戶的寫權限,然後恢復。它應該拋出異常。 –