[JAVA-2968] Infinite loop on MongoCursor initiated from ChangeStream Created: 10/Sep/18 Updated: 11/Sep/19 Resolved: 18/Oct/18 |
|
| Status: | Closed |
| Project: | Java Driver |
| Component/s: | Query Operations |
| Affects Version/s: | 3.6.4 |
| Fix Version/s: | None |
| Type: | Task | Priority: | Minor - P4 |
| Reporter: | Roman Golovan | Assignee: | Unassigned |
| Resolution: | Done | Votes: | 0 |
| Labels: | None | ||
| Remaining Estimate: | Not Specified | ||
| Time Spent: | Not Specified | ||
| Original Estimate: | Not Specified | ||
| 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.
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.
Please, advise what could be done in this case and whether my code should work as expected, but has a bug. |
| Comments |
| Comment by Jeffrey Yemin [ 18/Oct/18 ] | |||||||||
|
You can use MongoCursor#tryNext. Here's an example.
I changed your code up a bit to reflect the fact that the watch method returns a MongoCursor<ChangeStreamDocument<T>, as well as a check for the change stream operation type. Just to let you know this project is for Java driver bugs or feature requests. The best place for questions regarding MongoDB usage or the Java driver specifics is the mongodb-user mailinglist or stackoverflow as you will reach a broader audience there. If your business requires an answer from MongoDB within a time frame then we do offer production support. If you do follow up via one of the options above please post a link and I will follow the conversation there. |