Javanshir
Javanshir

Reputation: 792

Spring boot executes without error, however, no tables are created on the server

Most of the code is copied from the old projects, except this time I use Gradle instead of maven.

When I run the project on my local machine via IntelliJ, the new tables are created and everything works.

However, when I upload it to tomcat, I don't see the tables on the database. When I check the endpoints and try to save new data, it is possible and after saving I can access them.

While the project is in tomcat, the data is accessible. As soon as I reload/undeploy and deploy again, the data is lost and I can't access the data via services. Until I save new data. Maybe it has something to do with "in-memory databases".

application.properties:

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

I checked for hours and couldn't find the problem. I log into the server and checked the catalina logs, no error messages. They are more or less identical until the point where the IntelliJ creates the tables, it is missing on Catalina.out.

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

Main class is in package x.y, and entities are in x.y.database.tables.

log from 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

Upvotes: 5

Views: 1573

Answers (2)

Vikas Adyar
Vikas Adyar

Reputation: 148

The problem which you are facing is a classic issue. Before rectifying this issue let me tell you that there are few coding standards which are missing here. Solving this would help you create a robust and close to production-ready code.

  • Spring boot comes with embedded tomcat. So you don't need to upload the artifact to another tomcat server. (Correct me if I have got it wrong. You can use this command to start the server java -jar <JAR_FILE_NAME>.jar)

  • Manage your database separately. You could use Flyway or Liquibase to handle database migration. This way your schema will be clean. With this you could change spring.jpa.hibernate.ddl-auto to validate or totally get rid if this property.

  • Add spring.jpa.show-sql=true to log the database queries (Used for debugging and not in production).

Addressing your query.

  • Please get rid of the spring.jpa.hibernate.ddl-auto and use the latest mysql driver which is com.mysql.cj.jdbc.Driver.
  • Also make sure that you are checking the right database for the data. (Also you could get rid of any additional dependencies in your project which you are not using. Like H2 database which corresponds to in-memory DB. This is unlikely as the logs are clearly showing the driver used is for Mysql.)

Upvotes: 1

Andronicus
Andronicus

Reputation: 26066

You're missing on some configuration, for example generate-dll. Try with the following:

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

I've used different driver class, because the one from your properties is deprecated (which is unrelated to your problem).

P.S.: I agree with the comments - update shouldn't be used in production. I wouldn't even rely on hibernate to manage the schema of my database, but rather use tools like flyway / liquibase.

Upvotes: 3

Related Questions