【发布时间】:2020-07-08 15:10:07
【问题描述】:
我尝试实现一个 RESTful WebService,它能够直接从数据库中流式传输数百万条记录。 我正在使用 SpringBoot 2.2.5、Hibernate 5 和 PostgreSQL 11
根据这篇文章: https://www.airpair.com/java/posts/spring-streams-memory-efficiency
需要一步将标志“allowResultAccessAfterCompletion”设置为真。 但是如何在 Spring Boot 中做到这一点?
到目前为止,我的应用程序中没有任何 SessionFactory、EntityManagerFactory、Datasource 等配置。一切都由 SpringBoot 自动配置。
如果我在下面添加建议的配置,应用程序将无法启动,因为缺少 SessionFactory。
@Configuration
@EnableTransactionManagement
public class DataConfig {
@Autowired @Bean
public PlatformTransactionManager txManager(SessionFactory sf) {
HibernateTransactionManager mgr = new HibernateTransactionManager(sf);
mgr.setAllowResultAccessAfterCompletion(true);
return mgr;
}
... (the rest of your data config, including the LocalSessionFactoryBean) ...
}
如果我通过从 EntityManagerFactory 解包来提供 SessionFactory bean,我会得到另一个异常:
通过字段 'entityManagerFactory' 表达的不满足的依赖关系;嵌套异常是 org.springframework.beans.factory.BeanCurrentlyInCreationException:创建名为“getSessionFactory”的 bean 时出错:当前正在创建请求的 bean:是否存在无法解析的循环引用?
有人对我的设置有有效的配置吗?
这个标志不能简单地由 application.properties 中的一些配置值设置吗?
谢谢!
【问题讨论】:
-
你的 pom.xml 中是否添加了
spring-boot-starter-data-jpamaven 依赖项?还在application.properties/application.yml中添加适当的密钥(驱动程序类名称、用户名、密码、url)
标签: java spring-boot hibernate spring-transactions transactionmanager