Skip to content
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

NamedParameterJdbcTemplate throws unexpected exception for null query #31391

Closed
mdeinum opened this issue Oct 9, 2023 · 0 comments
Closed

NamedParameterJdbcTemplate throws unexpected exception for null query #31391

mdeinum opened this issue Oct 9, 2023 · 0 comments
Assignees
Labels
in: data Issues in data modules (jdbc, orm, oxm, tx) status: backported An issue that has been backported to maintenance branches type: bug A general bug
Milestone

Comments

@mdeinum
Copy link
Contributor

mdeinum commented Oct 9, 2023

I was answering a question on StackOverflow which stated that the NamedParameterJdbcTemplate won't throw an exception when the query is null.

Whilst creating a test for this I did notice that an exception was being thrown but not the exception that I expected. I was expecting an IllegalArgumentException from the NamedParameterUtils with a proper error message stating the SQL cannot be null.

Instead however I was greeted with an exception from the ConcurrentLruCache.

java.lang.NullPointerException: Cannot invoke "Object.hashCode()" because "key" is null

	at java.base/java.util.concurrent.ConcurrentHashMap.get(ConcurrentHashMap.java:936)
	at org.springframework.util.ConcurrentLruCache.get(ConcurrentLruCache.java:101)
	at org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate.getParsedSql(NamedParameterJdbcTemplate.java:483)
	at org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate.batchUpdate(NamedParameterJdbcTemplate.java:379)
	at org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate.batchUpdate(NamedParameterJdbcTemplate.java:370)
	at com.apress.springboot3recipes.jdbc.JdbcCustomerRepositoryTest.insertNewCustomer(JdbcCustomerRepositoryTest.java:42)
	at java.base/java.lang.reflect.Method.invoke(Method.java:580)
	at java.base/java.util.ArrayList.forEach(ArrayList.java:1596)
	at java.base/java.util.ArrayList.forEach(ArrayList.java:1596)

Stating that the key cannot be null.

While an exception is still being thrown (and it is a NullPointerException) it does feel like the wrong exception here. Looks like a regression from the introduction/rewrite of the ConcurrentLruCache. It might be better to copy (or move?) the check from the NamedParameterUtils as a fail fast to the NamedParameterJdbcTemplate instead.

Spring Version 6.1.0-SNAPSHOT but probably applies to earlier versions as well.

@mdeinum mdeinum changed the title NamedParameterJdbcTemplate NamedParameterJdbcTemplate throws unexpected exception. Oct 9, 2023
@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Oct 9, 2023
@sbrannen sbrannen added the in: data Issues in data modules (jdbc, orm, oxm, tx) label Oct 9, 2023
@sbrannen sbrannen changed the title NamedParameterJdbcTemplate throws unexpected exception. NamedParameterJdbcTemplate throws unexpected exception for null query Oct 9, 2023
@jhoeller jhoeller added type: bug A general bug and removed status: waiting-for-triage An issue we've not yet triaged or decided on labels Oct 10, 2023
@jhoeller jhoeller self-assigned this Oct 10, 2023
@jhoeller jhoeller added this to the 6.0.13 milestone Oct 10, 2023
@github-actions github-actions bot added status: backported An issue that has been backported to maintenance branches and removed for: backport-to-5.3.x labels Oct 10, 2023
jhoeller added a commit that referenced this issue Oct 10, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: data Issues in data modules (jdbc, orm, oxm, tx) status: backported An issue that has been backported to maintenance branches type: bug A general bug
Projects
None yet
Development

No branches or pull requests

4 participants