static <T> T withReleasableConnection(final WriteBinding binding, final MongoException connectionException, final CallableWithConnectionAndSource<T> callable) { ConnectionSource source = null; Connection connection; try { source = binding.getWriteConnectionSource(); connection = source.getConnection(); } catch (Throwable t){ if (source != null) { source.release(); } throw connectionException; } try { return callable.call(source, connection); } finally { source.release(); } }
@Override public ServerDescription getServerDescription() { return wrapped.getServerDescription(); }
@Override @SuppressWarnings("checkstyle:methodlength") public ConnectionSource retain() { wrapped = wrapped.retain(); return this; }
private static <T> T executeWrappedCommandProtocol(final String database, final BsonDocument command, final FieldNameValidator fieldNameValidator, final Decoder<T> decoder, final ConnectionSource source, final ReadPreference readPreference) { Connection connection = source.getConnection(); try { return executeWrappedCommandProtocol(database, command, fieldNameValidator, decoder, connection, readPreference, new IdentityTransformer<T>(), source.getSessionContext()); } finally { connection.release(); } }
static <D, T> T executeWrappedCommandProtocol(final WriteBinding binding, final String database, final BsonDocument command, final FieldNameValidator fieldNameValidator, final Decoder<D> decoder, final CommandTransformer<D, T> transformer) { ConnectionSource source = binding.getWriteConnectionSource(); try { return transformer.apply(executeWrappedCommandProtocol(database, command, fieldNameValidator, decoder, source, primary()), source.getServerDescription().getAddress()); } finally { source.release(); } }
private void getMore() { Connection connection = connectionSource.getConnection(); try { if (serverIsAtLeastVersionThreeDotTwo(connection.getDescription())) { try { initFromCommandResult(connection.command(namespace.getDatabaseName(), asGetMoreCommandDocument(), NO_OP_FIELD_NAME_VALIDATOR, ReadPreference.primary(), CommandResultDocumentCodec.create(decoder, "nextBatch"), connectionSource.getSessionContext())); } catch (MongoCommandException e) { throw translateCommandException(e, serverCursor); } } else { QueryResult<T> getMore = connection.getMore(namespace, serverCursor.getId(), getNumberToReturn(limit, batchSize, count), decoder); initFromQueryResult(getMore); } if (limitReached()) { killCursor(connection); } if (serverCursor == null) { this.connectionSource.release(); this.connectionSource = null; } } finally { connection.release(); } }
@Override public void release() { wrapped.release(); closeSessionIfCountIsZero(); } }
@Override public Connection getConnection() { return wrapped.getConnection(); }
QueryBatchCursor(final QueryResult<T> firstQueryResult, final int limit, final int batchSize, final long maxTimeMS, final Decoder<T> decoder, final ConnectionSource connectionSource, final Connection connection) { isTrueArgument("maxTimeMS >= 0", maxTimeMS >= 0); this.maxTimeMS = maxTimeMS; this.namespace = firstQueryResult.getNamespace(); this.serverAddress = firstQueryResult.getAddress(); this.limit = limit; this.batchSize = batchSize; this.decoder = notNull("decoder", decoder); if (firstQueryResult.getCursor() != null) { notNull("connectionSource", connectionSource); } if (connectionSource != null) { this.connectionSource = connectionSource.retain(); } else { this.connectionSource = null; } initFromQueryResult(firstQueryResult); if (limitReached()) { killCursor(connection); } if (serverCursor == null && this.connectionSource != null) { this.connectionSource.release(); this.connectionSource = null; } }
@Override public int getCount() { return wrapped.getCount(); }
private void killCursor(final Connection connection) { if (serverCursor != null) { notNull("connection", connection); if (serverIsAtLeastVersionThreeDotTwo(connection.getDescription())) { connection.command(namespace.getDatabaseName(), asKillCursorsCommandDocument(), NO_OP_FIELD_NAME_VALIDATOR, ReadPreference.primary(), new BsonDocumentCodec(), connectionSource.getSessionContext()); } else { connection.killCursor(namespace, singletonList(serverCursor.getId())); } serverCursor = null; } }
static <D, T> T executeWrappedCommandProtocol(final ReadBinding binding, final String database, final BsonDocument command, final Decoder<D> decoder, final CommandTransformer<D, T> transformer) { ConnectionSource source = binding.getReadConnectionSource(); try { return transformer.apply(executeWrappedCommandProtocol(database, command, decoder, source, binding.getReadPreference()), source.getServerDescription().getAddress()); } finally { source.release(); } }
private void getMore() { Connection connection = connectionSource.getConnection(); try { if (serverIsAtLeastVersionThreeDotTwo(connection.getDescription())) { try { initFromCommandResult(connection.command(namespace.getDatabaseName(), asGetMoreCommandDocument(), NO_OP_FIELD_NAME_VALIDATOR, ReadPreference.primary(), CommandResultDocumentCodec.create(decoder, "nextBatch"), connectionSource.getSessionContext())); } catch (MongoCommandException e) { throw translateCommandException(e, serverCursor); } } else { QueryResult<T> getMore = connection.getMore(namespace, serverCursor.getId(), getNumberToReturn(limit, batchSize, count), decoder); initFromQueryResult(getMore); } if (limitReached()) { killCursor(connection); } if (serverCursor == null) { this.connectionSource.release(); this.connectionSource = null; } } finally { connection.release(); } }
@Override public void close() { if (!closed) { closed = true; try { killCursor(); } finally { if (connectionSource != null) { connectionSource.release(); } } } }
static <T> T withConnectionSource(final ConnectionSource source, final CallableWithConnection<T> callable) { Connection connection = source.getConnection(); try { return callable.call(connection); } finally { connection.release(); } }
QueryBatchCursor(final QueryResult<T> firstQueryResult, final int limit, final int batchSize, final long maxTimeMS, final Decoder<T> decoder, final ConnectionSource connectionSource, final Connection connection) { isTrueArgument("maxTimeMS >= 0", maxTimeMS >= 0); this.maxTimeMS = maxTimeMS; this.namespace = firstQueryResult.getNamespace(); this.serverAddress = firstQueryResult.getAddress(); this.limit = limit; this.batchSize = batchSize; this.decoder = notNull("decoder", decoder); if (firstQueryResult.getCursor() != null) { notNull("connectionSource", connectionSource); } if (connectionSource != null) { this.connectionSource = connectionSource.retain(); } else { this.connectionSource = null; } initFromQueryResult(firstQueryResult); if (limitReached()) { killCursor(connection); } if (serverCursor == null && this.connectionSource != null) { this.connectionSource.release(); this.connectionSource = null; } }
private static <T> T executeWrappedCommandProtocol(final String database, final BsonDocument command, final FieldNameValidator fieldNameValidator, final Decoder<T> decoder, final ConnectionSource source, final ReadPreference readPreference) { Connection connection = source.getConnection(); try { return executeWrappedCommandProtocol(database, command, fieldNameValidator, decoder, connection, readPreference, new IdentityTransformer<T>(), source.getSessionContext()); } finally { connection.release(); } }
@Override public int getCount() { return wrapped.getCount(); }
private void killCursor(final Connection connection) { if (serverCursor != null) { notNull("connection", connection); if (serverIsAtLeastVersionThreeDotTwo(connection.getDescription())) { connection.command(namespace.getDatabaseName(), asKillCursorsCommandDocument(), NO_OP_FIELD_NAME_VALIDATOR, ReadPreference.primary(), new BsonDocumentCodec(), connectionSource.getSessionContext()); } else { connection.killCursor(namespace, singletonList(serverCursor.getId())); } serverCursor = null; } }
static <T> T withReleasableConnection(final WriteBinding binding, final CallableWithConnectionAndSource<T> callable) { ConnectionSource source = binding.getWriteConnectionSource(); try { return callable.call(source, source.getConnection()); } finally { source.release(); } }