Skip to content

Commit e3c837a

Browse files
committed
convenience method to access information about which server returned result.
Requested by user and useful to verify proper request routing.
1 parent 235338a commit e3c837a

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

src/main/com/mongodb/DBApiLayer.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -454,7 +454,10 @@ void close(){
454454
}
455455
}
456456
}
457-
457+
458+
public ServerAddress getServerAddress() {
459+
return _host;
460+
}
458461

459462
Response _curResult;
460463
Iterator<DBObject> _cur;

src/main/com/mongodb/DBCursor.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -645,6 +645,20 @@ public DBCollection getCollection(){
645645
return _collection;
646646
}
647647

648+
/**
649+
* Gets the Server Address of the server that data is pulled from.
650+
* Note that this information is not available if no data has been retrieved yet.
651+
* Availability is specific to underlying implementation and may vary.
652+
* @return
653+
*/
654+
public ServerAddress getServerAddress() {
655+
if (_it != null) {
656+
if (_it instanceof DBApiLayer.Result)
657+
return ((DBApiLayer.Result)_it).getServerAddress();
658+
}
659+
return null;
660+
}
661+
648662
@Override
649663
public String toString() {
650664
StringBuilder sb = new StringBuilder();
@@ -658,6 +672,9 @@ public String toString() {
658672
sb.append(", limit=").append(_numWanted);
659673
if (_skip > 0)
660674
sb.append(", skip=").append(_skip);
675+
ServerAddress addr = getServerAddress();
676+
if (addr != null)
677+
sb.append(", addr=").append(addr);
661678
return sb.toString();
662679
}
663680

0 commit comments

Comments
 (0)