title: why I can't get result back in @Async method ? ("IllegalStateException: EntityManagerFactory is closed" is thrown)
body: <p>I am trying to write a simple Spring Boot Program and practice @Async. I want to fetch all records of <code>teacher</code> entity asynchronously.</p>
  <p>my database access objects:</p>
  <pre><code>public interface TeacherService {
      List&lt;Teacher&gt; findAll(Pageable pageable);
  }
  </code></pre>
  <p>how it is implemented:</p>
  <pre><code> public class TeacherServiceImpl implements TeacherService{
      
          @Autowired
          private TeacherRepository repository;
      
          @Override
          public List&lt;Teacher&gt; findAll(Pageable pageable) {
              Page&lt;Teacher&gt; page =  repository.findAll(pageable);
              return page.get().collect(Collectors.toList());
          }
  }
  </code></pre>
  <p>my Async method:</p>
  <pre><code>@Component
  public class PrintAllTeacherDataBaseResult {
      @Autowired
      TeacherService teacher;
  
      @Async
      public AsyncResult&lt;List&lt;Teacher&gt;&gt; printPage(Pageable page){
          return new AsyncResult&lt;&gt;(teacher.findAll(page));
      }
  }
  </code></pre>
  <p>when I call <code>printPage</code> method without <code>@Async</code> annotation it works fine. but when I use it throws the following exception:</p>
  <pre><code>java.lang.IllegalStateException: Failed to execute CommandLineRunner
      at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:807) [spring-boot-2.4.2.jar:2.4.2]
      at org.springframework.boot.SpringApplication.callRunners(SpringApplication.java:788) [spring-boot-2.4.2.jar:2.4.2]
      at org.springframework.boot.SpringApplication.run(SpringApplication.java:333) [spring-boot-2.4.2.jar:2.4.2]
      at org.springframework.boot.SpringApplication.run(SpringApplication.java:1311) [spring-boot-2.4.2.jar:2.4.2]
      at org.springframework.boot.SpringApplication.run(SpringApplication.java:1300) [spring-boot-2.4.2.jar:2.4.2]
      at com.hatef.demo.DemoApplication.main(DemoApplication.java:97) [classes/:na]
  Caused by: java.lang.ClassCastException: org.springframework.util.concurrent.ListenableFutureTask cannot be cast to org.springframework.scheduling.annotation.AsyncResult
      at async.PrintAllTeacherDataBaseResult$$EnhancerBySpringCGLIB$$ccbb6656.printPage(&lt;generated&gt;) ~[classes/:na]
      at com.hatef.demo.DemoApplication.run(DemoApplication.java:103) [classes/:na]
      at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:804) [spring-boot-2.4.2.jar:2.4.2]
      ... 5 common frames omitted
  
  
      java.lang.IllegalStateException: EntityManagerFactory is closed
      at org.hibernate.internal.SessionFactoryImpl.validateNotClosed(SessionFactoryImpl.java:513) ~[hibernate-core-5.4.27.Final.jar:5.4.27.Final]
      at org.hibernate.internal.SessionFactoryImpl.getMetamodel(SessionFactoryImpl.java:660) ~[hibernate-core-5.4.27.Final.jar:5.4.27.Final]
      at org.hibernate.hql.internal.ast.util.SessionFactoryHelper.findEntityPersisterByName(SessionFactoryHelper.java:141) ~[hibernate-core-5.4.27.Final.jar:5.4.27.Final]
      at org.hibernate.hql.internal.ast.util.SessionFactoryHelper.requireClassPersister(SessionFactoryHelper.java:167) ~[hibernate-core-5.4.27.Final.jar:5.4.27.Final]
      at org.hibernate.hql.internal.ast.tree.FromElementFactory.addFromElement(FromElementFactory.java:91) ~[hibernate-core-5.4.27.Final.jar:5.4.27.Final]
      at org.hibernate.hql.internal.ast.tree.FromClause.addFromElement(FromClause.java:77) ~[hibernate-core-5.4.27.Final.jar:5.4.27.Final]
      at org.hibernate.hql.internal.ast.HqlSqlWalker.createFromElement(HqlSqlWalker.java:333) ~[hibernate-core-5.4.27.Final.jar:5.4.27.Final]
      at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.fromElement(HqlSqlBaseWalker.java:3758) ~[hibernate-core-5.4.27.Final.jar:5.4.27.Final]
      at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.fromElementList(HqlSqlBaseWalker.java:3647) ~[hibernate-core-5.4.27.Final.jar:5.4.27.Final]
      at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.fromClause(HqlSqlBaseWalker.java:732) ~[hibernate-core-5.4.27.Final.jar:5.4.27.Final]
      at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.query(HqlSqlBaseWalker.java:588) ~[hibernate-core-5.4.27.Final.jar:5.4.27.Final]
      at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.selectStatement(HqlSqlBaseWalker.java:325) ~[hibernate-core-5.4.27.Final.jar:5.4.27.Final]
      at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.statement(HqlSqlBaseWalker.java:273) ~[hibernate-core-5.4.27.Final.jar:5.4.27.Final]
      at org.hibernate.hql.internal.ast.QueryTranslatorImpl.analyze(QueryTranslatorImpl.java:276) ~[hibernate-core-5.4.27.Final.jar:5.4.27.Final]
      at org.hibernate.hql.internal.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:192) ~[hibernate-core-5.4.27.Final.jar:5.4.27.Final]
      at org.hibernate.hql.internal.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:144) ~[hibernate-core-5.4.27.Final.jar:5.4.27.Final]
      at org.hibernate.engine.query.spi.HQLQueryPlan.&lt;init&gt;(HQLQueryPlan.java:113) ~[hibernate-core-5.4.27.Final.jar:5.4.27.Final]
      at org.hibernate.engine.query.spi.HQLQueryPlan.&lt;init&gt;(HQLQueryPlan.java:73) ~[hibernate-core-5.4.27.Final.jar:5.4.27.Final]
      at org.hibernate.engine.query.spi.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:162) ~[hibernate-core-5.4.27.Final.jar:5.4.27.Final]
      at org.hibernate.internal.AbstractSharedSessionContract.getQueryPlan(AbstractSharedSessionContract.java:604) ~[hibernate-core-5.4.27.Final.jar:5.4.27.Final]
      at org.hibernate.internal.AbstractSharedSessionContract.createQuery(AbstractSharedSessionContract.java:716) ~[hibernate-core-5.4.27.Final.jar:5.4.27.Final]
      at org.hibernate.internal.AbstractSharedSessionContract.createQuery(AbstractSharedSessionContract.java:779) ~[hibernate-core-5.4.27.Final.jar:5.4.27.Final]
      at org.hibernate.query.criteria.internal.CriteriaQueryImpl$1.buildCompiledQuery(CriteriaQueryImpl.java:314) ~[hibernate-core-5.4.27.Final.jar:5.4.27.Final]
      at org.hibernate.query.criteria.internal.compile.CriteriaCompiler.compile(CriteriaCompiler.java:165) ~[hibernate-core-5.4.27.Final.jar:5.4.27.Final]
      at org.hibernate.internal.AbstractSharedSessionContract.createQuery(AbstractSharedSessionContract.java:742) ~[hibernate-core-5.4.27.Final.jar:5.4.27.Final]
      at org.hibernate.internal.AbstractSharedSessionContract.createQuery(AbstractSharedSessionContract.java:113) ~[hibernate-core-5.4.27.Final.jar:5.4.27.Final]
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_151]
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_151]
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_151]
      at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_151]
      at org.springframework.orm.jpa.SharedEntityManagerCreator$SharedEntityManagerInvocationHandler.invoke(SharedEntityManagerCreator.java:311) ~[spring-orm-5.3.3.jar:5.3.3]
      at com.sun.proxy.$Proxy101.createQuery(Unknown Source) ~[na:na]
      at org.springframework.data.jpa.repository.support.SimpleJpaRepository.getQuery(SimpleJpaRepository.java:703) ~[spring-data-jpa-2.4.3.jar:2.4.3]
      at org.springframework.data.jpa.repository.support.SimpleJpaRepository.getQuery(SimpleJpaRepository.java:654) ~[spring-data-jpa-2.4.3.jar:2.4.3]
      at org.springframework.data.jpa.repository.support.SimpleJpaRepository.findAll(SimpleJpaRepository.java:444) ~[spring-data-jpa-2.4.3.jar:2.4.3]
      at org.springframework.data.jpa.repository.support.SimpleJpaRepository.findAll(SimpleJpaRepository.java:411) ~[spring-data-jpa-2.4.3.jar:2.4.3]
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_151]
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_151]
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_151]
      at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_151]
      at org.springframework.data.repository.core.support.RepositoryMethodInvoker$RepositoryFragmentMethodInvoker.lambda$new$0(RepositoryMethodInvoker.java:289) ~[spring-data-commons-2.4.3.jar:2.4.3]
      at org.springframework.data.repository.core.support.RepositoryMethodInvoker.doInvoke(RepositoryMethodInvoker.java:137) ~[spring-data-commons-2.4.3.jar:2.4.3]
      at org.springframework.data.repository.core.support.RepositoryMethodInvoker.invoke(RepositoryMethodInvoker.java:121) ~[spring-data-commons-2.4.3.jar:2.4.3]
      at org.springframework.data.repository.core.support.RepositoryComposition$RepositoryFragments.invoke(RepositoryComposition.java:524) ~[spring-data-commons-2.4.3.jar:2.4.3]
      at org.springframework.data.repository.core.support.RepositoryComposition.invoke(RepositoryComposition.java:285) ~[spring-data-commons-2.4.3.jar:2.4.3]
      at org.springframework.data.repository.core.support.RepositoryFactorySupport$ImplementationMethodExecutionInterceptor.invoke(RepositoryFactorySupport.java:531) ~[spring-data-commons-2.4.3.jar:2.4.3]
      at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) [spring-aop-5.3.3.jar:5.3.3]
      at org.springframework.data.repository.core.support.QueryExecutorMethodInterceptor.doInvoke(QueryExecutorMethodInterceptor.java:156) ~[spring-data-commons-2.4.3.jar:2.4.3]
      at org.springframework.data.repository.core.support.QueryExecutorMethodInterceptor.invoke(QueryExecutorMethodInterceptor.java:131) ~[spring-data-commons-2.4.3.jar:2.4.3]
      at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) [spring-aop-5.3.3.jar:5.3.3]
      at org.springframework.data.projection.DefaultMethodInvokingMethodInterceptor.invoke(DefaultMethodInvokingMethodInterceptor.java:80) ~[spring-data-commons-2.4.3.jar:2.4.3]
      at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) [spring-aop-5.3.3.jar:5.3.3]
      at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:123) ~[spring-tx-5.3.3.jar:5.3.3]
      at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:388) ~[spring-tx-5.3.3.jar:5.3.3]
      at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:119) [spring-tx-5.3.3.jar:5.3.3]
      at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) [spring-aop-5.3.3.jar:5.3.3]
      at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:137) [spring-tx-5.3.3.jar:5.3.3]
      at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) [spring-aop-5.3.3.jar:5.3.3]
      at org.springframework.data.jpa.repository.support.CrudMethodMetadataPostProcessor$CrudMethodMetadataPopulatingMethodInterceptor.invoke(CrudMethodMetadataPostProcessor.java:174) [spring-data-jpa-2.4.3.jar:2.4.3]
      at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) [spring-aop-5.3.3.jar:5.3.3]
      at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:97) [spring-aop-5.3.3.jar:5.3.3]
      at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) [spring-aop-5.3.3.jar:5.3.3]
      at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:215) [spring-aop-5.3.3.jar:5.3.3]
      at com.sun.proxy.$Proxy105.findAll(Unknown Source) [na:na]
      at services.TeacherServiceImpl.findAll(TeacherServiceImpl.java:33) [classes/:na]
      at async.PrintAllTeacherDataBaseResult.printPage(PrintAllTeacherDataBaseResult.java:23) [classes/:na]
      at async.PrintAllTeacherDataBaseResult$$FastClassBySpringCGLIB$$ccfcfab4.invoke(&lt;generated&gt;) [classes/:na]
      at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:218) [spring-core-5.3.3.jar:5.3.3]
      at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:779) [spring-aop-5.3.3.jar:5.3.3]
      at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163) [spring-aop-5.3.3.jar:5.3.3]
      at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.proceed(CglibAopProxy.java:750) [spring-aop-5.3.3.jar:5.3.3]
      at org.springframework.aop.interceptor.AsyncExecutionInterceptor.lambda$invoke$0(AsyncExecutionInterceptor.java:115) [spring-aop-5.3.3.jar:5.3.3]
      at java.util.concurrent.FutureTask.run$$$capture(FutureTask.java:266) ~[na:1.8.0_151]
      at java.util.concurrent.FutureTask.run(FutureTask.java) ~[na:1.8.0_151]
      at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) ~[na:1.8.0_151]
      at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) ~[na:1.8.0_151]
      at java.lang.Thread.run(Thread.java:748) ~[na:1.8.0_151]
  </code></pre>
  <p>and how I call it:</p>
  <pre><code>@Autowired
  ApplicationContextProvider applicationContextProvider;
  
  public static void main(String[] args) {
      SpringApplication.run(DemoApplication.class, args);
  }
  
  @Override
  public void run(String...args) throws Exception {
      PrintAllTeacherDataBaseResult p = applicationContextProvider.getApplicationContext().getBean(PrintAllTeacherDataBaseResult.class);
      AsyncResult&lt;List&lt;Teacher&gt;&gt; a = p.printPage(PageRequest.of(0, 2));
      a.get().stream().forEach(System.out::println);
  }
  </code></pre>
  <p>but my question is  why it throws exception.</p>
  <hr>
  <p><strong>update</strong></p>
  <p>I don't know why but I changed <code>@EnableAsync</code> to <code>@EnableAsync(mode = AdviceMode.ASPECTJ)</code> and everything worked! <strong>but it is not created new thread</strong></p>
  <p>now my question is why it throws exception when using <code>@EnableAsync</code> and why it works when <code>@EnableAsync(mode = AdviceMode.ASPECTJ)</code></p>
  <hr>
  <p><strong>update</strong></p>
  <p>I could solve my problem by applying these changes to my code:
  first revert  <code>@EnableAsync(mode = AdviceMode.ASPECTJ)</code> to <code>@EnableAsync</code> and then:</p>
  <pre><code>public interface TeacherService {
      @Async
      Future&lt;List&lt;Teacher&gt;&gt; findAll(Pageable pageable);
  }
  </code></pre>
  <p>and how it is implemented:</p>
  <pre><code>public class TeacherServiceImpl implements TeacherService{
  
          @Autowired
          private TeacherRepository repository;
  
          @Override
          public Future&lt;List&lt;Teacher&gt;&gt; findAll(Pageable pageable) {
               Page&lt;Teacher&gt; page =  repository.findAll(pageable);
               return new AsyncResult&lt;&gt;(page.get().collect(Collectors.toList()));
      }
  }
  </code></pre>
  <p>and how I ran above methods:</p>
  <pre><code>@Autowired
  ApplicationContextProvider applicationContextProvider;
  
  public static void main(String[] args) {
      SpringApplication.run(DemoApplication.class, args);
  }
  
  @Override
  public void run(String...args) throws Exception {
      PrintAllTeacherDataBaseResult p = applicationContextProvider.getApplicationContext().getBean(PrintAllTeacherDataBaseResult.class);
      Future&lt;List&lt;Teacher&gt;&gt; a = p.printPage(PageRequest.of(0, 2));
      a.get().forEach(System.out::println);
  }
  </code></pre>
  <p>I appreciate any help that clarifies exactly what is going on.</p>