【发布时间】:2019-09-20 15:41:51
【问题描述】:
我正在开发 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 千(!)行 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 中的 deuceMainApplicationClass() 将返回 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
【问题讨论】:
-
您使用的是嵌入式数据库?我没有看到你的 mysql 属性
-
这绝对是奇怪的。你能发布一些 SQL 语句吗?
标签: mysql hibernate spring-boot javafx spring-data-jpa