|
19 | 19 | import com.mongodb.MongoException;
|
20 | 20 | import com.mongodb.ReadPreference;
|
21 | 21 | import com.mongodb.connection.Cluster;
|
| 22 | +import com.mongodb.connection.ClusterDescription; |
22 | 23 | import com.mongodb.connection.Connection;
|
| 24 | +import com.mongodb.connection.ServerDescription; |
23 | 25 | import com.mongodb.internal.connection.ConcurrentPool;
|
24 | 26 | import com.mongodb.internal.connection.ConcurrentPool.Prune;
|
25 | 27 | import com.mongodb.internal.connection.NoOpSessionContext;
|
26 | 28 | import com.mongodb.internal.validator.NoOpFieldNameValidator;
|
27 | 29 | import com.mongodb.selector.ReadPreferenceServerSelector;
|
| 30 | +import com.mongodb.selector.ServerSelector; |
28 | 31 | import com.mongodb.session.ServerSession;
|
29 | 32 | import org.bson.BsonArray;
|
30 | 33 | import org.bson.BsonBinary;
|
|
36 | 39 | import org.bson.codecs.UuidCodec;
|
37 | 40 |
|
38 | 41 | import java.util.ArrayList;
|
| 42 | +import java.util.Collections; |
39 | 43 | import java.util.List;
|
40 | 44 | import java.util.UUID;
|
41 | 45 |
|
@@ -118,7 +122,23 @@ private void endClosedSessions() {
|
118 | 122 | return;
|
119 | 123 | }
|
120 | 124 |
|
121 |
| - Connection connection = cluster.selectServer(new ReadPreferenceServerSelector(ReadPreference.primaryPreferred())).getConnection(); |
| 125 | + final List<ServerDescription> primaryPreferred = new ReadPreferenceServerSelector(ReadPreference.primaryPreferred()) |
| 126 | + .select(cluster.getCurrentDescription()); |
| 127 | + if (primaryPreferred.isEmpty()) { |
| 128 | + return; |
| 129 | + } |
| 130 | + |
| 131 | + Connection connection = cluster.selectServer(new ServerSelector() { |
| 132 | + @Override |
| 133 | + public List<ServerDescription> select(final ClusterDescription clusterDescription) { |
| 134 | + for (ServerDescription cur : clusterDescription.getServerDescriptions()) { |
| 135 | + if (cur.getAddress().equals(primaryPreferred.get(0).getAddress())) { |
| 136 | + return Collections.singletonList(cur); |
| 137 | + } |
| 138 | + } |
| 139 | + return Collections.emptyList(); |
| 140 | + } |
| 141 | + }).getConnection(); |
122 | 142 | try {
|
123 | 143 | connection.command("admin",
|
124 | 144 | new BsonDocument("endSessions", new BsonArray(closedSessionIdentifiers)), new NoOpFieldNameValidator(),
|
|
0 commit comments