@@ -341,7 +341,7 @@ public CloseableIterator<T> doInCollection(DBCollection collection) throws Mongo
341
341
ReadDbObjectCallback <T > readCallback = new ReadDbObjectCallback <T >(mongoConverter , entityType , collection
342
342
.getName ());
343
343
344
- return new CloseableIterableCusorAdapter <T >(cursorPreparer .prepare (cursor ), exceptionTranslator , readCallback );
344
+ return new CloseableIterableCursorAdapter <T >(cursorPreparer .prepare (cursor ), exceptionTranslator , readCallback );
345
345
}
346
346
});
347
347
}
@@ -445,7 +445,7 @@ public <T> T execute(DbCallback<T> action) {
445
445
DB db = this .getDb ();
446
446
return action .doInDB (db );
447
447
} catch (RuntimeException e ) {
448
- throw potentiallyConvertRuntimeException (e );
448
+ throw potentiallyConvertRuntimeException (e , exceptionTranslator );
449
449
}
450
450
}
451
451
@@ -461,7 +461,7 @@ public <T> T execute(String collectionName, CollectionCallback<T> callback) {
461
461
DBCollection collection = getAndPrepareCollection (getDb (), collectionName );
462
462
return callback .doInCollection (collection );
463
463
} catch (RuntimeException e ) {
464
- throw potentiallyConvertRuntimeException (e );
464
+ throw potentiallyConvertRuntimeException (e , exceptionTranslator );
465
465
}
466
466
}
467
467
@@ -1548,10 +1548,17 @@ protected String replaceWithResourceIfNecessary(String function) {
1548
1548
throw new InvalidDataAccessApiUsageException (String .format ("Resource %s not found!" , function ));
1549
1549
}
1550
1550
1551
+ Scanner scanner = null ;
1552
+
1551
1553
try {
1552
- return new Scanner (functionResource .getInputStream ()).useDelimiter ("\\ A" ).next ();
1554
+ scanner = new Scanner (functionResource .getInputStream ());
1555
+ return scanner .useDelimiter ("\\ A" ).next ();
1553
1556
} catch (IOException e ) {
1554
1557
throw new InvalidDataAccessApiUsageException (String .format ("Cannot read map-reduce file %s!" , function ), e );
1558
+ } finally {
1559
+ if (scanner != null ) {
1560
+ scanner .close ();
1561
+ }
1555
1562
}
1556
1563
}
1557
1564
@@ -1814,7 +1821,7 @@ private DBCollection getAndPrepareCollection(DB db, String collectionName) {
1814
1821
prepareCollection (collection );
1815
1822
return collection ;
1816
1823
} catch (RuntimeException e ) {
1817
- throw potentiallyConvertRuntimeException (e );
1824
+ throw potentiallyConvertRuntimeException (e , exceptionTranslator );
1818
1825
}
1819
1826
}
1820
1827
@@ -1840,7 +1847,7 @@ private <T> T executeFindOneInternal(CollectionCallback<DBObject> collectionCall
1840
1847
collectionName )));
1841
1848
return result ;
1842
1849
} catch (RuntimeException e ) {
1843
- throw potentiallyConvertRuntimeException (e );
1850
+ throw potentiallyConvertRuntimeException (e , exceptionTranslator );
1844
1851
}
1845
1852
}
1846
1853
@@ -1893,7 +1900,7 @@ private <T> List<T> executeFindMultiInternal(CollectionCallback<DBCursor> collec
1893
1900
}
1894
1901
}
1895
1902
} catch (RuntimeException e ) {
1896
- throw potentiallyConvertRuntimeException (e );
1903
+ throw potentiallyConvertRuntimeException (e , exceptionTranslator );
1897
1904
}
1898
1905
}
1899
1906
@@ -1923,7 +1930,7 @@ private void executeQueryInternal(CollectionCallback<DBCursor> collectionCallbac
1923
1930
}
1924
1931
1925
1932
} catch (RuntimeException e ) {
1926
- throw potentiallyConvertRuntimeException (e );
1933
+ throw potentiallyConvertRuntimeException (e , exceptionTranslator );
1927
1934
}
1928
1935
}
1929
1936
@@ -2002,18 +2009,6 @@ protected void handleAnyWriteResultErrors(WriteResult writeResult, DBObject quer
2002
2009
}
2003
2010
}
2004
2011
2005
- /**
2006
- * Tries to convert the given {@link RuntimeException} into a {@link DataAccessException} but returns the original
2007
- * exception if the conversation failed. Thus allows safe rethrowing of the return value.
2008
- *
2009
- * @param ex
2010
- * @return
2011
- */
2012
- private RuntimeException potentiallyConvertRuntimeException (RuntimeException ex ) {
2013
- RuntimeException resolved = this .exceptionTranslator .translateExceptionIfPossible (ex );
2014
- return resolved == null ? ex : resolved ;
2015
- }
2016
-
2017
2012
/**
2018
2013
* Inspects the given {@link CommandResult} for erros and potentially throws an
2019
2014
* {@link InvalidDataAccessApiUsageException} for that error.
@@ -2052,6 +2047,20 @@ private DBObject getMappedSortObject(Query query, Class<?> type) {
2052
2047
return queryMapper .getMappedSort (query .getSortObject (), mappingContext .getPersistentEntity (type ));
2053
2048
}
2054
2049
2050
+ /**
2051
+ * Tries to convert the given {@link RuntimeException} into a {@link DataAccessException} but returns the original
2052
+ * exception if the conversation failed. Thus allows safe re-throwing of the return value.
2053
+ *
2054
+ * @param ex the exception to translate
2055
+ * @param exceptionTranslator the {@link PersistenceExceptionTranslator} to be used for translation
2056
+ * @return
2057
+ */
2058
+ private static RuntimeException potentiallyConvertRuntimeException (RuntimeException ex ,
2059
+ PersistenceExceptionTranslator exceptionTranslator ) {
2060
+ RuntimeException resolved = exceptionTranslator .translateExceptionIfPossible (ex );
2061
+ return resolved == null ? ex : resolved ;
2062
+ }
2063
+
2055
2064
// Callback implementations
2056
2065
2057
2066
/**
@@ -2298,7 +2307,7 @@ public DBCursor prepare(DBCursor cursor) {
2298
2307
}
2299
2308
2300
2309
} catch (RuntimeException e ) {
2301
- throw potentiallyConvertRuntimeException (e );
2310
+ throw potentiallyConvertRuntimeException (e , exceptionTranslator );
2302
2311
}
2303
2312
2304
2313
return cursorToUse ;
@@ -2345,20 +2354,20 @@ public GeoResult<T> doWith(DBObject object) {
2345
2354
* @since 1.7
2346
2355
* @author Thomas Darimont
2347
2356
*/
2348
- static class CloseableIterableCusorAdapter <T > implements CloseableIterator <T > {
2357
+ static class CloseableIterableCursorAdapter <T > implements CloseableIterator <T > {
2349
2358
2350
2359
private volatile Cursor cursor ;
2351
2360
private PersistenceExceptionTranslator exceptionTranslator ;
2352
2361
private DbObjectCallback <T > objectReadCallback ;
2353
2362
2354
2363
/**
2355
- * Creates a new {@link CloseableIterableCusorAdapter } backed by the given {@link Cursor}.
2364
+ * Creates a new {@link CloseableIterableCursorAdapter } backed by the given {@link Cursor}.
2356
2365
*
2357
2366
* @param cursor
2358
2367
* @param exceptionTranslator
2359
2368
* @param objectReadCallback
2360
2369
*/
2361
- public CloseableIterableCusorAdapter (Cursor cursor , PersistenceExceptionTranslator exceptionTranslator ,
2370
+ public CloseableIterableCursorAdapter (Cursor cursor , PersistenceExceptionTranslator exceptionTranslator ,
2362
2371
DbObjectCallback <T > objectReadCallback ) {
2363
2372
2364
2373
this .cursor = cursor ;
@@ -2376,7 +2385,7 @@ public boolean hasNext() {
2376
2385
try {
2377
2386
return cursor .hasNext ();
2378
2387
} catch (RuntimeException ex ) {
2379
- throw exceptionTranslator . translateExceptionIfPossible (ex );
2388
+ throw potentiallyConvertRuntimeException (ex , exceptionTranslator );
2380
2389
}
2381
2390
}
2382
2391
@@ -2392,7 +2401,7 @@ public T next() {
2392
2401
T converted = objectReadCallback .doWith (item );
2393
2402
return converted ;
2394
2403
} catch (RuntimeException ex ) {
2395
- throw exceptionTranslator . translateExceptionIfPossible (ex );
2404
+ throw potentiallyConvertRuntimeException (ex , exceptionTranslator );
2396
2405
}
2397
2406
}
2398
2407
@@ -2403,7 +2412,7 @@ public void close() {
2403
2412
try {
2404
2413
c .close ();
2405
2414
} catch (RuntimeException ex ) {
2406
- throw exceptionTranslator . translateExceptionIfPossible (ex );
2415
+ throw potentiallyConvertRuntimeException (ex , exceptionTranslator );
2407
2416
} finally {
2408
2417
cursor = null ;
2409
2418
exceptionTranslator = null ;
0 commit comments