-
Notifications
You must be signed in to change notification settings - Fork 38.3k
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
Provide access to all counters in case of BatchUpdateException with multiple batches #23867
Comments
If anyone is coming here for the work around (I am using spring-jdbc-5.3.29.jar, oracle8-19.3.0.0.jar, Oracle 19, JDK 8) and has the "second issue" (batch size < collection size), then the above sample needs to be adapted. I am trying to insert ~40,000 records and Oracle is throwing |
I don't know if everything in this issue is still current.
This no longer seems to be the case.
I have three proposals how this could be solved:
|
Playing a bit with the example above, we throw a I think a try/catch here could be useful: spring-framework/spring-jdbc/src/main/java/org/springframework/jdbc/core/JdbcTemplate.java Line 1119 in 94e2bef
We could then throw an exception that contains something built with I've moved the sample above to a test case, see snicoll@b90368b WDYT @jhoeller? |
Maybe we could replace the original |
@asollberger, @marschall, and anyone else interested, I've pushed a proposal for this see snicoll@bda23b4. Although I am not a huge fan of extending We'd like some feedback before moving on. |
@snicoll I had a short look at it. I had encountered some problems when reviewing the code
Regarding the code itself:
|
Thanks for the feedback but let's please focus on the task at hand, I don't know why you can't build framework at the moment, I am not aware we rely on plugins-release so it might be something you've added on your end?
It's not a details of the
We don't since the one we create has all the information of the original, plus the information about the batches that were invoked prior to the exception. Yes, this is intentional.
OK. We have to keep
It has to be for exception translation to occur as usual. Not being one would break backward compatiblity (see the original description if you need an example). |
Alright.
It doesn't have the same information. The next exception ( https://github.com/alibaba/wasp/blob/master/src/main/java/com/alibaba/wasp/jdbc/JdbcBatchUpdateException.java Oracle seems to store their extensions in the cause which should keep working.
I see your point. Modifying it to give it different semantics may also not be a good idea.
Do you mean the check in |
This commit updates JbcTemplate#batchUpdate to provide additional information when one batch fails. Previously, the raw BatchUpdateException was thrown with no way to know what had completed thus far. This commit creates an AggregatedBatchUpdateException that wraps the original BatchUpdateException, yet providing the counters of the batches that ran prior to the exception. In essence, this represents the same state as the return value of the method if no batch fails. Closes spring-projectsgh-23867
Thanks very much for the feedback @marschall, that's very useful. Here is another commit that should address the issues you've raised:
No I didn't mean that. Although that as well even if we can change our own translator to work with the exception. If you work with batches, it's very likely that the code is expecting a I believe we're good to go but I am giving it a few more days for others to chime in if they have concerns. The next milestone is next week. |
Hi Stephane |
@asollberger to make it easier for you to test, I've merged this so that it's available in Spring Framework |
I think the implementation is a fair compromise. If I could add one thing then I think this Line 187 in bf5e218
|
Alright, finally got my own test case together and saw the returned successfulUpdateCount and the originalExpection. Thanks a lot |
Affects: 3.0.0.M1 to 5.2.0.RELEASE
Goal
I'm trying to use the jdbcTemplate.batchUpdate and would like to know the exact failed statement.
Issues
Code to reproduce the problem
I use this table to quickly get a unique constraint exception
Configuration
Spring: 5.1.3.RELEASE
Database: Oracle 18c
Code trying to catch the BatchUpdateException
Possible solutions
The problem I'm having happens here:
https://github.com/spring-projects/spring-framework/blame/master/spring-jdbc/src/main/java/org/springframework/jdbc/support/SQLErrorCodeSQLExceptionTranslator.java#L179
The original stack
sqlEx
gets overridden with the next exception and thus the information of the BatchUpdateException gets lost.I understand the intent to translate the cause into an exception that is a little more meaningful, but IMHO the original BatchUpdateException should not be trashed.
Wherever a new exception is created the original exception should be used for the stack. So instead of:
the original exception should be used:
The second issue is probably here:
https://github.com/spring-projects/spring-framework/blob/master/spring-jdbc/src/main/java/org/springframework/jdbc/core/JdbcTemplate.java#L1059
rowsAffected
needs to be communicated back to the caller to pinpoint the problematic updateWorkaround
My current solution consist of writing my own Translator, overriding the doTranslate method and setting the translator when I create the jdbcTemplate Bean
The second issue cannot be fixed as easily. Either avoid the use of the batched update or set the batch size very high to not run into that problem
The text was updated successfully, but these errors were encountered: