@@ -94,7 +94,13 @@ public void close() {
94
94
try {
95
95
closing = true ;
96
96
serverSessionPool .close ();
97
- endClosedSessions ();
97
+
98
+ List <BsonDocument > identifiers ;
99
+ synchronized (this ) {
100
+ identifiers = new ArrayList <BsonDocument >(closedSessionIdentifiers );
101
+ closedSessionIdentifiers .clear ();
102
+ }
103
+ endClosedSessions (identifiers );
98
104
} finally {
99
105
closed = true ;
100
106
}
@@ -111,14 +117,21 @@ private void closeSession(final ServerSessionImpl serverSession) {
111
117
return ;
112
118
}
113
119
114
- closedSessionIdentifiers .add (serverSession .getIdentifier ());
115
- if (closedSessionIdentifiers .size () == END_SESSIONS_BATCH_SIZE ) {
116
- endClosedSessions ();
120
+ List <BsonDocument > identifiers = null ;
121
+ synchronized (this ) {
122
+ closedSessionIdentifiers .add (serverSession .getIdentifier ());
123
+ if (closedSessionIdentifiers .size () == END_SESSIONS_BATCH_SIZE ) {
124
+ identifiers = new ArrayList <BsonDocument >(closedSessionIdentifiers );
125
+ closedSessionIdentifiers .clear ();
126
+ }
127
+ }
128
+ if (identifiers != null ) {
129
+ endClosedSessions (identifiers );
117
130
}
118
131
}
119
132
120
- private void endClosedSessions () {
121
- if (closedSessionIdentifiers .isEmpty ()) {
133
+ private void endClosedSessions (final List < BsonDocument > identifiers ) {
134
+ if (identifiers .isEmpty ()) {
122
135
return ;
123
136
}
124
137
@@ -141,12 +154,11 @@ public List<ServerDescription> select(final ClusterDescription clusterDescriptio
141
154
}).getConnection ();
142
155
try {
143
156
connection .command ("admin" ,
144
- new BsonDocument ("endSessions" , new BsonArray (closedSessionIdentifiers )), new NoOpFieldNameValidator (),
157
+ new BsonDocument ("endSessions" , new BsonArray (identifiers )), new NoOpFieldNameValidator (),
145
158
ReadPreference .primaryPreferred (), new BsonDocumentCodec (), NoOpSessionContext .INSTANCE );
146
159
} catch (MongoException e ) {
147
160
// ignore exceptions
148
161
} finally {
149
- closedSessionIdentifiers .clear ();
150
162
connection .release ();
151
163
}
152
164
}
0 commit comments