gpt4 book ai didi

mysql - SpringApplication.run() 加载整个数据库

转载 作者:行者123 更新时间:2023-11-29 15:37:58 26 4
gpt4 key购买 nike

我正在开发 Spring Boot、MySQL、JavaFX、客户端服务器应用程序 - 没有网络 - 并且产生了令人惊讶的效果,尽管我没有更改 UI 中的任何实体,但我收到了一个 ObjectOptimisticLockingFailureException 异常,提示“行已更新或被另一笔交易删除”。所以我想知道是什么 - 如果不是我 - 正在更新这个实体,并开始通过打开进行调试

spring.jpa.show-sql=true
spring.jpa.properties.hibernate.use_sql_comments=true
spring.jpa.properties.hibernate.format_sql=true
logging.level.org.hibernate.type=TRACE

在我的属性文件中查看应用程序和数据库之间发生了什么。我发现了一些我根本不明白的东西:

当应用程序启动时,就在开始时,据我所知,在调用我的任何代码之前,会调用 SpringApplication.run(..) :

@Override
public void init() throws Exception {
springContext = SpringApplication.run(ProdMgrApp.class);
..
}

当我在调试器中执行此命令时 - 但如果不从调试器运行应用程序 - 应用程序会生成 563,000 (!) 行 SQL 代码,基本上查询整个数据库 - 数千个选择,超过 100 个更新和大约 400 个插入。有趣的是,尽管有插入语句,数据库内容并没有加倍或扩展。但是乐观锁(@Version)的整数版本信息正在增加。在某种程度上,它并没有什么害处,但是需要一段时间 - 而且也没有在控制台上调试语句 - 并且当数据库增长时,..这是不行的。

我做错了什么?

尽管我现在正在使用 Spring Boot,特别是 JPA 部分,但我距离成为专家还很远。如果您需要更多信息,请告诉我。

编辑:我调试了一下,意识到因为我结合了 JavaFX 和 Spring Boot,所以应用程序的启动与“正常”设置不同。在非 JavaFX 应用程序中,SpringApplication.run() 调用位于 main() 中。在 JavaFX 应用程序中,该调用位于 init() 中 - 另请参阅 https://better-coding.com/javafx-spring-boot-gradle-project-setup-guide-and-test/,因为 SpringApplication 内的 deduceMainApplicationClass() 将返回 null。这可能是根本原因吗?

跟踪看起来像这样:

INFO 17:00 o.s.b.StartupInfoLogger.logStarting:50: Starting application on ThinkPad with PID 6664 (started by Alexander in C:\Users\Alexander\Documents\Codebase\agiletunes-codespace\agiletunes-productmanager) 
INFO 17:00 o.s.b.SpringApplication.logStartupProfileInfo:646: No active profile set, falling back to default profiles: default
INFO 17:00 o.s.d.r.c.RepositoryConfigurationDelegate.registerRepositoriesIn:126: Bootstrapping Spring Data repositories in DEFAULT mode.
INFO 17:00 o.s.d.r.c.RepositoryConfigurationDelegate.registerRepositoriesIn:182: Finished Spring Data repository scanning in 1151ms. Found 39 repository interfaces.
INFO 17:00 c.z.h.HikariDataSource.getConnection:110: HikariPool-1 - Starting...
INFO 17:00 c.z.h.HikariDataSource.getConnection:123: HikariPool-1 - Start completed.
INFO 17:00 o.h.j.i.u.LogHelper.logPersistenceUnitInformation:31: HHH000204: Processing PersistenceUnitInfo [
name: default
...]
INFO 17:00 o.h.Version.logVersion:46: HHH000412: Hibernate Core {5.3.10.Final}
INFO 17:00 o.h.c.Environment.<clinit>:213: HHH000206: hibernate.properties not found
INFO 17:00 o.h.a.c.r.j.JavaReflectionManager.<clinit>:49: HCANN000001: Hibernate Commons Annotations {5.0.4.Final}
DEBUG 17:00 o.h.t.BasicTypeRegistry.register:156: Adding type registration boolean -> org.hibernate.type.BooleanType@5ffacf79
DEBUG 17:00 o.h.t.BasicTypeRegistry.register:156: Adding type registration boolean -> org.hibernate.type.BooleanType@5ffacf79
DEBUG 17:00 o.h.t.BasicTypeRegistry.register:156: Adding type registration java.lang.Boolean -> org.hibernate.type.BooleanType@5ffacf79
DEBUG 17:00 o.h.t.BasicTypeRegistry.register:156: Adding type registration numeric_boolean -> org.hibernate.type.NumericBooleanType@4058800
DEBUG 17:00 o.h.t.BasicTypeRegistry.register:156: Adding type registration true_false -> org.hibernate.type.TrueFalseType@46bb075a
DEBUG 17:00 o.h.t.BasicTypeRegistry.register:156: Adding type registration yes_no -> org.hibernate.type.YesNoType@7d390456
.. more lines of registrations and ParameterValues ..
DEBUG 17:01 o.h.t.EnumType.setParameterValues:126: Using NAMED-based conversion for Enum com.agiletunes.shared.domain.risk.Risk$Severity
DEBUG 17:01 o.h.t.EnumType.setParameterValues:126: Using NAMED-based conversion for Enum com.agiletunes.shared.domain.risk.Risk$Type
DEBUG 17:01 o.h.t.EnumType.setParameterValues:126: Using NAMED-based conversion for Enum com.agiletunes.shared.domain.risk.Risk$Type
TRACE 17:01 o.h.t.s.TypeConfiguration.sessionFactoryCreated:195: Handling #sessionFactoryCreated from [org.hibernate.internal.SessionFactoryImpl@25b9be87] for TypeConfiguration
INFO 17:01 o.s.o.j.AbstractEntityManagerFactoryBean.buildNativeEntityManagerFactory:415: Initialized JPA EntityManagerFactory for persistence unit 'default'
INFO 17:01 o.s.b.StartupInfoLogger.logStarted:59: Started application in 20.227 seconds (JVM running for 22.033)
INFO 17:01 o.h.h.i.QueryTranslatorFactoryInitiator.initiateService:47: HHH000397: Using ASTQueryTranslatorFactory
Hibernate:
/* select
generatedAlias0
from
Product as generatedAlias0 */ select
product0_.id as id2_59_,
product0_.goal as goal3_59_,
product0_.identifier as identifi4_59_,
product0_.level as level5_59_,
product0_.parent_id as parent_25_59_,
product0_.plannedBegin as plannedB6_59_,
product0_.plannedEnd as plannedE7_59_,

followed by thousands lines of SQL

这是我的属性文件:

#No JMX needed - disabling it allows for faster startup
spring.jmx.enabled=false
spring.main.banner-mode=off
#no web server needed
spring.main.web-application-type=none

# Properties can be queried in the code e.g. @Value(value = "${spring.datasource.driver-class-name}") private String message;
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://127.0.0.1/agiletunesdb?useSSL=false&serverTimezone=Europe/Berlin&useUnicode=true&characterEncoding=utf-8&characterSetResults=utf-8
spring.datasource.username=YYYYYY
spring.datasource.password=XXXXXX

# create db schema
#spring.jpa.hibernate.ddl-auto=create
#spring.jpa.hibernate.ddl-auto=update

#---- Naming strategy: Use underscore instead of camel case
spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl

#---- Prevent use of deprecated [org.hibernate.id.MultipleHiLoPerTableGenerator] table-based id generator
spring.jpa.hibernate.use-new-id-generator-mappings=true

# The SQL dialect makes Hibernate generate better SQL for the chosen database
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5Dialect

#---- Show sql queries send to db
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.use_sql_comments=true
#---- Print SQL statements spread over multiple lines for easier readibility
spring.jpa.properties.hibernate.format_sql=true
#---- show parameter values in sql statements complemental to "?"
logging.level.org.hibernate.type=TRACE


#---- Switch on colors
spring.output.ansi.enabled=ALWAYS
logging.pattern.console=%highlight(%5p) %d{HH:mm} %C{3}.%method:%L: %msg %n

最佳答案

问题是我有一个 CommandLineRunner,它应该在数据库为空时创建测试数据。此 CommandLineRunner 尝试从某个表中读取所有实体,如果没有返回任何内容,则会填充所有表。问题在于相关实体的延迟加载出了问题。我必须对此进行调查,如果需要,我会提出一个新问题。

关于mysql - SpringApplication.run() 加载整个数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58031490/

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