6
votes

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?

1
What in your output makes you think the scheduler is being called? The exception seems to be related to the entity manager factory. Post the full stack trace, the problem could be related to another exception that is not included in the portion of the trace you posted. - joanlofe
By the way, you have a typo in the name of your method, it is schedulenot schdeule. - joanlofe
at com.sun.proxy.$Proxy70.findByTraIdAndTransactionNameAndExecutionTime(Unknown Source) at nl.yestelecom.boss.common.services.BossExtChangeService.getExtchangesByTransactionName(BossExtChangeService.java:29) at nl.yestelecom.boss.requestservices.RequestServiceImpl.handleExtchanges(RequestServiceImpl.java:41) at nl.yestelecom.boss.processor.BusinessService.doChange(BusinessService.java:47) at nl.yestelecom.boss.processor.BusinessService.startService(BusinessService.java:36) at nl.yestelecom.boss.processor.SchedulerJob.schdeule(SchedulerJob.java:20) - Ravindra Devadiga
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:497) at org.springframework.scheduling.support.ScheduledMethodRunnable.run(ScheduledMethodRunnable.java:65) at org.springframework.scheduling.support.DelegatingErrorHandlingRunnable.run(DelegatingErrorHandlingRunnable.java:54) - Ravindra Devadiga
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) at java.util.concurrent.FutureTask.runAndReset(FutureTask.java:308) at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$301(ScheduledThreadPoolExecutor.java:180) at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:294) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) at java.lang.Thread.run(Thread.java:745) - Ravindra Devadiga

1 Answers

1
votes

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