如何在更新记录时处理异步有序Bulkwrite异常

时间:2016-04-22 12:32:55

标签: mongodb

在什么情况下,通常更新失败。我使用bulkwrite操作来更新所有记录。

假设在将数据写入db时发生异常,如何处理这些异常?任何人都可以帮我解决这个问题....

我的代码:

public void asyncWriteToMongo(String mappingName, List<WriteModel<Document>> documentList, String colName,
                String status) throws Exception {

            final CountDownLatch bulkLatch = new CountDownLatch(1);
            SingleResultCallback<BulkWriteResult> printBatchResult = new SingleResultCallback<BulkWriteResult>() {
                @Override
                public void onResult(final BulkWriteResult result, final Throwable t) {

                    try {
                        updateStatus(name, result.getCount());
                        System.out.println(result +" "+ t);
                    } catch (Exception e) {
                        t.setStackTrace(e.getStackTrace());
                    }
                    if (t != null) {
                        t.printStackTrace();
                    }
                    bulkLatch.countDown();
                }
            };

            ConnectionManager cm = new ConnectionManager();
            com.mongodb.async.client.MongoDatabase mongoDatabase = cm.getAsyncMongoClient();
            com.mongodb.async.client.MongoCollection<Document> collection = mongoDatabase.getCollection(colName);
            try{
            collection.bulkWrite(documentList,printBatchResult);
            }catch (Exception e) {
                   e.printStackTrace();
                }

            bulkLatch.await();
        }

0 个答案:

没有答案