Details
-
Improvement
-
Resolution: Won't Fix
-
Major - P3
-
None
-
legacy-1.1.0
-
None
-
None
Description
While reading from a replica set and the primary node stepped down, the cursor may return a NULL pointer.
Example code (legacy driver 1.1.0) :
mongo::DBClientReplicaSet connection("replicaName", hosts, 0);
|
const bool connected = connection.connect();
|
|
for( int i=0; i<1000; ++i)
|
{
|
mongo::BSONObj query = BSON( "fieldA" << i );
|
mongo::Query mQuery = mongo::Query( query );
|
mongo::BSONArray bsonTags;
|
mQuery.readPref( mongo::ReadPreference_PrimaryPreferred, bsonTags );
|
std::auto_ptr<mongo::DBClientCursor> cursor = connection.query( "database.collection",
|
mQuery,
|
0, 0, 0,
|
mongo::QueryOption_SlaveOk, 0 );
|
// If there has been an election in the replica set, the cursor could be NULL.
|
if (cursor.get() != NULL){
|
while(cursor->more()){
|
mongo::BSONObj p = cursor->next();
|
std::cout << p.getStringField("fieldB")<< std::endl;
|
}
|
}
|
else {
|
std::cout<< "cursor is NULL, handle error" <<std::endl;
|
}
|
}
|