2323import com .mongodb .binding .AsyncConnectionSource ;
2424import com .mongodb .binding .AsyncReadBinding ;
2525import com .mongodb .binding .AsyncReadWriteBinding ;
26+ import com .mongodb .binding .AsyncSessionBinding ;
2627import com .mongodb .binding .AsyncSingleConnectionBinding ;
2728import com .mongodb .binding .AsyncWriteBinding ;
2829import com .mongodb .binding .ClusterBinding ;
2930import com .mongodb .binding .ReadWriteBinding ;
31+ import com .mongodb .binding .SessionBinding ;
3032import com .mongodb .binding .SingleConnectionBinding ;
3133import com .mongodb .connection .AsyncConnection ;
3234import com .mongodb .connection .AsynchronousSocketChannelStreamFactory ;
6264
6365import java .util .ArrayList ;
6466import java .util .Collections ;
67+ import java .util .HashMap ;
6568import java .util .List ;
6669import java .util .Map ;
6770
@@ -90,6 +93,8 @@ public final class ClusterFixture {
9093 private static ConnectionString connectionString ;
9194 private static Cluster cluster ;
9295 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 >();
9398
9499 static {
95100 String mongoURIProperty = System .getProperty (MONGODB_URI_SYSTEM_PROPERTY_NAME );
@@ -201,20 +206,27 @@ public static synchronized ConnectionString getConnectionString() {
201206 return connectionString ;
202207 }
203208
209+ public static ReadWriteBinding getBinding (final Cluster cluster ) {
210+ return new ClusterBinding (cluster , ReadPreference .primary ());
211+ }
212+
204213 public static ReadWriteBinding getBinding () {
205- return getBinding (getCluster ());
214+ return getBinding (getCluster (), ReadPreference . primary () );
206215 }
207216
208217 public static ReadWriteBinding getBinding (final ReadPreference readPreference ) {
209218 return getBinding (getCluster (), readPreference );
210219 }
211220
212- public static ReadWriteBinding getBinding (final Cluster cluster ) {
213- return getBinding (cluster , ReadPreference .primary ());
214- }
215-
216221 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 );
218230 }
219231
220232 public static SingleConnectionBinding getSingleConnectionBinding () {
@@ -229,20 +241,27 @@ public static AsyncSingleConnectionBinding getAsyncSingleConnectionBinding(final
229241 return new AsyncSingleConnectionBinding (cluster , 20 , SECONDS );
230242 }
231243
244+ public static AsyncReadWriteBinding getAsyncBinding (final Cluster cluster ) {
245+ return new AsyncClusterBinding (cluster , ReadPreference .primary ());
246+ }
247+
232248 public static AsyncReadWriteBinding getAsyncBinding () {
233- return getAsyncBinding (getAsyncCluster ());
249+ return getAsyncBinding (getAsyncCluster (), ReadPreference . primary () );
234250 }
235251
236252 public static AsyncReadWriteBinding getAsyncBinding (final ReadPreference readPreference ) {
237253 return getAsyncBinding (getAsyncCluster (), readPreference );
238254 }
239255
240- public static AsyncReadWriteBinding getAsyncBinding (final Cluster cluster ) {
241- return getAsyncBinding (cluster , ReadPreference .primary ());
242- }
243-
244256 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 );
246265 }
247266
248267 public static synchronized Cluster getCluster () {
0 commit comments