Search code examples
javaspringhibernateaopspring-transactions

Java application with Spring and Hibernate upgraded from 3.2.1 to 4.3.2 and 4.2.0 to 5.2.1 throwing error


Application throwing the following error:

org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type [org.springframework.transaction.PlatformTransactionManager] is defined: expected single matching bean but found 3

Hi we have a webapp which we upgraded to the above configurations which runs on java 8, tomcat 8. On the first interaction with the database(write transactions), the application throws the above error.

We have used Spring's Hibernate Transaction Manager implementation. We have spring aop defined to include the bo methods with the transaction and the transaction advice for the necessary actions (roll-backs basically) on certain exceptions.

Note: JDK and tomcat are also upgraded, both from 6 to 8

We had the same configuration(mentioned below) in the app before upgrade of Spring, Hibernate, tomcat and java which was and is still working properly, not even once we encountered this error.

Each data source, session factory and transaction managers are defined as below Data Sources goes like this

session factory configuration
<bean id="sessionFactory1" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
    <property name="dataSource" ref="dataSource1"/>
    <property name="mappingResources">
       <list>
          <!-- List of hbm mappings -->
       </list>
    </property>
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">${hibernate.dialect}</prop>
            <prop key="hibernate.show_sql">true</prop>
            <prop key="hibernate.generate_statistics">true</prop>
            <prop key="hibernate.format_sql">false</prop>
            <prop key="hibernate.use_sql_comments">false</prop>
            <prop key="hibernate.connection.release_mode">after_transaction</prop>
            <prop key="hibernate.c3p0.timeout">1</prop>
        </props>
    </property>
</bean> 

Transaction manager, transaction advice and aop config
<bean id="transactionManager1" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactory1"/>
</bean>
<tx:advice id="defaultTxAdvice1" transaction-manager="transactionManager1">
   <tx:attributes>
      <tx:method name="get*" read-only="true"/>
      <tx:method name="*" rollback-for="com.xyz.platformcore.common.exception.VyasaException"/>
   </tx:attributes>
</tx:advice>

<aop:config>
  <aop:pointcut id="serviceOperation1" expression="execution(* com..*BO.*(..)) || execution(* com..*Bo.*(..)) || execution(* com..*bo.*(..)) || execution(* com.xyz.platformcore..*BO.*(..)) || execution(* com.xyz.framework..*bo.*(..)) || execution(* com.xyz.framework..*Bo.*(..)) || execution(* com.xyz.platformcore..*Bo.*(..)) ||  @annotation(com.xyz.platformcore.common.hibernate.VyasaTransactionAwareHibernateOperation) || within(@com.xyz.platformcore.common.hibernate.VyasaTransactionAwareHibernateOperation *)" />
  <aop:advisor advice-ref="defaultTxAdvice1" pointcut-ref="serviceOperation1"/>
</aop:config>

My dao method

public void invalidateSomeThing() {
    Query query = getSession1().createQuery("update SomeThing set isActive =:isActive");
    query.setParameter("isActive", false);
    query.executeUpdate();
}
getSession1() gives the Session object from SessionFcatory1 which connects to DataSource1

Any help is highly appreciated


Solution

  • Pls check this out ... "Transaction manager cache fails to repopulate when multiple transaction managers defined"

    https://jira.spring.io/browse/SPR-14609

    Its a bug in the Spring FW 4.3.2 release. Fixed in 4.3.3.

    Regards, --Yash