<p dir="auto"><strong><a href="https://jira.spring.io/secure/ViewProfile.jspa?name=zdv" rel="nofollow">Dmitry V. Zemnitskiy</a></strong> opened <strong><a href="https://jira.spring.io/browse/SPR-3849?redirect=false" rel="nofollow">SPR-3849</a></strong> and commented</p>
<p dir="auto">Copied from <a href="http://forum.springframework.org/showthread.php?p=140845" rel="nofollow">http://forum.springframework.org/showthread.php?p=140845</a><br>
Also see <a href="http://forum.springframework.org/showthread.php?t=31531" rel="nofollow">http://forum.springframework.org/showthread.php?t=31531</a></p>
<p dir="auto">I just checked with latest available spring (2.0.6), hibernate (3.2.5), hibernate entity manager and MS SQL database (both MSDE and MS SQL Server 2000), jtds driver.</p>
<p dir="auto">As I see it is still not solved, that is, with declarative transactions and exception translation, having business manager class annotated with <code class="notranslate">@Repository</code> and business method annotated with <code class="notranslate">@Transactional</code> (no nested transactions), example configuration below:</p>
<p dir="auto">Code:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="&lt;bean id=&quot;entityManagerFactory&quot;
	class=&quot;org.springframework.orm.jpa.LocalEntityManagerFactoryBean&quot;&gt;
	&lt;property name=&quot;persistenceUnitName&quot; value=&quot;${jpa.persistence.unit}&quot; /&gt;
	&lt;property name=&quot;jpaVendorAdapter&quot; ref=&quot;hibernateAdapter&quot; /&gt;
&lt;/bean&gt;

&lt;bean name=&quot;hibernateAdapter&quot;
	class=&quot;org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter&quot; /&gt;

&lt;bean id=&quot;transactionManager&quot;
	class=&quot;org.springframework.orm.jpa.JpaTransactionManager&quot;&gt;
	&lt;property name=&quot;entityManagerFactory&quot;
		ref=&quot;entityManagerFactory&quot; /&gt;
		
&lt;/bean&gt;

&lt;bean
	class=&quot;org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor&quot; /&gt;

&lt;bean
	class=&quot;org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor&quot; /&gt;

&lt;bean id=&quot;entityManager&quot;
	class=&quot;org.springframework.orm.jpa.support.SharedEntityManagerBean&quot;&gt;
	&lt;property name=&quot;entityManagerFactory&quot;
		ref=&quot;entityManagerFactory&quot; /&gt;
&lt;/bean&gt;

&lt;tx:annotation-driven transaction-manager=&quot;transactionManager&quot; /&gt;"><pre class="notranslate"><code class="notranslate">&lt;bean id="entityManagerFactory"
	class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean"&gt;
	&lt;property name="persistenceUnitName" value="${jpa.persistence.unit}" /&gt;
	&lt;property name="jpaVendorAdapter" ref="hibernateAdapter" /&gt;
&lt;/bean&gt;

&lt;bean name="hibernateAdapter"
	class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" /&gt;

&lt;bean id="transactionManager"
	class="org.springframework.orm.jpa.JpaTransactionManager"&gt;
	&lt;property name="entityManagerFactory"
		ref="entityManagerFactory" /&gt;
		
&lt;/bean&gt;

&lt;bean
	class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" /&gt;

&lt;bean
	class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" /&gt;

&lt;bean id="entityManager"
	class="org.springframework.orm.jpa.support.SharedEntityManagerBean"&gt;
	&lt;property name="entityManagerFactory"
		ref="entityManagerFactory" /&gt;
&lt;/bean&gt;

&lt;tx:annotation-driven transaction-manager="transactionManager" /&gt;
</code></pre></div>
<p dir="auto">I get UnexpectedRollbackException caused by integrity violation exception, because actual database operation and constraints verification are seems deferred to commit() in MS SQL (much like in Oracle I think).</p>
<p dir="auto">Here's exception stack trace:</p>
<p dir="auto">Code:</p>
<p dir="auto">org.springframework.transaction.UnexpectedRollbackException: JPA transaction unexpectedly rolled back (maybe marked rollback-only after a failed operation); nested exception is javax.persistence.RollbackException: Error while commiting the transaction<br>
Caused by:<br>
javax.persistence.RollbackException: Error while commiting the transaction<br>
at org.hibernate.ejb.TransactionImpl.commit(TransactionImpl.java:71)<br>
at org.springframework.orm.jpa.JpaTransactionManager.doCommit(JpaTransactionManager.java:433)<br>
at org.springframework.transaction.support.AbstractPlatformTransactionManager.processCommit(AbstractPlatformTransactionManager.java:662)<br>
at org.springframework.transaction.support.AbstractPlatformTransactionManager.commit(AbstractPlatformTransactionManager.java:632)<br>
at org.springframework.transaction.interceptor.TransactionAspectSupport.commitTransactionAfterReturning(TransactionAspectSupport.java:314)<br>
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:117)<br>
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:166)<br>
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:204)<br>
at $Proxy34.handleChangeMsisdn(Unknown Source)<br>
at com.vyke.mobile.server.io.ConnectionHandlerImpl.packetReceived(ConnectionHandlerImpl.java:254)</p>
<p dir="auto">... removed application code calls ...</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="at java.lang.Thread.run(Unknown Source)"><pre class="notranslate"><code class="notranslate">at java.lang.Thread.run(Unknown Source)
</code></pre></div>
<p dir="auto">Caused by: org.hibernate.exception.ConstraintViolationException: could not update: [com.vyke.mobile.server.domain.Client#4]<br>
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:71)<br>
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)<br>
at org.hibernate.persister.entity.AbstractEntityPersister.update(AbstractEntityPersister.java:2425)<br>
at org.hibernate.persister.entity.AbstractEntityPersister.updateOrInsert(AbstractEntityPersister.java:2307)<br>
at org.hibernate.persister.entity.AbstractEntityPersister.update(AbstractEntityPersister.java:2607)<br>
at org.hibernate.action.EntityUpdateAction.execute(EntityUpdateAction.java:92)<br>
at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:250)<br>
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:234)<br>
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:142)<br>
at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:298)<br>
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)<br>
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1000)<br>
at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:338)<br>
at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:106)<br>
at org.hibernate.ejb.TransactionImpl.commit(TransactionImpl.java:54)<br>
... 29 more<br>
Caused by: java.sql.SQLException: Violation of UNIQUE KEY constraint 'UQ__Client__72C60C4A'. Cannot insert duplicate key in object 'Client'.<br>
at net.sourceforge.jtds.jdbc.SQLDiagnostic.addDiagnostic(SQLDiagnostic.java:365)<br>
at net.sourceforge.jtds.jdbc.TdsCore.tdsErrorToken(TdsCore.java:2781)<br>
at net.sourceforge.jtds.jdbc.TdsCore.nextToken(TdsCore.java:2224)<br>
at net.sourceforge.jtds.jdbc.TdsCore.getMoreResults(TdsCore.java:628)<br>
at net.sourceforge.jtds.jdbc.JtdsStatement.processResults(JtdsStatement.java:525)<br>
at net.sourceforge.jtds.jdbc.JtdsStatement.executeSQL(JtdsStatement.java:487)<br>
at net.sourceforge.jtds.jdbc.JtdsPreparedStatement.executeUpdate(JtdsPreparedStatement.java:421)<br>
at org.hibernate.jdbc.NonBatchingBatcher.addToBatch(NonBatchingBatcher.java:23)<br>
at org.hibernate.persister.entity.AbstractEntityPersister.update(AbstractEntityPersister.java:2403)<br>
... 41 more</p>
<p dir="auto">I found very similar problem description here :</p>
<p dir="auto"><a href="http://forum.springframework.org/showthread.php?t=31531" rel="nofollow">http://forum.springframework.org/showthread.php?t=31531</a></p>
<p dir="auto">it is dated by last year..</p>
<p dir="auto">I was able to solve the problem only by verification during business method invocation if conflicting record already exists in database using extra select statement.</p>
<p dir="auto">IMO it is very rough solution and quite bad problem in Spring, as the situation is quite common and it voids all efforts made to improve persistence exception<br>
handling in Spring at all.</p>
<p dir="auto">Regards,<br>
Dima</p>
<hr>
<p dir="auto"><strong>Affects:</strong> 2.0.5, 2.0.6, 2.1 M3</p>
<p dir="auto"><strong><a href="https://jira.spring.io/secure/ViewProfile.jspa?name=dmitriy" rel="nofollow">Dmitriy Kopylenko</a></strong> opened <strong><a href="https://jira.spring.io/browse/SPR-163?redirect=false" rel="nofollow">SPR-163</a></strong> and commented</p>
<p dir="auto">There is a notion of Errors &lt;&lt;interface&gt;&gt;, so during validation there might be some property values rejected. We treat Errors as critical e.g. in Controllers after validation we check Errors collection and if not empty we <strong>DO NOT</strong> proceed (invoke the middle tier). There are some use cases however that state - some validations should be treated as <strong>WARNINGS</strong> e.g. if the rule is not satisfied, just display a warning message to the user, but still proceed with the workflow.</p>
<p dir="auto">I'm not sure if that's been solved already, but in any case, would be nice to create a Warnings &lt;&lt;interface&gt;&gt; to distinguish between those to concepts (Errors -&gt; do not proceed, Warnings -&gt; still proceed)</p>
<ul dir="auto">
<li>Dmitriy.</li>
</ul>
<hr>
<p dir="auto"><strong>Affects:</strong> 1.1 RC1</p>
<p dir="auto"><strong>Issue Links:</strong></p>
<ul dir="auto">
<li><a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="398061072" data-permission-text="Title is private" data-url="https://github.com/spring-projects/spring-framework/issues/6123" data-hovercard-type="issue" data-hovercard-url="/spring-projects/spring-framework/issues/6123/hovercard" href="https://github.com/spring-projects/spring-framework/issues/6123">#6123</a> Expand Errors object to be a Messages object (<em><strong>"is duplicated by"</strong></em>)</li>
</ul>
<p dir="auto">22 votes, 15 watchers</p>