Spring boot执行没有错误,但是服务器上没有创建表

Jav*_*hir 5 java mysql sql spring spring-boot

大部分代码都是从旧项目中复制的,只不过这次我使用 Gradle 而不是 maven。

当我通过 IntelliJ 在本地计算机上运行该项目时,会创建新表并且一切正常。

但是,当我将其上传到tomcat时,我在数据库上看不到表。当我检查端点并尝试保存新数据时,这是可能的,并且保存后我可以访问它们。

当项目在tomcat中时,数据是可以访问的。一旦我重新加载/取消部署并再次部署,数据就会丢失,并且我无法通过服务访问数据。直到我保存新数据。也许它与“内存数据库”有关。

应用程序属性:

spring.jpa.hibernate.ddl-auto=update
spring.datasource.driverClassName=com.mysql.jdbc.Driver

spring.datasource.url=jdbc:mysql://localhost:3306/database_name?autoReconnect=true&useSSL=false&createDatabaseIfNotExist=true
spring.datasource.username=username
spring.datasource.password=password

spring.jta.enabled=true

spring.datasource.hikari.minimum-idle=10
spring.datasource.hikari.maximum-pool-size=300
spring.datasource.hikari.idleTimeout=30000
spring.datasource.hikari.maxLifetime=2000000
spring.datasource.hikari.connectionTimeout=30000
Run Code Online (Sandbox Code Playgroud)

我检查了几个小时也没找到问题所在。我登录服务器并检查 Catalina 日志,没有错误消息。它们或多或少是相同的,直到 IntelliJ 创建表时,它在 Catalina.out 上丢失了。

org.hibernate.SQL : create table table_name ...
Run Code Online (Sandbox Code Playgroud)

主类位于包 xy 中,实体位于 xydatabase.tables 中。

从 catalina.out 登录:

30-May-2021 23:15:40.921 INFO [ajp-nio-8009-exec-356] org.apache.jasper.servlet.TldScanner.scanJars At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
30-May-2021 23:15:41.009 INFO [ajp-nio-8009-exec-356] org.apache.catalina.core.ApplicationContext.log 2 Spring WebApplicationInitializers detected on classpath

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::                (v2.5.0)

2021-05-30 23:15:41.778  INFO 1454 --- [o-8009-exec-356] com.global.ServletInitializer            : Starting ServletInitializer using Java 1.8.0_292 on ubuntu-droplet1 with PID 1454 (/opt/tomcat/temp/50-global##10049/WEB-INF/classes started by tomcat in /)
2021-05-30 23:15:41.782  INFO 1454 --- [o-8009-exec-356] com.global.ServletInitializer            : No active profile set, falling back to default profiles: default
2021-05-30 23:15:42.051  WARN 1454 --- [kground-preinit] o.s.h.c.j.Jackson2ObjectMapperBuilder    : For Jackson Kotlin classes support please add "com.fasterxml.jackson.module:jackson-module-kotlin" to the classpath
2021-05-30 23:15:43.027  INFO 1454 --- [o-8009-exec-356] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data JPA repositories in DEFAULT mode.
2021-05-30 23:15:43.150  INFO 1454 --- [o-8009-exec-356] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 105 ms. Found 1 JPA repository interfaces.
30-May-2021 23:15:43.881 INFO [ajp-nio-8009-exec-356] org.apache.catalina.core.ApplicationContext.log Initializing Spring embedded WebApplicationContext
2021-05-30 23:15:43.881  INFO 1454 --- [o-8009-exec-356] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 2023 ms
2021-05-30 23:15:43.931 TRACE 1454 --- [o-8009-exec-369] 
2021-05-30 23:15:44.372  INFO 1454 --- [o-8009-exec-356] com.zaxxer.hikari.HikariDataSource       : HikariPool-28 - Starting...
Loading class `com.mysql.jdbc.Driver'. This is deprecated. The new driver class is `com.mysql.cj.jdbc.Driver'. The driver is automatically registered via the SPI and manual loading of the driver class is generally unnecessary.
2021-05-30 23:15:44.704  INFO 1454 --- [o-8009-exec-356] com.zaxxer.hikari.HikariDataSource       : HikariPool-28 - Start completed.
2021-05-30 23:15:44.796  INFO 1454 --- [o-8009-exec-356] o.hibernate.jpa.internal.util.LogHelper  : HHH000204: Processing PersistenceUnitInfo [name: default]
2021-05-30 23:15:44.910  INFO 1454 --- [o-8009-exec-356] org.hibernate.Version                    : HHH000412: Hibernate ORM core version 5.4.31.Final
2021-05-30 23:15:45.199  INFO 1454 --- [o-8009-exec-356] o.hibernate.annotations.common.Version   : HCANN000001: Hibernate Commons Annotations {5.1.2.Final}
2021-05-30 23:15:45.504  INFO 1454 --- [o-8009-exec-356] org.hibernate.dialect.Dialect            : HHH000400: Using dialect: org.hibernate.dialect.H2Dialect
2021-05-30 23:15:46.812  INFO 1454 --- [o-8009-exec-356] o.h.e.t.j.p.i.JtaPlatformInitiator       : HHH000490: Using JtaPlatform implementation: [org.hibernate.engine.transaction.jta.platform.internal.NoJtaPlatform]
2021-05-30 23:15:46.829  INFO 1454 --- [o-8009-exec-356] j.LocalContainerEntityManagerFactoryBean : Initialized JPA EntityManagerFactory for persistence unit 'default'
2021-05-30 23:15:47.884  WARN 1454 --- [o-8009-exec-356] JpaBaseConfiguration$JpaWebConfiguration : spring.jpa.open-in-view is enabled by default. Therefore, database queries may be performed during view rendering. Explicitly configure spring.jpa.open-in-view to disable this warning
2021-05-30 23:15:48.265  INFO 1454 --- [o-8009-exec-356] o.s.s.web.DefaultSecurityFilterChain     : Creating filter chain: Ant [pattern='/**'], [org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter@1177a1e1, org.springframework.security.web.context.SecurityContextPersistenceFilter@3f4454d9, org.springframework.security.web.header.HeaderWriterFilter@132a4fd3, org.springframework.security.web.authentication.logout.LogoutFilter@24276bb0, org.springframework.security.web.authentication.www.BasicAuthenticationFilter@5f273afb, org.springframework.security.web.savedrequest.RequestCacheAwareFilter@5ef67d5b, org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter@7361a373, org.springframework.security.web.authentication.AnonymousAuthenticationFilter@5e9936ce, org.springframework.security.web.session.SessionManagementFilter@3e4a0114, org.springframework.security.web.access.ExceptionTranslationFilter@14437cb5, org.springframework.security.web.access.intercept.FilterSecurityInterceptor@1ca8217d]
2021-05-30 23:15:49.023  INFO 1454 --- [o-8009-exec-356] com.global.ServletInitializer            : Started ServletInitializer in 7.871 seconds (JVM running for 24656.257)
2021-05-30 23:15:49.025  INFO 1454 --- [o-8009-exec-356] o.s.b.a.ApplicationAvailabilityBean      : Application availability state LivenessState changed to CORRECT
2021-05-30 23:15:49.028  INFO 1454 --- [o-8009-exec-356] o.s.b.a.ApplicationAvailabilityBean      : Application availability state ReadinessState changed to ACCEPTING_TRAFFIC
30-May-2021 23:15:49.036 INFO [ajp-nio-8009-exec-356] org.apache.catalina.startup.HostConfig.deployWAR Deployment of web application archive [/opt/tomcat/some_name/global##10049.war] has finished in [11,445] ms
30-May-2021 23:15:49.036 INFO [ajp-nio-8009-exec-356] org.apache.catalina.core.ApplicationContext.log HTMLManager: list: Listing contexts for virtual host '**domain**'
30-May-2021 23:16:01.419 INFO [ajp-nio-8009-exec-369] org.apache.catalina.core.ApplicationContext.log HTMLManager: list: Listing contexts for virtual host '**domain**'
30-May-2021 23:16:02.012 INFO [ajp-nio-8009-exec-372] org.apache.catalina.core.ApplicationContext.log HTMLManager: list: Listing contexts for virtual host '**domain**'
30-May-2021 23:16:04.502 INFO [ajp-nio-8009-exec-371] org.apache.catalina.core.ApplicationContext.log Initializing Spring DispatcherServlet 'dispatcherServlet'
2021-05-30 23:16:04.503  INFO 1454 --- [o-8009-exec-371] o.s.web.servlet.DispatcherServlet        : Initializing Servlet 'dispatcherServlet'
2021-05-30 23:16:04.505  INFO 1454 --- [o-8009-exec-371] o.s.web.servlet.DispatcherServlet        : Completed initialization in 2 ms
Run Code Online (Sandbox Code Playgroud)

And*_*cus 3

您缺少某些配置,例如generate-dll。请尝试以下操作:

spring.datasource.url=jdbc:mysql://localhost:3306/dbName
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.username=username
spring.datasource.password=password
spring.jpa.generate-ddl=true
spring.jpa.hibernate.ddl-auto=update
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
Run Code Online (Sandbox Code Playgroud)

我使用了不同的驱动程序类,因为您的属性中的驱动程序类已被弃用(这与您的问题无关)。

PS:我同意这些评论 -update不应该在生产中使用。我什至不会依赖 hibernate 来管理数据库的架构,而是使用 Flyway / liquibase 等工具。