Search code examples
springspring-boothibernateh2application.properties

SpringBoot - My project is using a database in memory, and I don't know how to stop doing that and change to an actual persistent db


I have created a Spring project using a h2 database (in memory) to work with it while I was starting with the project. Now, I want to work with persistent data as my project is done. The problem it, I can't switch and I don't see where the problem is. These are my application.properties:

spring.application.name=picoopspring

#Database
spring.datasource.url=jdbc:mysql://localhost:3306/picoopspring
spring.datasource.username=johndoe
spring.datasource.password=johndoe
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver

spring.jpa.database-platform=org.hibernate.dialect.MySQLDialect
spring.jpa.defer-datasource-initialization=true
spring.jpa.show-sql=true
  
spring.jpa.hibernate.ddl-auto=update

spring.h2.console.enabled=true

This is my pom.xml:


    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        
        <dependency>
            <groupId>org.springdoc</groupId>
            <artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
            <version>2.0.3</version>
        </dependency>

        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-validator</artifactId>
            <version>8.0.0.Final</version>
        </dependency>

        <dependency>
            <groupId>org.modelmapper</groupId>
            <artifactId>modelmapper</artifactId>
            <version>3.1.1</version>
        </dependency>

        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.mockito</groupId>
            <artifactId>mockito-core</artifactId>
            <version>5.11.0</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.28</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>
        
        
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.22</version>
            <scope>provided</scope>
        </dependency>
        
        <dependency>
            <groupId>io.jsonwebtoken</groupId>
            <artifactId>jjwt-impl</artifactId>
            <version>0.11.5</version>
        </dependency>
        <dependency>
            <groupId>io.jsonwebtoken</groupId>
            <artifactId>jjwt-api</artifactId>
            <version>0.11.5</version>
        </dependency>
        <dependency>
            <groupId>io.jsonwebtoken</groupId>
            <artifactId>jjwt-jackson</artifactId>
            <version>0.11.5</version>
        </dependency>

    </dependencies>

I I have the database created in my mysql, with no tables on it. The logs are the next ones:

2024-05-10T19:41:43.115+02:00  INFO 15620 --- [           main] project.picoop.PicoopApplication         : No active profile set, falling back to 1 default profile: "default"

2024-05-10T19:41:44.320+02:00  INFO 15620 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data JPA repositories in DEFAULT mode.

2024-05-10T19:41:44.370+02:00  INFO 15620 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 44 ms. Found 3 JPA repository interfaces.

2024-05-10T19:41:45.180+02:00  INFO 15620 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port 8080 (http)

2024-05-10T19:41:45.200+02:00  INFO 15620 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]

2024-05-10T19:41:45.200+02:00  INFO 15620 --- [           main] o.apache.catalina.core.StandardEngine    : Starting Servlet engine: [Apache Tomcat/10.1.19]

2024-05-10T19:41:45.316+02:00  INFO 15620 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext

2024-05-10T19:41:45.319+02:00  INFO 15620 --- [           main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 2146 ms

2024-05-10T19:41:45.469+02:00  INFO 15620 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Starting...

2024-05-10T19:41:45.734+02:00  INFO 15620 --- [           main] com.zaxxer.hikari.pool.HikariPool        : HikariPool-1 - Added connection conn0: url=jdbc:h2:mem:7f78d327-0c69-4c8a-8962-3481c091e42a user=SA

2024-05-10T19:41:45.735+02:00  INFO 15620 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Start completed.

2024-05-10T19:41:45.804+02:00  INFO 15620 --- [           main] o.hibernate.jpa.internal.util.LogHelper  : HHH000204: Processing PersistenceUnitInfo [name: default]

2024-05-10T19:41:46.035+02:00  INFO 15620 --- [           main] org.hibernate.Version                    : HHH000412: Hibernate ORM core version 6.4.4.Final

2024-05-10T19:41:46.140+02:00  INFO 15620 --- [           main] o.h.c.internal.RegionFactoryInitiator    : HHH000026: Second-level cache disabled

2024-05-10T19:41:46.465+02:00  INFO 15620 --- [           main] o.s.o.j.p.SpringPersistenceUnitInfo      : No LoadTimeWeaver setup: ignoring JPA class transformer

2024-05-10T19:41:47.242+02:00  INFO 15620 --- [           main] o.h.e.t.j.p.i.JtaPlatformInitiator       : HHH000489: No JTA platform available (set 'hibernate.transaction.jta.platform' to enable JTA platform integration)

2024-05-10T19:41:47.281+02:00  INFO 15620 --- [           main] j.LocalContainerEntityManagerFactoryBean : Initialized JPA EntityManagerFactory for persistence unit 'default'

2024-05-10T19:41:47.602+02:00  INFO 15620 --- [           main] o.s.d.j.r.query.QueryEnhancerFactory     : Hibernate is in classpath; If applicable, HQL parser will be used.

2024-05-10T19:41:48.668+02:00  WARN 15620 --- [           main] 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

2024-05-10T19:41:49.894+02:00  INFO 15620 --- [           main] o.s.s.web.DefaultSecurityFilterChain     : Will secure any request with [org.springframework.security.web.session.DisableEncodeUrlFilter@19cc697c, org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter@33012e6f, org.springframework.security.web.context.SecurityContextHolderFilter@d88d9d7, org.springframework.security.web.header.HeaderWriterFilter@7280785f, org.springframework.web.filter.CorsFilter@7593716d, org.springframework.security.web.authentication.logout.LogoutFilter@37bafef8, project.picoop.auth.JWTAuthFilter@592fbd8, org.springframework.security.web.savedrequest.RequestCacheAwareFilter@5d38758a, org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter@4acae4c7, org.springframework.security.web.authentication.AnonymousAuthenticationFilter@4086e003, org.springframework.security.web.session.SessionManagementFilter@1b12c870, org.springframework.security.web.access.ExceptionTranslationFilter@7c2d4398, org.springframework.security.web.access.intercept.AuthorizationFilter@75bbeb89]

2024-05-10T19:41:50.443+02:00  INFO 15620 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port 8080 (http) with context path ''

2024-05-10T19:41:50.458+02:00  INFO 15620 --- [           main] project.picoop.PicoopApplication         : Started PicoopApplication in 7.706 seconds (process running for 8.105)

As I understand, this log says the database is created in spring project memory: 2024-05-10T19:41:45.734+02:00 INFO 15620 --- [ main] com.zaxxer.hikari.pool.HikariPool : HikariPool-1 - Added connection conn0: url=jdbc:h2:mem:7f78d327-0c69-4c8a-8962-3481c091e42a user=SA

What I'm doing wrong here?

If it works, SA is the name I gave when I first started the application, so I suppose, application.properties have not been readed since then. The route file is: src/main/resources/application.properties


Solution

  • Have you tried removing the h2 dependency ?

    <dependency>
        <groupId>com.h2database</groupId>
        <artifactId>h2</artifactId>
        <scope>runtime</scope>
    </dependency>