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

Translate SQL Exception with State S0001 and Vendor Code 2628 to a Spring Exception in MSSQL 2019 #30681

Closed
NVelichkovski opened this issue Jun 16, 2023 · 3 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

@NVelichkovski
Copy link

Issue Description:

In the Spring JDBC framework, SQL exceptions with state S0001 and vendor code 2628 for MSSQL 2019 are not properly translated to a Spring exception. This behavior differs from the previous version, MSSQL 2017, where the same exception was translated into a Data Integrity Violation exception by Spring. The issue can be reproduced by inserting a value larger in length by the max allowed value of a column.

Expected Behavior:

When encountering an SQL exception with state S0001 and vendor code 2628 message String or binary data would be truncated in table '%.*ls', column '%.*ls'. Truncated value: '%.*ls'. from MSSQL 2019, the Spring JDBC framework should accurately translate it into a corresponding Spring exception. Instead the exception ends uncategorised (UncategorizedSQLException).

In older version of the MSSQL server (2017) the same exception is defined with:

  • state: 22001
  • vendor code: 8152

then when the chain of the sql exception translators are executed the state class 22 is recognised as an DataIntegrityViolationException within the SQLErrorCodeSQLExceptionTranslator. But in versions above 2017th the state is changed to S0001, so none of the translators understand the exception.

Proposed solution:

Since i could not found another broken exception translation, the vendor code can be added in the sql-error-codes.xml.

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Jun 16, 2023
@jhoeller jhoeller self-assigned this Jun 16, 2023
@jhoeller jhoeller added the in: data Issues in data modules (jdbc, orm, oxm, tx) label Jun 16, 2023
@jhoeller
Copy link
Contributor

We generally rely on SQLExceptionSubclassTranslator by default now in 6.0, based on the JDBC 4 exception subclasses, with a few extra tweaks on our side. Which SQLException subclass is being thrown in your scenario there?

That said, you may add a custom sql-error-codes.xml file to still get the error code mappings on 6.0, specifying some custom codes there or even just an empty file in the classpath root (since our legacy default error code mappings still apply then).

@NVelichkovski
Copy link
Author

NVelichkovski commented Jun 16, 2023

I am on a spring version 5.3.21, but i believe the issue exist in versions 6.0 as well . The subclass is an SQLServerException.
Regarding a solution, i created a temp custom exception translator implementing the SQLErrorCodeSQLExceptionTranslator in order not to be affected, but this now needs to be duplicated across our affected components, so i see it as a temporary solution.

@jhoeller
Copy link
Contributor

jhoeller commented Jun 16, 2023

So the SQL Server driver is not actually throwing JDBC 4 exception subclasses? Ouch. That might be a problem with the default behavior on Spring Framework 6.0 then.

In any case, we can certainly add 2628 to the dataIntegrityViolationCodes category for our MS-SQL entry in sql-error-codes.xml. With 5.3.x, this will automatically apply then; for 6.0, it requires opting in.

@jhoeller jhoeller added type: enhancement A general enhancement and removed status: waiting-for-triage An issue we've not yet triaged or decided on labels Jun 16, 2023
@jhoeller jhoeller changed the title Failure to Translate SQL Exception with State S0001 and Vendor Code 2628 to a Spring Exception in MSSQL 2019 Translate SQL Exception with State S0001 and Vendor Code 2628 to a Spring Exception in MSSQL 2019 Jun 16, 2023
@jhoeller jhoeller added this to the 6.0.11 milestone Jun 16, 2023
@jhoeller jhoeller added the for: backport-to-5.3.x Marks an issue as a candidate for backport to 5.3.x label Jun 16, 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 Jun 16, 2023
jhoeller added a commit that referenced this issue Jun 17, 2023
mdeinum pushed a commit to mdeinum/spring-framework that referenced this issue Jun 29, 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: enhancement A general enhancement
Projects
None yet
Development

No branches or pull requests

3 participants