Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring boot with scheduler-BeanCreationNotAllowedException: Error creating bean with name 'entityManagerFactory': Singleton bean creation not allowed

We have a spring boot project with scheduler which reads the data from the database at fixed intervals.

While building project from STS using maven we are getting below error in the console while it is running the test cases even though final build status is success.

org.springframework.beans.factory.BeanCreationNotAllowedException: Error creating bean with name 'entityManagerFactory': Singleton bean creation not allowed while the singletons of this factory are in destruction (Do not request a bean from a BeanFactory in a destroy method implementation!) at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:216) at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:299) at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199) at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeansOfType(DefaultListableBeanFactory.java:523) at org.springframework.beans.factory.BeanFactoryUtils.beansOfTypeIncludingAncestors(BeanFactoryUtils.java:276) at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.detectPersistenceExceptionTranslators(PersistenceExceptionTranslationInterceptor.java:162) at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:145) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) at org.springframework.data.jpa.repository.support.CrudMethodMetadataPostProcessor$CrudMethodMetadataPopulatingMethodIntercceptor.invoke(CrudMethodMetadataPostProcessor.java:122) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:207) at com.sun.proxy.$Proxy70.findByTraIdAndTransactionNameAndExecutionTime(Unknown Source) at

Application file

@SpringBootApplication
@PropertySource("classpath:application.properties")
@EnableScheduling
public class ProvisioningApplication {

    public static void main(String[] args) {

        SpringApplication.run(ProvisioningApplication.class, args);

    }
}

Scheduler File

BusinessService has the logic of reading the database

@Component
public class SchedulerJob {

    @Autowired
    BusinessService service;

    @Scheduled(fixedRate=300000) //5mnts
    public void schdeule() {
        service.startService();

    }
}

Test File

@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = ProvisioningApplication.class)
public class ProvisioningApplicationTests {

    @Test
    public void contextLoads() {
    }

}

Question here is why spring boot runs the scheduler task while building the project and why it is throwing the above exception?

like image 704
Ravindra Devadiga Avatar asked Dec 23 '15 11:12

Ravindra Devadiga


People also ask

What is beancreationexception in Spring Boot?

Overview In this article, we are discussing the Spring org.springframework.beans.factory.BeanCreationException – this is a very common exception thrown when the BeanFactory creates beans of the bean definitions and encounteres a problem. The article will discuss the most common causes of this exception along with the solution.

What is cannotloadbeanclassexception in Spring Boot?

Cause: org.springframework.beans.factory.CannotLoadBeanClassException This exception is thrown when Spring cannot load the class of the defined bean – this may occur if the Spring XML Configuration contains a bean that simply doesn't have a corresponding class.

Why do I get a bean creation exception in spring?

Another similar cause for the bean creation exception is Spring trying to inject a bean by type – namely by its interface – and finding two or more bean implementing that interface in the context. For example, BeanB1 and BeanB2 both implement the same interface: @Component public class BeanB1 implements IBeanB { ...

What is beancreationnotallowedexception?

org. springframework. beans. factory. BeanCreationNotAllowedException: Error creating bean with name 'entityManagerFactory': Singleton bean creation not allowed while the singletons of this factory are in destruction (Do not request a bean from a BeanFactory in a destroy method implementation!) at org. springframework. beans. factory. support.


1 Answers

In Spring Boot, when you do a maven build the test cases are run by default. In this scenario the integration test scripts are run which will be trying to connect to your database. Since you dont have anything to be excecuted as part of integration test in your project. One possible solution is to declare your class ProvisioningApplicationTests as abstract. This will restrict the instance creation of ProvisioningApplicationTests class.

@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = ProvisioningApplication.class)
public abstract class ProvisioningApplicationTests {
    @Test
    public void contextLoads() {
    }
  }

The other way to solve this issue is to include the below code in your pom.xml

<plugins>
   <plugin>
    <artifactId>maven-surefire-plugin</artifactId>
    <configuration>
     <skipTests>false</skipTests>
     <excludes>
      <exclude>**/*IT.java</exclude>
     </excludes>
    </configuration>
   </plugin>
   <plugin>
    <artifactId>maven-failsafe-plugin</artifactId>
    <executions>
     <execution>
      <id>integration-test</id>
      <goals>
       <goal>integration-test</goal>
      </goals>
      <configuration>
       <skipTests>true</skipTests>
       <includes>
        <include>**/*IT.class</include>
       </includes>
      </configuration>
     </execution>
    </executions>
   </plugin>
  </plugins>

This will exclude your integration test classes to be executed while building youy project. maven-surefire-plugin is use to run unit tests. maven-failsafe-plugin is use to run integration tests. While using this approach make sure that all your integration class file names ends with 'IT'. E.g. UserTestIT.java

like image 89
Mohith Kumar Avatar answered Sep 23 '22 22:09

Mohith Kumar