@@ -86,7 +86,7 @@ public void close() {
86
86
* @return
87
87
*/
88
88
public void insertDocument (String dataBaseName , String collectionName , String input )
89
- throws MongoWriteException {
89
+ throws MongoWriteException , MongoClientException {
90
90
MongoCollection <Document > collection = getMongoCollection (dataBaseName , collectionName );
91
91
if (collection != null ) {
92
92
final Document dbObjectInput = Document .parse (input );
@@ -123,7 +123,7 @@ public ArrayList<String> getAllDocuments(String dataBaseName, String collectionN
123
123
}
124
124
}
125
125
} catch (Exception e ) {
126
- LOGGER .error ("Failed to retrieve documents. " , e );
126
+ LOGGER .error ("Failed to retrieve documents from the collection {} as {} " , collectionName , e . getMessage () );
127
127
}
128
128
return result ;
129
129
}
@@ -137,7 +137,7 @@ public ArrayList<String> getAllDocuments(String dataBaseName, String collectionN
137
137
* @return
138
138
*/
139
139
public ArrayList <String > find (String dataBaseName , String collectionName ,
140
- MongoQuery query ) {
140
+ MongoQuery query ) throws MongoClientException {
141
141
ArrayList <String > result = new ArrayList <>(0 );
142
142
143
143
try {
@@ -161,7 +161,7 @@ public ArrayList<String> find(String dataBaseName, String collectionName,
161
161
*/
162
162
public boolean updateDocument (String dataBaseName , String collectionName ,
163
163
MongoQuery queryFilter ,
164
- String updateInput ) {
164
+ String updateInput ) throws MongoClientException {
165
165
try {
166
166
return doUpdate (dataBaseName , collectionName , queryFilter , updateInput );
167
167
} catch (Exception e ) {
@@ -219,7 +219,7 @@ public boolean dropDocument(String dataBaseName, String collectionName, MongoQue
219
219
* @param ttlValue seconds
220
220
*/
221
221
public void createTTLIndex (String dataBaseName , String collectionName , String fieldName ,
222
- int ttlValue ) {
222
+ int ttlValue ) throws MongoClientException {
223
223
MongoCollection <Document > collection = getMongoCollection (dataBaseName , collectionName );
224
224
IndexOptions indexOptions = new IndexOptions ().expireAfter ((long ) ttlValue ,
225
225
TimeUnit .SECONDS );
@@ -274,7 +274,7 @@ private void createMongoClient() throws AbortExecutionException {
274
274
}
275
275
276
276
private ArrayList <String > doFind (String dataBaseName , String collectionName ,
277
- MongoQuery query ) {
277
+ MongoQuery query ) throws MongoClientException {
278
278
LOGGER .debug (
279
279
"Find and retrieve data from database.\n Database: {}\n Collection: {}\n Condition/Query: {}" ,
280
280
dataBaseName , collectionName , query .getQueryString ());
@@ -310,7 +310,7 @@ private ArrayList<String> doFind(String dataBaseName, String collectionName,
310
310
311
311
private Document doFindAndModify (String dataBaseName , String collectionName ,
312
312
MongoQuery queryFilter ,
313
- String updateInput ) {
313
+ String updateInput ) throws MongoClientException {
314
314
MongoCollection <Document > collection = getMongoCollection (dataBaseName , collectionName );
315
315
if (collection == null ) {
316
316
return null ;
@@ -327,7 +327,7 @@ private Document doFindAndModify(String dataBaseName, String collectionName,
327
327
}
328
328
329
329
private boolean doUpdate (String dataBaseName , String collectionName , MongoQuery queryFilter ,
330
- String updateInput ) {
330
+ String updateInput ) throws MongoClientException {
331
331
MongoCollection <Document > collection = getMongoCollection (dataBaseName , collectionName );
332
332
if (collection == null ) {
333
333
return false ;
@@ -344,7 +344,7 @@ private boolean doUpdate(String dataBaseName, String collectionName, MongoQuery
344
344
return updateWasPerformed ;
345
345
}
346
346
347
- private boolean doDrop (String dataBaseName , String collectionName , MongoQuery query ) {
347
+ private boolean doDrop (String dataBaseName , String collectionName , MongoQuery query ) throws MongoClientException {
348
348
MongoCollection <Document > collection = getMongoCollection (dataBaseName , collectionName );
349
349
if (collection == null ) {
350
350
return false ;
@@ -364,33 +364,27 @@ private boolean doDrop(String dataBaseName, String collectionName, MongoQuery qu
364
364
365
365
}
366
366
367
- private MongoCollection <Document > getMongoCollection (String databaseName ,
368
- String collectionName ) {
367
+ private MongoCollection <Document > getMongoCollection (String databaseName , String collectionName )
368
+ throws MongoClientException {
369
369
if (mongoClient == null ) {
370
- return null ;
370
+ throw new MongoClientException ( "Failed to connect MongoDB" ) ;
371
371
}
372
372
373
- try {
374
- verifyExistanceOfCollection (databaseName , collectionName );
375
-
376
- MongoDatabase db = mongoClient .getDatabase (databaseName );
377
- MongoCollection <Document > collection = db .getCollection (collectionName );
378
- return collection ;
379
- } catch (MongoClientException e ) {
380
- LOGGER .error ("Failure when handling Mongo collection: {} " , e .getMessage (), e );
381
- return null ;
382
- }
373
+ verifyExistenceOfCollection (databaseName , collectionName );
383
374
375
+ MongoDatabase db = mongoClient .getDatabase (databaseName );
376
+ MongoCollection <Document > collection = db .getCollection (collectionName );
377
+ return collection ;
384
378
}
385
379
386
- private void verifyExistanceOfCollection (String databaseName , String collectionName ) {
380
+ private void verifyExistenceOfCollection (String databaseName , String collectionName ) throws MongoClientException {
387
381
List <String > collectionList = getCollectionList (databaseName );
388
382
if (!collectionList .contains (collectionName )) {
389
383
createCollection (databaseName , collectionName );
390
384
}
391
385
}
392
386
393
- private List <String > getCollectionList (String databaseName ) {
387
+ private List <String > getCollectionList (String databaseName ) throws MongoClientException {
394
388
try {
395
389
MongoDatabase db = mongoClient .getDatabase (databaseName );
396
390
List <String > collectionList = db .listCollectionNames ().into (new ArrayList <String >());
@@ -404,7 +398,7 @@ private List<String> getCollectionList(String databaseName) {
404
398
}
405
399
}
406
400
407
- private void createCollection (String databaseName , String collectionName ) {
401
+ private void createCollection (String databaseName , String collectionName ) throws MongoClientException {
408
402
try {
409
403
LOGGER .debug (
410
404
"The requested database({}) / collection({}) not found in mongodb, Creating." ,
@@ -429,7 +423,7 @@ private void createCollection(String databaseName, String collectionName) {
429
423
* @param e
430
424
*/
431
425
private void checkIfCollectionExistError (String databaseName , String collectionName ,
432
- MongoCommandException e ) {
426
+ MongoCommandException e ) throws MongoClientException {
433
427
String collectionExistsError = String .format ("collection '%s.%s' already exists" ,
434
428
databaseName , collectionName );
435
429
0 commit comments