23
23
import com .mongodb .binding .AsyncConnectionSource ;
24
24
import com .mongodb .binding .AsyncReadBinding ;
25
25
import com .mongodb .binding .AsyncReadWriteBinding ;
26
+ import com .mongodb .binding .AsyncSessionBinding ;
26
27
import com .mongodb .binding .AsyncSingleConnectionBinding ;
27
28
import com .mongodb .binding .AsyncWriteBinding ;
28
29
import com .mongodb .binding .ClusterBinding ;
29
30
import com .mongodb .binding .ReadWriteBinding ;
31
+ import com .mongodb .binding .SessionBinding ;
30
32
import com .mongodb .binding .SingleConnectionBinding ;
31
33
import com .mongodb .connection .AsyncConnection ;
32
34
import com .mongodb .connection .AsynchronousSocketChannelStreamFactory ;
62
64
63
65
import java .util .ArrayList ;
64
66
import java .util .Collections ;
67
+ import java .util .HashMap ;
65
68
import java .util .List ;
66
69
import java .util .Map ;
67
70
@@ -90,6 +93,8 @@ public final class ClusterFixture {
90
93
private static ConnectionString connectionString ;
91
94
private static Cluster cluster ;
92
95
private static Cluster asyncCluster ;
96
+ private static Map <ReadPreference , ReadWriteBinding > bindingMap = new HashMap <ReadPreference , ReadWriteBinding >();
97
+ private static Map <ReadPreference , AsyncReadWriteBinding > asyncBindingMap = new HashMap <ReadPreference , AsyncReadWriteBinding >();
93
98
94
99
static {
95
100
String mongoURIProperty = System .getProperty (MONGODB_URI_SYSTEM_PROPERTY_NAME );
@@ -201,20 +206,27 @@ public static synchronized ConnectionString getConnectionString() {
201
206
return connectionString ;
202
207
}
203
208
209
+ public static ReadWriteBinding getBinding (final Cluster cluster ) {
210
+ return new ClusterBinding (cluster , ReadPreference .primary ());
211
+ }
212
+
204
213
public static ReadWriteBinding getBinding () {
205
- return getBinding (getCluster ());
214
+ return getBinding (getCluster (), ReadPreference . primary () );
206
215
}
207
216
208
217
public static ReadWriteBinding getBinding (final ReadPreference readPreference ) {
209
218
return getBinding (getCluster (), readPreference );
210
219
}
211
220
212
- public static ReadWriteBinding getBinding (final Cluster cluster ) {
213
- return getBinding (cluster , ReadPreference .primary ());
214
- }
215
-
216
221
private static ReadWriteBinding getBinding (final Cluster cluster , final ReadPreference readPreference ) {
217
- return new ClusterBinding (cluster , readPreference );
222
+ if (!bindingMap .containsKey (readPreference )) {
223
+ ReadWriteBinding binding = new ClusterBinding (cluster , readPreference );
224
+ if (serverVersionAtLeast (3 , 6 )) {
225
+ binding = new SessionBinding (binding );
226
+ }
227
+ bindingMap .put (readPreference , binding );
228
+ }
229
+ return bindingMap .get (readPreference );
218
230
}
219
231
220
232
public static SingleConnectionBinding getSingleConnectionBinding () {
@@ -229,20 +241,27 @@ public static AsyncSingleConnectionBinding getAsyncSingleConnectionBinding(final
229
241
return new AsyncSingleConnectionBinding (cluster , 20 , SECONDS );
230
242
}
231
243
244
+ public static AsyncReadWriteBinding getAsyncBinding (final Cluster cluster ) {
245
+ return new AsyncClusterBinding (cluster , ReadPreference .primary ());
246
+ }
247
+
232
248
public static AsyncReadWriteBinding getAsyncBinding () {
233
- return getAsyncBinding (getAsyncCluster ());
249
+ return getAsyncBinding (getAsyncCluster (), ReadPreference . primary () );
234
250
}
235
251
236
252
public static AsyncReadWriteBinding getAsyncBinding (final ReadPreference readPreference ) {
237
253
return getAsyncBinding (getAsyncCluster (), readPreference );
238
254
}
239
255
240
- public static AsyncReadWriteBinding getAsyncBinding (final Cluster cluster ) {
241
- return getAsyncBinding (cluster , ReadPreference .primary ());
242
- }
243
-
244
256
public static AsyncReadWriteBinding getAsyncBinding (final Cluster cluster , final ReadPreference readPreference ) {
245
- return new AsyncClusterBinding (cluster , readPreference );
257
+ if (!asyncBindingMap .containsKey (readPreference )) {
258
+ AsyncReadWriteBinding binding = new AsyncClusterBinding (cluster , readPreference );
259
+ if (serverVersionAtLeast (3 , 6 )) {
260
+ binding = new AsyncSessionBinding (binding );
261
+ }
262
+ asyncBindingMap .put (readPreference , binding );
263
+ }
264
+ return asyncBindingMap .get (readPreference );
246
265
}
247
266
248
267
public static synchronized Cluster getCluster () {
0 commit comments