0

I am facing issue while creating bean for HibernateTransactionManager in a Spring Batch Application.

Application is unable to create bean for transactionManager for HibernateTransactionManager as it is already created by SimpleBatchConfiguration class while enabling @EnableBatchProcessing annotation.

After further analysis, I found that SimpleBatchConfiguration class creates a transactionManager bean of type PlatformTransactionManager. This PlatformTransactionManager class extends TransactionManager.

Also while trying to create transactionManager bean for HibernateTransactionManager (this also extends TransactionManager class), application is failed to start saying that

No qualifying bean found for org.springframework.transaction.TransactionManager. Expecting one but found two: transactionManager,transactionManagerBean.

Below is the bean defination for transactionManagerBean.

<bean id="transactionManagerBean" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
    <property name="sessionFactory" ref="hibernateSessionFactory"/>
</bean>

<tx:annotation-driven transaction-manager="transactionManagerBean"/>

I am using: Spring batch: 4.2.7-RELEASE Spring boot: 2.3.11-RELEASE Hibernate: 5.4.31-Final Spring-cloud-starter: 2.2.5-RELEASE

Can anyone please help.

Dev
  • 11
  • 3

2 Answers2

1

This is a known issue and was reported here: https://github.com/spring-projects/spring-batch/issues/816. This will be fixed in the next major version (as it is breaking change).

The workaround for now is to enable bean overriding if you use Spring Boot or give your transaction manager a name other than transactionManager. Otherwise, you can omit using @EnableBatchProcessing and configure infrastructure beans manually.

Mahmoud Ben Hassine
  • 28,519
  • 3
  • 32
  • 50
0

Use the bean element's primary attribute. It indicates that a bean should be given preference when multiple candidates are qualified to autowire a single-valued dependency.

<bean primary="true"/>

Rahul Dey
  • 163
  • 2
  • 7
  • Hi @Rahul As you suggested I add primary="true" in hibernateTransaction bean definition. After that I received below error. The bean 'org.springframework.transaction.config.internhttps://stackoverflow.com/questions/52621792/initialise-h2-database-for-spring-batch-application?rq=1alTransactionAdvisor', defined in class path resource [org/springframework/transaction/annotation/ProxyTransactionManagementConfiguration.class], could not be registered. A bean with that name has already been defined and overriding is disabled. Can you please help... – Dev Jun 21 '21 at 02:51
  • It's having conflicts due to the same name. Try to give some other name to your bean. – Rahul Dey Jun 21 '21 at 06:30