Skip to content

Fix NullPointerException in MongoSequenceIncrementer #4811

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

blake-bauman
Copy link

When using a MongoDB repository for the first time, the collections used by MongoSequenceIncrementer don't yet exist, which results in the call to findOneAndUpdate().getLong() to reutrn a null, which then results in an NPE unboxing the Long into a long.

Adding upsert(true) creates the initial document with a count of 0 and immediately increments it to 1.

When using a MongoDB repository for the first time, the collections
used by MongoSequenceIncrementer don't yet exist, which results
in the call to findOneAndUpdate().getLong() to reutrn a null,
which then results in an NPE unboxing the Long into a long.

Adding upsert(true) creates the initial document with a
count of 0 and immediately increments it to 1.

Signed-off-by: blake_bauman <[email protected]>
@blake-bauman
Copy link
Author

Is this a valid PR? We aren't able to use MongoDB as a repository without manually creating these documents and it took some reverse engineering to figure out what to create.

@fmbenhassine
Copy link
Contributor

Is this a valid PR? We aren't able to use MongoDB as a repository without manually creating these documents and it took some reverse engineering to figure out what to create.

Similar to the JDBC job repository configuration, it is expected to execute this script in MongoDB before running any job. There is a note about that in the what's new section, but I believe this is not clear enough and we need to update the reference docs with this prerequisite in the job repository configuration section as well. I created #4859 for that.

With that script, there is no need for the upsert flag added in this PR (this test, which initializes the database with that script, passes without the upset flag), so I guess this PR is indeed not valid. Do you agree?

@fmbenhassine fmbenhassine added the status: waiting-for-reporter Issues for which we are waiting for feedback from the reporter label Jun 3, 2025
@blake-bauman
Copy link
Author

I can see that you'd want to ensure that the indexes get created, so perhaps upsert isn't the right option after all. I appreciate the documentation change. I didn't think of looking in "what's new" since I just went straight to the reference guide.

The big problem, I think is that if the collection is not available, there is an NPE being thrown with no explanation, which could be an issue for those that didn't realize they missed a step. A couple of suggestions I would have is:

  • Detecting on startup if a required collection isn't available and failing with an informative message.
  • Or.....One of the benefits of MongoDB over JDBC is the ability to create these collections with the right attributes on the fly. So, a nice enhancement would be to detect that the collection is not created and automatically create it with the expected attributes before starting the job.

@fmbenhassine fmbenhassine added status: feedback-provided Issues for which the feedback requested from the reporter was provided and removed status: waiting-for-reporter Issues for which we are waiting for feedback from the reporter labels Jun 7, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: feedback-provided Issues for which the feedback requested from the reporter was provided
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants