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

JdbcTemplate does not call handleWarnings in case of exception #23106

Closed
jMediaConverter opened this issue Jun 10, 2019 · 7 comments
Closed

JdbcTemplate does not call handleWarnings in case of exception #23106

jMediaConverter opened this issue Jun 10, 2019 · 7 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: enhancement A general enhancement
Milestone

Comments

@jMediaConverter
Copy link

Affects: 4.2.4

The JdbcTemplate class allows statement warnings to be processed, see the JdbcTemplate# handleWarnings(Statement) method. However, the warnings are only processed on successful execution of the statement, though warnings may be generated even if the statement fails.

See the methods:
execute(StatementCallback)
execute(PreparedStatementCreator , PreparedStatementCallback)
execute(CallableStatementCreator , CallableStatementCallback )

All these should probably call handleWarnings in their catch block before closing the statement.

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Jun 10, 2019
@jhoeller jhoeller self-assigned this Jun 10, 2019
@jhoeller jhoeller added in: core Issues in core modules (aop, beans, core, context, expression) in: data Issues in data modules (jdbc, orm, oxm, tx) and removed status: waiting-for-triage An issue we've not yet triaged or decided on labels Jun 10, 2019
@jhoeller jhoeller added this to the 5.2 M3 milestone Jun 10, 2019
@jhoeller jhoeller added type: enhancement A general enhancement and removed in: core Issues in core modules (aop, beans, core, context, expression) labels Jun 11, 2019
@jhoeller jhoeller modified the milestones: 5.2 M3, 5.x Backlog Jun 11, 2019
@gabbagnale
Copy link

I think that might be enough move the call to handleWarnings inside finally instead that in the try. or double it inside the catch

@jMediaConverter
Copy link
Author

I think that might be enough move the call to handleWarnings inside finally instead that in the try. or double it inside the catch

That's right.

@jhoeller
Copy link
Contributor

jhoeller commented Jul 9, 2023

There are only really two modes of operation for handleWarnings: either log a debug message when ignoreWarnings=true (the default), or otherwise throw a SQLWarningException. This makes sense for successful statements but I wonder what kind of behavior you'd expect for failed statements where a primary exception is about to be thrown. Overriding such a primary exception with a SQLWarningException is awkward, so we can't simply call handleWarnings from the catch block there. We could only really call it with a nested catch (SQLWarningException) block that logs the warning, or with a new variant of handleWarnings which always logs a warn message when ignoreWarnings=false (but never throws an exception).

@jMediaConverter
Copy link
Author

Let me explain my exact use case. I am calling a stored procedure using JdbcTemplate, that stored procedure creates multiple warnings and then it fails for whatever reason (SqlException is thrown). In this scenario there is no way for me to access/log the warnings that the stored procedure generated because handleWarnings is called only on successful execution of the stored procedure. It could potentially be called in the exception block or in the finally. In my project I extended the JdbcTemplate and overrode the execute methods and created my custom warnings handling mechanism. However, I do think it would be nice for JdbcTemplate to do this out of the box.
I do see your point about the second mode of operation of handleWarnings with SQLWarningException hiding the primary exception. Maybe SqlWarningException could take the primary exception as the cause.

@sbrannen sbrannen changed the title JdbcTemplate does not call handleWarnings in case of exception. JdbcTemplate does not call handleWarnings in case of exception Jul 10, 2023
@jhoeller
Copy link
Contributor

It looks like we can add the SQLWarning from the SQLWarningException to the primary SQLException as a chained exception, through the JDBC 4 SQLException.setNextException method. So doing that in case of ignoreWarnings=false seems like a sensible arrangement, whereas the default ignoreWarnings=true behavior would be a no-op (just potentially with debug logging). In any case, you'd get the handleWarnings(Statement) invocation in the exception case as well, so any custom logic in an overridden method there would get invoked.

@jMediaConverter
Copy link
Author

Sounds like a good plan. Thanks.

@jhoeller jhoeller modified the milestones: 6.x Backlog, 6.0.11 Jul 10, 2023
@jhoeller jhoeller added the for: backport-to-5.3.x Marks an issue as a candidate for backport to 5.3.x label Jul 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 Marks an issue as a candidate for backport to 5.3.x labels Jul 10, 2023
@jhoeller
Copy link
Contributor

Thanks for quick turnaround! Rolling this into 6.0.11 and backporting it to 5.3.29, both to be released on Thursday.

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: enhancement A general enhancement
Projects
None yet
Development

No branches or pull requests

4 participants