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