18
18
import java .util .Collection ;
19
19
import java .util .List ;
20
20
import java .util .Set ;
21
- import java .util .concurrent .atomic .AtomicReference ;
22
- import java .util .concurrent .locks .Lock ;
23
21
import java .util .concurrent .locks .ReentrantLock ;
24
22
import java .util .function .Consumer ;
25
23
import java .util .function .Supplier ;
26
- import java .util .function .UnaryOperator ;
27
24
import java .util .stream .Stream ;
28
25
29
26
import org .bson .Document ;
50
47
import org .springframework .data .mongodb .core .query .Query ;
51
48
import org .springframework .data .mongodb .core .query .Update ;
52
49
import org .springframework .data .mongodb .core .query .UpdateDefinition ;
50
+ import org .springframework .data .mongodb .util .Lock ;
53
51
import org .springframework .lang .Nullable ;
54
52
import org .springframework .util .Assert ;
55
53
import org .springframework .util .ClassUtils ;
@@ -192,20 +190,18 @@ default SessionScoped withSession(Supplier<ClientSession> sessionProvider) {
192
190
193
191
return new SessionScoped () {
194
192
195
- private final Lock lock = new ReentrantLock ();
193
+ private final Lock lock = Lock . of ( new ReentrantLock () );
196
194
private @ Nullable ClientSession session ;
197
195
198
196
@ Override
199
197
public <T > T execute (SessionCallback <T > action , Consumer <ClientSession > onComplete ) {
200
198
201
- lock .lock ();
202
- try {
199
+ lock .executeWithoutResult (() -> {
200
+
203
201
if (session == null ) {
204
202
session = sessionProvider .get ();
205
203
}
206
- } finally {
207
- lock .unlock ();
208
- }
204
+ });
209
205
210
206
try {
211
207
return action .doInSession (MongoOperations .this .withSession (session ));
@@ -950,8 +946,8 @@ default <T> List<T> findDistinct(Query query, String field, String collection, C
950
946
* Triggers <a href="https://docs.mongodb.org/manual/reference/method/db.collection.findAndModify/">findAndModify </a>
951
947
* to apply provided {@link Update} on documents matching {@link Criteria} of given {@link Query}.
952
948
*
953
- * @param query the {@link Query} class that specifies the {@link Criteria} used to find a document and also an optional
954
- * fields specification. Must not be {@literal null}.
949
+ * @param query the {@link Query} class that specifies the {@link Criteria} used to find a document and also an
950
+ * optional fields specification. Must not be {@literal null}.
955
951
* @param update the {@link UpdateDefinition} to apply on matching documents. Must not be {@literal null}.
956
952
* @param entityClass the parametrized type. Must not be {@literal null}.
957
953
* @return the converted object that was updated before it was updated or {@literal null}, if not found.
@@ -966,8 +962,8 @@ default <T> List<T> findDistinct(Query query, String field, String collection, C
966
962
* Triggers <a href="https://docs.mongodb.org/manual/reference/method/db.collection.findAndModify/">findAndModify </a>
967
963
* to apply provided {@link Update} on documents matching {@link Criteria} of given {@link Query}.
968
964
*
969
- * @param query the {@link Query} class that specifies the {@link Criteria} used to find a document and also an optional
970
- * fields specification. Must not be {@literal null}.
965
+ * @param query the {@link Query} class that specifies the {@link Criteria} used to find a document and also an
966
+ * optional fields specification. Must not be {@literal null}.
971
967
* @param update the {@link UpdateDefinition} to apply on matching documents. Must not be {@literal null}.
972
968
* @param entityClass the parametrized type. Must not be {@literal null}.
973
969
* @param collectionName the collection to query. Must not be {@literal null}.
@@ -984,8 +980,8 @@ default <T> List<T> findDistinct(Query query, String field, String collection, C
984
980
* to apply provided {@link Update} on documents matching {@link Criteria} of given {@link Query} taking
985
981
* {@link FindAndModifyOptions} into account.
986
982
*
987
- * @param query the {@link Query} class that specifies the {@link Criteria} used to find a document and also an optional
988
- * fields specification.
983
+ * @param query the {@link Query} class that specifies the {@link Criteria} used to find a document and also an
984
+ * optional fields specification.
989
985
* @param update the {@link UpdateDefinition} to apply on matching documents.
990
986
* @param options the {@link FindAndModifyOptions} holding additional information.
991
987
* @param entityClass the parametrized type.
@@ -1004,8 +1000,8 @@ default <T> List<T> findDistinct(Query query, String field, String collection, C
1004
1000
* to apply provided {@link Update} on documents matching {@link Criteria} of given {@link Query} taking
1005
1001
* {@link FindAndModifyOptions} into account.
1006
1002
*
1007
- * @param query the {@link Query} class that specifies the {@link Criteria} used to find a document and also an optional
1008
- * fields specification. Must not be {@literal null}.
1003
+ * @param query the {@link Query} class that specifies the {@link Criteria} used to find a document and also an
1004
+ * optional fields specification. Must not be {@literal null}.
1009
1005
* @param update the {@link UpdateDefinition} to apply on matching documents. Must not be {@literal null}.
1010
1006
* @param options the {@link FindAndModifyOptions} holding additional information. Must not be {@literal null}.
1011
1007
* @param entityClass the parametrized type. Must not be {@literal null}.
@@ -1030,8 +1026,8 @@ <T> T findAndModify(Query query, UpdateDefinition update, FindAndModifyOptions o
1030
1026
* Options are defaulted to {@link FindAndReplaceOptions#empty()}. <br />
1031
1027
* <strong>NOTE:</strong> The replacement entity must not hold an {@literal id}.
1032
1028
*
1033
- * @param query the {@link Query} class that specifies the {@link Criteria} used to find a document and also an optional
1034
- * fields specification. Must not be {@literal null}.
1029
+ * @param query the {@link Query} class that specifies the {@link Criteria} used to find a document and also an
1030
+ * optional fields specification. Must not be {@literal null}.
1035
1031
* @param replacement the replacement document. Must not be {@literal null}.
1036
1032
* @return the converted object that was updated or {@literal null}, if not found.
1037
1033
* @throws org.springframework.data.mapping.MappingException if the collection name cannot be
@@ -1051,8 +1047,8 @@ default <T> T findAndReplace(Query query, T replacement) {
1051
1047
* Options are defaulted to {@link FindAndReplaceOptions#empty()}. <br />
1052
1048
* <strong>NOTE:</strong> The replacement entity must not hold an {@literal id}.
1053
1049
*
1054
- * @param query the {@link Query} class that specifies the {@link Criteria} used to find a document and also an optional
1055
- * fields specification. Must not be {@literal null}.
1050
+ * @param query the {@link Query} class that specifies the {@link Criteria} used to find a document and also an
1051
+ * optional fields specification. Must not be {@literal null}.
1056
1052
* @param replacement the replacement document. Must not be {@literal null}.
1057
1053
* @param collectionName the collection to query. Must not be {@literal null}.
1058
1054
* @return the converted object that was updated or {@literal null}, if not found.
@@ -1070,8 +1066,8 @@ default <T> T findAndReplace(Query query, T replacement, String collectionName)
1070
1066
* taking {@link FindAndReplaceOptions} into account.<br />
1071
1067
* <strong>NOTE:</strong> The replacement entity must not hold an {@literal id}.
1072
1068
*
1073
- * @param query the {@link Query} class that specifies the {@link Criteria} used to find a document and also an optional
1074
- * fields specification. Must not be {@literal null}.
1069
+ * @param query the {@link Query} class that specifies the {@link Criteria} used to find a document and also an
1070
+ * optional fields specification. Must not be {@literal null}.
1075
1071
* @param replacement the replacement document. Must not be {@literal null}.
1076
1072
* @param options the {@link FindAndModifyOptions} holding additional information. Must not be {@literal null}.
1077
1073
* @return the converted object that was updated or {@literal null}, if not found. Depending on the value of
@@ -1093,8 +1089,8 @@ default <T> T findAndReplace(Query query, T replacement, FindAndReplaceOptions o
1093
1089
* taking {@link FindAndReplaceOptions} into account.<br />
1094
1090
* <strong>NOTE:</strong> The replacement entity must not hold an {@literal id}.
1095
1091
*
1096
- * @param query the {@link Query} class that specifies the {@link Criteria} used to find a document and also an optional
1097
- * fields specification. Must not be {@literal null}.
1092
+ * @param query the {@link Query} class that specifies the {@link Criteria} used to find a document and also an
1093
+ * optional fields specification. Must not be {@literal null}.
1098
1094
* @param replacement the replacement document. Must not be {@literal null}.
1099
1095
* @param options the {@link FindAndModifyOptions} holding additional information. Must not be {@literal null}.
1100
1096
* @return the converted object that was updated or {@literal null}, if not found. Depending on the value of
@@ -1116,8 +1112,8 @@ default <T> T findAndReplace(Query query, T replacement, FindAndReplaceOptions o
1116
1112
* taking {@link FindAndReplaceOptions} into account.<br />
1117
1113
* <strong>NOTE:</strong> The replacement entity must not hold an {@literal id}.
1118
1114
*
1119
- * @param query the {@link Query} class that specifies the {@link Criteria} used to find a document and also an optional
1120
- * fields specification. Must not be {@literal null}.
1115
+ * @param query the {@link Query} class that specifies the {@link Criteria} used to find a document and also an
1116
+ * optional fields specification. Must not be {@literal null}.
1121
1117
* @param replacement the replacement document. Must not be {@literal null}.
1122
1118
* @param options the {@link FindAndModifyOptions} holding additional information. Must not be {@literal null}.
1123
1119
* @param entityType the parametrized type. Must not be {@literal null}.
@@ -1141,8 +1137,8 @@ default <T> T findAndReplace(Query query, T replacement, FindAndReplaceOptions o
1141
1137
* taking {@link FindAndReplaceOptions} into account.<br />
1142
1138
* <strong>NOTE:</strong> The replacement entity must not hold an {@literal id}.
1143
1139
*
1144
- * @param query the {@link Query} class that specifies the {@link Criteria} used to find a document and also an optional
1145
- * fields specification. Must not be {@literal null}.
1140
+ * @param query the {@link Query} class that specifies the {@link Criteria} used to find a document and also an
1141
+ * optional fields specification. Must not be {@literal null}.
1146
1142
* @param replacement the replacement document. Must not be {@literal null}.
1147
1143
* @param options the {@link FindAndModifyOptions} holding additional information. Must not be {@literal null}.
1148
1144
* @param entityType the type used for mapping the {@link Query} to domain type fields and deriving the collection
@@ -1171,8 +1167,8 @@ default <S, T> T findAndReplace(Query query, S replacement, FindAndReplaceOption
1171
1167
* taking {@link FindAndReplaceOptions} into account.<br />
1172
1168
* <strong>NOTE:</strong> The replacement entity must not hold an {@literal id}.
1173
1169
*
1174
- * @param query the {@link Query} class that specifies the {@link Criteria} used to find a document and also an optional
1175
- * fields specification. Must not be {@literal null}.
1170
+ * @param query the {@link Query} class that specifies the {@link Criteria} used to find a document and also an
1171
+ * optional fields specification. Must not be {@literal null}.
1176
1172
* @param replacement the replacement document. Must not be {@literal null}.
1177
1173
* @param options the {@link FindAndModifyOptions} holding additional information. Must not be {@literal null}.
1178
1174
* @param entityType the type used for mapping the {@link Query} to domain type fields. Must not be {@literal null}.
@@ -1680,7 +1676,8 @@ default long exactCount(Query query, String collectionName) {
1680
1676
* acknowledged} remove operation was successful or not.
1681
1677
*
1682
1678
* @param object must not be {@literal null}.
1683
- * @param collectionName name of the collection where the documents will be removed from, must not be {@literal null} or empty.
1679
+ * @param collectionName name of the collection where the documents will be removed from, must not be {@literal null}
1680
+ * or empty.
1684
1681
* @return the {@link DeleteResult} which lets you access the results of the previous delete.
1685
1682
*/
1686
1683
DeleteResult remove (Object object , String collectionName );
@@ -1704,7 +1701,8 @@ default long exactCount(Query query, String collectionName) {
1704
1701
*
1705
1702
* @param query the query document that specifies the criteria used to remove a document.
1706
1703
* @param entityClass class of the pojo to be operated on. Can be {@literal null}.
1707
- * @param collectionName name of the collection where the documents will be removed from, must not be {@literal null} or empty.
1704
+ * @param collectionName name of the collection where the documents will be removed from, must not be {@literal null}
1705
+ * or empty.
1708
1706
* @return the {@link DeleteResult} which lets you access the results of the previous delete.
1709
1707
* @throws IllegalArgumentException when {@literal query}, {@literal entityClass} or {@literal collectionName} is
1710
1708
* {@literal null}.
@@ -1718,7 +1716,8 @@ default long exactCount(Query query, String collectionName) {
1718
1716
* information. Use {@link #remove(Query, Class, String)} to get full type specific support.
1719
1717
*
1720
1718
* @param query the query document that specifies the criteria used to remove a document.
1721
- * @param collectionName name of the collection where the documents will be removed from, must not be {@literal null} or empty.
1719
+ * @param collectionName name of the collection where the documents will be removed from, must not be {@literal null}
1720
+ * or empty.
1722
1721
* @return the {@link DeleteResult} which lets you access the results of the previous delete.
1723
1722
* @throws IllegalArgumentException when {@literal query} or {@literal collectionName} is {@literal null}.
1724
1723
*/
@@ -1730,7 +1729,8 @@ default long exactCount(Query query, String collectionName) {
1730
1729
* information. Use {@link #findAllAndRemove(Query, Class, String)} to get full type specific support.
1731
1730
*
1732
1731
* @param query the query document that specifies the criteria used to find and remove documents.
1733
- * @param collectionName name of the collection where the documents will be removed from, must not be {@literal null} or empty.
1732
+ * @param collectionName name of the collection where the documents will be removed from, must not be {@literal null}
1733
+ * or empty.
1734
1734
* @return the {@link List} converted objects deleted by this operation.
1735
1735
* @since 1.5
1736
1736
*/
@@ -1755,7 +1755,8 @@ default long exactCount(Query query, String collectionName) {
1755
1755
*
1756
1756
* @param query the query document that specifies the criteria used to find and remove documents.
1757
1757
* @param entityClass class of the pojo to be operated on.
1758
- * @param collectionName name of the collection where the documents will be removed from, must not be {@literal null} or empty.
1758
+ * @param collectionName name of the collection where the documents will be removed from, must not be {@literal null}
1759
+ * or empty.
1759
1760
* @return the {@link List} converted objects deleted by this operation.
1760
1761
* @since 1.5
1761
1762
*/
0 commit comments