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

Add DuplicateKeyException to DAO exception hierachy [SPR-5125] #9798

Closed
spring-projects-issues opened this issue Aug 29, 2008 · 2 comments
Closed
Labels
in: core Issues in core modules (aop, beans, core, context, expression) type: enhancement A general enhancement
Milestone

Comments

@spring-projects-issues
Copy link
Collaborator

spring-projects-issues commented Aug 29, 2008

Ben Rowlands opened SPR-5125 and commented

The Spring DAO exception hierarchy is not fine grained enough to isolate duplicate key errors. A duplicate key error is an example of where an application may catch the exception and invoke alternative logic instead of just letting it fall down the stack.

For example, we read messages from a queue and insert the data into a database. We explicitly catch a duplicate key insert exception as this indicates we've processed the same message earlier. This is more efficient than a guarding the update with a check-then-act (which also introduces a race condition that may lead to dup-key exception anyway).

Additionally, if we use single-phase commit for our JMS and DB transactions (commit DB first then JMS) a failure scenario is that the DB transaction commits but the JMS transaction rollsback. In this case the message is delivered again and we get a dup-key exception. We can then safely ignore such duplicate messages.

A dup-key error is currently classified as a DataIntegrityViolationException along with other errors such as a data bounds error so we can't simply catch this exception type. We could work around this be checking the vendor error-code/state however this is not portable and cumbersome.

Ideally an exception like:

public class DuplicateKeyException extends DataIntegrityViolationException { }

Spring already provides mappings from vendor specific codes so it would be very powerful if it could provide a fine grained exception to allow this common duplicate-key handling pattern to be detected just using exception type.

We currently use the following SQLExceptions to detect a dup-key:

  • Sybase: error-state=23000
  • DB2: error-state=23505

Issue Links:

Referenced from: commits a0139ab

1 votes, 2 watchers

@spring-projects-issues
Copy link
Collaborator Author

spring-projects-issues commented Aug 29, 2008

Ben Rowlands commented

This is similar (a more specific request) than #7663

@spring-projects-issues
Copy link
Collaborator Author

Ben Rowlands commented

Actually, we also need to check the vendor error-code in Sybase (FK violations also map onto 23000):

http://manuals.sybase.com/onlinebooks/group-as/asg1250e/refman/`@Generic__BookTextView`/27249

The error code 2601 specifies we have a duplicate key:

http://manuals.sybase.com/onlinebooks/group-as/asg1250e/svrtsg/`@Generic__BookTextView`/34048;pt=15380

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: core Issues in core modules (aop, beans, core, context, expression) type: enhancement A general enhancement
Projects
None yet
Development

No branches or pull requests

1 participant