Question

I`m still in a learning stage with spring transactions and aop, and i have some misundertandings :
1. if i do not use have an aop-advice and i specify for each service something like :

@Transactional(rollbackFor=java.lang.Exception.class,readOnly=true)

and then i look at spring log everything seems to work as expected. What i mean by this is that an transaction is created, and in case of exception i get a rollback. But if u use something like :

    <bean id="txManagerVA"  class="org.springframework.orm.jpa.JpaTransactionManager">
            <property name="entityManagerFactory"  ref="emfVA" />        
       </bean>

      <tx:annotation-driven transaction-manager="txManagerVA" />
<aop:config>
  <aop:pointcut id="transactionalServiceMethodsVA" expression="execution(* xxx.services.vs.*.*(..))"/>
  <aop:advisor advice-ref="txManagerVAAdvice" pointcut-ref="transactionalServiceMethodsVA"/>
</aop:config>

 <tx:advice id="txManagerVAAdvice" transaction-manager="txManagerVA">
    <tx:attributes>
     <tx:method name="get*" rollback-for="java.lang.Exception" read-only="true"/>    
     <tx:method name="*" rollback-for="java.lang.Exception" />   
    </tx:attributes>
 </tx:advice>

So, when i look at spring log,using this scenario, it seems that there are 2 transactions, on inside another, because after the rollback i get something like :

(IntermedServiceImpl.java:45) - WE HAVE AN EXCEPTION !!!! nullCrocodilu
(TransactionAspectSupport.java:406) - Completing transaction for [xxx.IntermedServiceImpl.getIntermed] after exception: java.lang.Exception: Crocodilu
(RuleBasedTransactionAttribute.java:130) - Applying rules to determine whether transaction should rollback on java.lang.Exception: Crocodilu
(RuleBasedTransactionAttribute.java:147) - Winning rollback rule is: RollbackRuleAttribute with pattern [java.lang.Exception]
(AbstractPlatformTransactionManager.java:935) - Triggering beforeCompletion synchronization
(TransactionSynchronizationManager.java:243) - Removed value [org.springframework.orm.jpa.ExtendedEntityManagerCreator$ExtendedEntityManagerSynchronization@63fd1f47] for key [org.hibernate.ejb.EntityManagerImpl@4eb35bed] from thread [thread-pool-1-8080(6)]
(AbstractPlatformTransactionManager.java:843) - Initiating transaction rollback
//SO FAR SO GOOD
(JpaTransactionManager.java:533) - Rolling back JPA transaction on EntityManager [org.hibernate.ejb.EntityManagerImpl@b3fbd77]
(AbstractPlatformTransactionManager.java:964) - Triggering afterCompletion synchronization
(TransactionSynchronizationManager.java:331) - Clearing transaction synchronization
(TransactionSynchronizationManager.java:243) - Removed value [org.springframework.orm.jpa.EntityManagerHolder@45a27493] for key [org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean@7ed30037] from thread [thread-pool-1-8080(6)]
(JpaTransactionManager.java:593) - Closing JPA EntityManager [org.hibernate.ejb.EntityManagerImpl@b3fbd77] after transaction
(EntityManagerFactoryUtils.java:343) - Closing JPA EntityManager
--> (AbstractPlatformTransactionManager.java:1012) - Resuming suspended transaction after completion of inner transaction <--
(TransactionSynchronizationManager.java:272) - Initializing transaction synchronization
(TransactionAspectSupport.java:406) - Completing transaction for [xxx.IntermedServiceImpl.getIntermed] after exception: java.lang.Exception: Crocodilu
(RuleBasedTransactionAttribute.java:130) - Applying rules to determine whether transaction should rollback on java.lang.Exception: Crocodilu
(RuleBasedTransactionAttribute.java:147) - Winning rollback rule is: null
(RuleBasedTransactionAttribute.java:152) - No relevant rollback rule found: applying default rules
(AbstractPlatformTransactionManager.java:922) - Triggering beforeCommit synchronization
(AbstractPlatformTransactionManager.java:935) - Triggering beforeCompletion synchronization
(AbstractPlatformTransactionManager.java:752) - Initiating transaction commit
(JpaTransactionManager.java:507) - Committing JPA transaction on EntityManager [org.hibernate.ejb.EntityManagerImpl@405c9cef]
(AbstractPlatformTransactionManager.java:948) - Triggering afterCommit synchronization
(AbstractPlatformTransactionManager.java:964) - Triggering afterCompletion synchronization
(TransactionSynchronizationManager.java:331) - Clearing transaction synchronization
(TransactionSynchronizationManager.java:243) - Removed value [org.springframework.orm.jpa.EntityManagerHolder@1f0cf366] for key [org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean@6012b597] from thread [thread-pool-1-8080(6)]
(JpaTransactionManager.java:593) - Closing JPA EntityManager [org.hibernate.ejb.EntityManagerImpl@405c9cef] after transaction
(EntityManagerFactoryUtils.java:343) - Closing JPA EntityManager

as you can see there i have marked the line that i do not understand.

And the second issue: in the same log file i`ve noticed a line like :

(JpaTransactionManager.java:407) - Not exposing JPA transaction [org.hibernate.ejb.EntityManagerImpl@b3fbd77] as JDBC transaction because JpaDialect [org.springframework.orm.jpa.DefaultJpaDialect@77acf1c0] does not support JDBC Connection retrieval

and i've googled a little bit and after added this :

<property name="jpaVendorAdapter">
        <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
            <property name="databasePlatform" value="org.hibernate.dialect.MySQL5InnoDBDialect" />
        </bean>
   </property> 

entitymanager bean, now it seems that jpa expose the transaction to jdbc transaction. The problem is that i do not know what are the advantages/disadvantages of this configuration.

No correct solution

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top