Hi everyone,
I'm using Spring declarative transactions. I want Spring to rollback transaction on checked exception. Should be simple.
applicationConext.xml:
My method is annotated by @Transactional, everything works OK if this method doesn't throw an exception. If it does I can see in my logs, that Spring looks for rollback rules twice. The first time it finds my rule OK and marks transaction as rollbackOnly, the second one it finds no rules, falls back to default and tries to commit. But transaction is marked as rollbackOnly, so commit fails, but my web tier gets Spring exception instead of mine. Here's log from my test:Code:<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"> <property name="persistenceUnitName" value="x"/> <property name="dataSource" ref="dataSource"/> </bean> <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"> <property name="entityManagerFactory" ref="entityManagerFactory"/> </bean> <tx:annotation-driven transaction-manager="transactionManager" order="100"/> <tx:advice id="defaultTxAdvice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="*" rollback-for="Throwable"/> </tx:attributes> </tx:advice> <aop:config> <aop:pointcut id="defaultServiceOperation" expression="execution(* (@org.springframework.stereotype.Service *).*(..))"/> <aop:advisor pointcut-ref="defaultServiceOperation" advice-ref="defaultTxAdvice"/> </aop:config> <aop:aspectj-autoproxy/>
I'm using Spring 3.0.3.GA with JPA2 provided by Hibernate 3.5.3-Final. What am I missing?Code:INFO [pl.x.y.psi.service.impl.TimePeriodServiceImpl:468] - Unable to block time period, conflicts exists: Time Period 1 DEBUG [org.springframework.transaction.interceptor.TransactionInterceptor:387] - Completing transaction for [pl.x.y.psi.service.api.TimePeriodService.blockTimePeriod] after exception: pl.x.y.psi.exception.ConflictedSalesPlanException: Unable to block time period, conflicts exists: Time Period 1 DEBUG [org.springframework.transaction.interceptor.RuleBasedTransactionAttribute:130] - Applying rules to determine whether transaction should rollback on pl.x.y.psi.exception.ConflictedSalesPlanException: Unable to block time period, conflicts exists: Time Period 1 DEBUG [org.springframework.transaction.interceptor.RuleBasedTransactionAttribute:147] - Winning rollback rule is: RollbackRuleAttribute with pattern [Throwable] DEBUG [org.springframework.orm.jpa.JpaTransactionManager:850] - Participating transaction failed - marking existing transaction as rollback-only DEBUG [org.springframework.orm.jpa.JpaTransactionManager:513] - Setting JPA transaction on EntityManager [org.hibernate.ejb.EntityManagerImpl@23f2bc83] rollback-only DEBUG [org.springframework.transaction.interceptor.TransactionInterceptor:387] - Completing transaction for [pl.x.y.psi.service.api.TimePeriodService.blockTimePeriod] after exception: pl.x.y.psi.exception.ConflictedSalesPlanException: Unable to block time period, conflicts exists: Time Period 1 DEBUG [org.springframework.transaction.interceptor.RuleBasedTransactionAttribute:130] - Applying rules to determine whether transaction should rollback on pl.x.y.psi.exception.ConflictedSalesPlanException: Unable to block time period, conflicts exists: Time Period 1 DEBUG [org.springframework.transaction.interceptor.RuleBasedTransactionAttribute:147] - Winning rollback rule is: null DEBUG [org.springframework.transaction.interceptor.RuleBasedTransactionAttribute:152] - No relevant rollback rule found: applying default rules DEBUG [org.springframework.orm.jpa.JpaTransactionManager:925] - Triggering beforeCommit synchronization DEBUG [org.springframework.orm.jpa.JpaTransactionManager:938] - Triggering beforeCompletion synchronization DEBUG [org.springframework.orm.jpa.JpaTransactionManager:752] - Initiating transaction commit DEBUG [org.springframework.orm.jpa.JpaTransactionManager:462] - Committing JPA transaction on EntityManager [org.hibernate.ejb.EntityManagerImpl@23f2bc83] DEBUG [org.springframework.orm.jpa.JpaTransactionManager:967] - Triggering afterCompletion synchronization DEBUG [org.springframework.transaction.support.TransactionSynchronizationManager:316] - Clearing transaction synchronization DEBUG [org.springframework.transaction.support.TransactionSynchronizationManager:229] - Removed value [org.springframework.orm.jpa.EntityManagerHolder@3e7c609] for key [org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean@657a7adf] from thread [main] DEBUG [org.springframework.orm.jpa.JpaTransactionManager:548] - Closing JPA EntityManager [org.hibernate.ejb.EntityManagerImpl@23f2bc83] after transaction DEBUG [org.springframework.orm.jpa.EntityManagerFactoryUtils:328] - Closing JPA EntityManager ERROR [org.springframework.transaction.interceptor.TransactionInterceptor:415] - Application exception overridden by commit exception pl.x.y.psi.exception.ConflictedSalesPlanException: Unable to block time period, conflicts exists: Time Period 1
Best regards
Jacek Bilski


Reply With Quote

