4

I'm using Spring Boot with Mongo 3.4 (in cluster with MongoS) The mongo client options configuration has the option cursorFinalizerEnabled.

According to documentation, this flag allows to:

Mongo Template closes the cursors. Making this true, spawns a thread on every new MongoClient.

Attempts to clean up DBCursors that are not closed.

MongoClientOptions options = MongoClientOptions.builder()
    .cursorFinalizerEnabled(false)
    .build();

What is the best practice? true or false? performance effect?

Community
  • 1
  • 1
Leonel
  • 2,796
  • 5
  • 25
  • 36

1 Answers1

3

The default value of cursorFinalizerEnabled is true (see MongoClientOptions). So, your MongoClient will spawn this thread (and apply this behaviour) unless you choose not to.

This feature provides a safety net for client code which is (or might be) casual about handling cursors. So, depending on how you treat your cursors it might be useful or it might be a no-op.

The standard advice is: if your client code ensures that the close method of DBCursor is always invoked then you can set this to false. Otherwise, just accept the default.

As for the performance implications; it's hard to measure that. If your client code does not leave any open, unused cursors then it's a no-op but if your client code does leave open, unused cursors then this flag will help to reduce the impact on shared resources. Spawning a single thread to run this harvester seems like a low cost so if you are at all unsure about how your client code handles cursors then it's worth enabling it.

And, of course, as with all performance questions; the most reliable way of detemining the performance effect (if any) is to test with and without this flag and then compare :)

glytching
  • 44,936
  • 9
  • 114
  • 120