<p dir="auto"><strong><a href="https://jira.spring.io/secure/ViewProfile.jspa?name=tuomas_kiviaho" rel="nofollow">Tuomas Kiviaho</a></strong> opened <strong><a href="https://jira.spring.io/browse/SPR-6107?redirect=false" rel="nofollow">SPR-6107</a></strong> and commented</p>
<p dir="auto">In order to define bean using factory method from other than bean class itself requires currently using of a registered factory bean. Registered factory bean feels quite useless when implementing factory method pattern. In worst case bean count duplicates if bean name of factory bean has to be generated on the fly.</p>
<p dir="auto">MethodInvokingFactoryBean (with help of MethodInvoker) allows static class as prefix of factory method name and can therefore be used as registered factory bean but incorporating of this logic directly to org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.getTypeForFactoryMethod (and possibly to some other places than I've overlooked) would omit the need of factory bean altogether in these cases. Using MethodInvoker might also be beneficial since it already contains logic to handle illegal state scenarios which would be possible with this addition.</p>
<hr>
<p dir="auto"><strong>Affects:</strong> 2.5.6</p>
<p dir="auto"><strong><a href="https://jira.spring.io/secure/ViewProfile.jspa?name=benrowlands" rel="nofollow">Ben Rowlands</a></strong> opened <strong><a href="https://jira.spring.io/browse/SPR-5125?redirect=false" rel="nofollow">SPR-5125</a></strong> and commented</p>
<p dir="auto">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.</p>
<p dir="auto">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).</p>
<p dir="auto">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.</p>
<p dir="auto">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.</p>
<p dir="auto">Ideally an exception like:</p>
<p dir="auto">public class DuplicateKeyException extends DataIntegrityViolationException { }</p>
<p dir="auto">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.</p>
<p dir="auto">We currently use the following SQLExceptions to detect a dup-key:</p>
<ul dir="auto">
<li>Sybase: error-state=23000</li>
<li>DB2: error-state=23505</li>
</ul>
<hr>
<p dir="auto"><strong>Issue Links:</strong></p>
<ul dir="auto">
<li><a href="https://jira.spring.io/browse/SPRNET-1215" rel="nofollow">SPRNET-1215</a> Add DuplicateKeyException to DAO exception hierachy (<em><strong>"is depended on by"</strong></em>)</li>
<li><a href="https://jira.spring.io/browse/SPRNET-1216" rel="nofollow">SPRNET-1216</a> Change exception translation mappings to throw DuplicateKeyException (<em><strong>"is depended on by"</strong></em>)</li>
<li><a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="398090700" data-permission-text="Title is private" data-url="https://github.com/spring-projects/spring-framework/issues/9799" data-hovercard-type="issue" data-hovercard-url="/spring-projects/spring-framework/issues/9799/hovercard" href="https://github.com/spring-projects/spring-framework/issues/9799">#9799</a> Add DuplicateKeyException to DAO exception hierachy (<em><strong>"is duplicated by"</strong></em>)</li>
<li><a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="398074031" data-permission-text="Title is private" data-url="https://github.com/spring-projects/spring-framework/issues/7663" data-hovercard-type="issue" data-hovercard-url="/spring-projects/spring-framework/issues/7663/hovercard" href="https://github.com/spring-projects/spring-framework/issues/7663">#7663</a> Subclass DataIntegrityViolationException to represent main constraint types and translate SQL exceptons into appropriate subclass</li>
</ul>
<p dir="auto"><strong>Referenced from:</strong> commits <a class="commit-link" data-hovercard-type="commit" data-hovercard-url="https://github.com/spring-projects/spring-framework/commit/a0139abf1e4088e60ae6cd56bbc4779dd522cfde/hovercard" href="https://github.com/spring-projects/spring-framework/commit/a0139abf1e4088e60ae6cd56bbc4779dd522cfde"><tt>a0139ab</tt></a></p>
<p dir="auto">1 votes, 2 watchers</p>