Spring引导执行时没有出现错误,但是,服务器上没有创建任何表



除了这次我使用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

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

org.hibernate.SQL : create table table_name ...

主类在包x.y中,实体在x.y.database.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

您缺少某些配置,例如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

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

附言:我同意这些评论——update不应该在生产中使用。我甚至不会依赖hibernate来管理数据库的模式,而是使用flyway/likibase之类的工具。

您所面临的问题是一个经典问题。在纠正这个问题之前,让我告诉你,这里缺少一些编码标准。解决这个问题将帮助您创建一个健壮的、接近生产的代码。

  • Spring引导带有嵌入式tomcat。因此,您不需要将工件上传到另一个tomcat服务器。(如果我弄错了,请纠正我。你可以用这个命令启动服务器java -jar <JAR_FILE_NAME>.jar(

  • 单独管理数据库。您可以使用Flyway或Liquibase来处理数据库迁移。这样,您的架构将是干净的。有了这个,你可以将spring.jpa.hibernate.ddl-auto更改为validate,或者完全去掉这个属性。

  • 添加spring.jpa.show-sql=true以记录数据库查询(用于调试,不用于生产(。

解决您的查询。

  • 请去掉spring.jpa.hibernate.ddl-auto,使用最新的mysql驱动程序com.mysql.cj.jdbc.Driver
  • 还要确保您正在检查正确的数据库中的数据。(此外,你可以在你的项目中消除任何你没有使用的额外依赖项。比如H2数据库,它对应于内存中的DB。这是不可能的,因为日志清楚地显示使用的驱动程序是用于Mysql的。(

相关内容

最新更新