gpt4 book ai didi

java - 为什么我无法在@Async 方法中取回结果? ("IllegalStateException: EntityManagerFactory is closed"被抛出)

转载 作者:行者123 更新时间:2023-12-03 11:19:32 26 4
gpt4 key购买 nike

我正在尝试编写一个简单的 Spring Boot 程序并练习 @Async。我想获取 teacher 的所有记录实体异步。
我的数据库访问对象:

public interface TeacherService {
List<Teacher> findAll(Pageable pageable);
}
它是如何实现的:
 public class TeacherServiceImpl implements TeacherService{

@Autowired
private TeacherRepository repository;

@Override
public List<Teacher> findAll(Pageable pageable) {
Page<Teacher> page = repository.findAll(pageable);
return page.get().collect(Collectors.toList());
}
}
我的异步方法:
@Component
public class PrintAllTeacherDataBaseResult {
@Autowired
TeacherService teacher;

@Async
public AsyncResult<List<Teacher>> printPage(Pageable page){
return new AsyncResult<>(teacher.findAll(page));
}
}
当我打电话时 printPage没有 @Async 的方法注释它工作正常。但是当我使用它时会抛出以下异常:
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(<generated>) ~[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.<init>(HQLQueryPlan.java:113) ~[hibernate-core-5.4.27.Final.jar:5.4.27.Final]
at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(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(<generated>) [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]
以及我如何称呼它:
@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<List<Teacher>> a = p.printPage(PageRequest.of(0, 2));
a.get().stream().forEach(System.out::println);
}
但我的问题是为什么它会抛出异常。

更新
不知道为什么,我改了 @EnableAsync@EnableAsync(mode = AdviceMode.ASPECTJ)一切正常! 但它没有创建新线程
现在我的问题是为什么它在使用 @EnableAsync 时会抛出异常以及为什么它在 @EnableAsync(mode = AdviceMode.ASPECTJ) 时有效

更新
我可以通过将这些更改应用于我的代码来解决我的问题:
第一次回复 @EnableAsync(mode = AdviceMode.ASPECTJ)@EnableAsync进而:
public interface TeacherService {
@Async
Future<List<Teacher>> findAll(Pageable pageable);
}
以及它是如何实现的:
public class TeacherServiceImpl implements TeacherService{

@Autowired
private TeacherRepository repository;

@Override
public Future<List<Teacher>> findAll(Pageable pageable) {
Page<Teacher> page = repository.findAll(pageable);
return new AsyncResult<>(page.get().collect(Collectors.toList()));
}
}
以及我如何运行上述方法:
@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<List<Teacher>> a = p.printPage(PageRequest.of(0, 2));
a.get().forEach(System.out::println);
}
我感谢任何能够准确说明正在发生的事情的帮助。

最佳答案

你应该看看这篇文章https://www.baeldung.com/spring-async
首先,返回类型的异步方法应该包含在 Future 中。
每次在特征对象上调用 get() 时,您都会订阅结果,当前线程将等待直到计算结果。
更新:
首先尝试阅读 AsyncExecutionInterceptor 和 Async 的 javadoc。

<p>In terms of target method signatures, any parameter types are supported.
However, the return type is constrained to either {@code void} or
{@link java.util.concurrent.Future}. In the latter case, you may declare the
more specific {@link org.springframework.util.concurrent.ListenableFuture} or
{@link java.util.concurrent.CompletableFuture} types which allow for richer
interaction with the asynchronous task and for immediate composition with
further processing steps.
它是一个 API,你应该遵循它。
在第 1 种情况下,您使用的是 AsyncResult。它是不允许的类型,但它继承自 ListenableFuture。
现在在 AsyncExecutionAspectSupport 的这个方法中
@Nullable
protected Object doSubmit(Callable<Object> task, AsyncTaskExecutor executor, Class<?> returnType) {
if (CompletableFuture.class.isAssignableFrom(returnType)) {
return CompletableFuture.supplyAsync(() -> {
try {
return task.call();
}
catch (Throwable ex) {
throw new CompletionException(ex);
}
}, executor);
}
else if (ListenableFuture.class.isAssignableFrom(returnType)) {
return ((AsyncListenableTaskExecutor) executor).submitListenable(task);
}
else if (Future.class.isAssignableFrom(returnType)) {
return executor.submit(task);
}
else {
executor.submit(task);
return null;
}
}
你进入了第二个 else 语句,后来它提供了 ClassCastException (你可以更深入地调试它)。
关于版本 2:
Future<List<Teacher>> a = p.printPage(PageRequest.of(0, 2));
实际上你没有调用printPage,你只是创建了一个future,一旦你开始执行它就会在里面调用它。
它将从 a.get() 开始,执行将在单独的线程中启动(如果您配置的线程执行器具有超过 1 个线程)。

关于java - 为什么我无法在@Async 方法中取回结果? ("IllegalStateException: EntityManagerFactory is closed"被抛出),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66005908/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com