Add DuplicateKeyException to DAO exception hierachy [SPR-5125] #9798
Labels
in: core
Issues in core modules (aop, beans, core, context, expression)
type: enhancement
A general enhancement
Milestone
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:
Issue Links:
Referenced from: commits a0139ab
1 votes, 2 watchers
The text was updated successfully, but these errors were encountered: