Uploaded image for project: 'Java Driver'
  1. Java Driver
  2. JAVA-2968

Infinite loop on MongoCursor initiated from ChangeStream

    XMLWordPrintable

Details

    • Task
    • Status: Closed
    • Minor - P4
    • Resolution: Done
    • 3.6.4
    • None
    • Query Operations
    • None

    Description

      I would like to leverage from ChangeStream usage to organize versioning/audit in my application. ChangeStreamDocuments are to be intercepted after any of insert*/save*/delete* operations is called using Spring Data/Mongo Repository.

      The following code snippet is used to iterate over collection's ChangeStreamDocuments.

      try (MongoCursor<Document> cursor = collection.watch().iterator()) {
          while (cursor.hasNext()) {
              store(cursor.next());
          }
      }
      

      The issue is that this code sticks after the first operation (new entity insertion) if the DB is empty. An infinite loop is run insideĀ cursor.hasNext().

      I could only come up with an asynchronous solution which opens a persistent cursor against the DB and applies a Block function on each new incoming event. The biggest drawback of this solution is that each persistent cursor consumes a connection from the pool.

      Block<ChangeStreamDocument<Document>> persistBlock = changeStreamDocument -> store(changeStreamDocument);
       
      collection.watch().forEach(persistBlock);
         
      

      Please, advise what could be done in this case and whether my code should work as expected, but has a bug.

      Attachments

        Activity

          People

            Unassigned Unassigned
            xwandi Roman Golovan
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: