Skip to content

Commit 431c6e5

Browse files
committed
JAVA-2821: When killing a cursor, exceptions are supposed to be suppressed. The suppression now includes the attempt to get a connection on which to kill the cursor, which may also fail (e.g. if the server has been killed)
1 parent b0d0ed4 commit 431c6e5

File tree

2 files changed

+33
-4
lines changed

2 files changed

+33
-4
lines changed

driver-core/src/main/com/mongodb/operation/QueryBatchCursor.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -277,13 +277,15 @@ private boolean limitReached() {
277277

278278
private void killCursor() {
279279
if (serverCursor != null) {
280-
Connection connection = connectionSource.getConnection();
281280
try {
282-
killCursor(connection);
281+
Connection connection = connectionSource.getConnection();
282+
try {
283+
killCursor(connection);
284+
} finally {
285+
connection.release();
286+
}
283287
} catch (MongoException e) {
284288
// Ignore exceptions from calling killCursor
285-
} finally {
286-
connection.release();
287289
}
288290
}
289291
}

driver-core/src/test/unit/com/mongodb/operation/QueryBatchCursorSpecification.groovy

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package com.mongodb.operation
1818

1919
import com.mongodb.MongoNamespace
2020
import com.mongodb.MongoSocketException
21+
import com.mongodb.MongoSocketOpenException
2122
import com.mongodb.ServerAddress
2223
import com.mongodb.binding.ConnectionSource
2324
import com.mongodb.connection.Connection
@@ -115,4 +116,30 @@ class QueryBatchCursorSpecification extends Specification {
115116
then:
116117
notThrown(Exception)
117118
}
119+
120+
def 'should handle exceptions when killing cursor and a connection can not be obtained'() {
121+
given:
122+
def serverAddress = new ServerAddress()
123+
def connection = Mock(Connection)
124+
def connectionSource = Stub(ConnectionSource) {
125+
getConnection() >> { throw new MongoSocketOpenException("can't open socket", serverAddress, new IOException()) }
126+
}
127+
connectionSource.retain() >> connectionSource
128+
129+
def namespace = new MongoNamespace('test', 'QueryBatchCursorSpecification')
130+
def firstBatch = new QueryResult(namespace, [], 42, serverAddress)
131+
def cursor = new QueryBatchCursor<Document>(firstBatch, 0, 2, 100, new BsonDocumentCodec(), connectionSource, connection)
132+
133+
when:
134+
cursor.close()
135+
136+
then:
137+
notThrown(MongoSocketException)
138+
139+
when:
140+
cursor.close()
141+
142+
then:
143+
notThrown(Exception)
144+
}
118145
}

0 commit comments

Comments
 (0)