Skip to content

Commit 988ead4

Browse files
authored
Refactor buffer cleaning in InternalStreamConnection.sendAndReceive (#884)
1 parent d89ed7e commit 988ead4

File tree

1 file changed

+7
-12
lines changed

1 file changed

+7
-12
lines changed

driver-core/src/main/com/mongodb/internal/connection/InternalStreamConnection.java

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -319,23 +319,18 @@ public boolean isClosed() {
319319
@Override
320320
public <T> T sendAndReceive(final CommandMessage message, final Decoder<T> decoder, final SessionContext sessionContext,
321321
final RequestContext requestContext) {
322-
ByteBufferBsonOutput bsonOutput = new ByteBufferBsonOutput(this);
323322
CommandEventSender commandEventSender;
324323

325-
try {
324+
try (ByteBufferBsonOutput bsonOutput = new ByteBufferBsonOutput(this)) {
326325
message.encode(bsonOutput, sessionContext);
327326
commandEventSender = createCommandEventSender(message, bsonOutput, requestContext);
328327
commandEventSender.sendStartedEvent();
329-
} catch (RuntimeException e) {
330-
bsonOutput.close();
331-
throw e;
332-
}
333-
334-
try {
335-
sendCommandMessage(message, bsonOutput, sessionContext);
336-
} catch (RuntimeException e) {
337-
commandEventSender.sendFailedEvent(e);
338-
throw e;
328+
try {
329+
sendCommandMessage(message, bsonOutput, sessionContext);
330+
} catch (RuntimeException e) {
331+
commandEventSender.sendFailedEvent(e);
332+
throw e;
333+
}
339334
}
340335

341336
if (message.isResponseExpected()) {

0 commit comments

Comments
 (0)