DuplicateKeyException is not always thrown even if Constraint Violation happens at EJB 2 CMP creation in EAP 6.x

Solution Unverified - Updated

Environment

  • Red Hat JBoss Enterprise Application Platform (EAP) 6.x

Issue

In EAP 6.x, EJB 2 CMP does not throw DuplicateKeyException for some database even if Constraint Violation happens.

Resolution

This content is not included.BZ#1370040 was raised to improve the checking mechanism in SQLExceptionProcessor.isDuplicateKey(e). And the mechanism will be changed to check if SQLState is 5 digit code starting with 23 and this change was included in EAP 6.4 CP12 (6.4.12) or later.

However, throwing CreateException is enough and harmless in general case.

Root Cause

SQLExceptionProcessor.isDuplicateKey(e) is called to detect constraint violation at EJB 2 CMP creation when <entity-command name="no-select-before-insert "/> is set in jbosscmp-jdbc.xml. However SQLExceptionProcessor.isDuplicateKey(e) checks only if SQLState is equal to 23000 or not. And some database does not always return 23000 as SQLState for constraint violation. For example, PostgreSQL returns 23505. Hence, DuplicateKeyException is not always thrown and instead CreateException is thrown in that case.

Components
Category

This solution is part of Red Hat’s fast-track publication program, providing a huge library of solutions that Red Hat engineers have created while supporting our customers. To give you the knowledge you need the instant it becomes available, these articles may be presented in a raw and unedited form.