Spring Planner. If there is a circular dependency, the scheduled method does not start in the transaction

I am working on an application that uses Spring 3, Hibernate, and JPA. I have two classes:

@Component class Manager { @Autowired Util util; } 

and

 @Component class Util { @Autowired Manager manager; @Scheduled(fixedDelay = 1 * 60 * 1000) @Transactional(propagation = Propagation.REQUIRED) public void scheduledMethod(){ // Need to update the database in a transaction } } 

The relevant part from the application context is as follows:

  <context:component-scan base-package="packageName" /> <tx:annotation-driven /> <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"> <property name="persistenceUnitName" value="defaultPU" /> <property name="dataSource" ref="dataSource" /> </bean> <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"> <property name="entityManagerFactory" ref="entityManagerFactory" /> </bean> <task:annotation-driven executor="executor" scheduler="scheduler"/> <task:executor id="executor" pool-size="10" queue-capacity="10000" rejection-policy="CALLER_RUNS"/> <task:scheduler id="scheduler" pool-size="10"/> 

In this configuration, I get the following exception

 javax.persistence.TransactionRequiredException: no transaction is in progress at org.hibernate.ejb.AbstractEntityManagerImpl.flush(AbstractEntityManagerImpl.java:978) at sun.reflect.GeneratedMethodAccessor88.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:601) at org.springframework.orm.jpa.ExtendedEntityManagerCreator$ExtendedEntityManagerInvocationHandler.invoke(ExtendedEntityManagerCreator.java:365) at com.sun.proxy.$Proxy43.flush(Unknown Source) at sun.reflect.GeneratedMethodAccessor88.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:601) at org.springframework.orm.jpa.SharedEntityManagerCreator$SharedEntityManagerInvocationHandler.invoke(SharedEntityManagerCreator.java:240) at com.sun.proxy.$Proxy43.flush(Unknown Source) 

If I remove the autowiring class of the Manager class from the Util class, it works fine. In addition, during debugging, I found that the scheduled method runs even if there is some error in the application context file.

For some outdated reasons, I cannot avoid circular dependency. Can someone help why this exception occurs in case of circular dependency?

+6
source share
1 answer

You can achieve this using @PostConstruct

 @Component class Manager { Util util; public void setUtil(Util util) { this.util = util; } } @Component class Util { @Autowired Manager manager; @PostConstruct public void init(){ manager.setUtil(this); } @Scheduled(fixedDelay = 1 * 60 * 1000) @Transactional(propagation = Propagation.REQUIRED) public void scheduledMethod(){ // Need to update the database in a transaction } } 
0
source

Source: https://habr.com/ru/post/950162/


All Articles