Paras
Paras

Reputation: 3481

Springboot connecting to unknown database

I created a Springboot application using https://start.spring.io/. After adding some initial application properties for spring-data-jpa, the application starts fine however, I do not see any tables created in my local database.

To test this further, I provided incorrect credentials in application.properties and started the application expecting it to fail, however it started fine.

I see in logs it is connecting to a Mysql database but not sure how and where.

Has anyone faced this strange issue ?

application.properties

spring.datasource.url = jdbc:mysql://localhost:3306/db
spring.datasource.username = db
spring.datasource.password = pass

spring.datasource.driver-class-name=com.mysql.jdbc.Driver

# Keep the connection alive if idle for a long time (needed in production)
spring.datasource.testWhileIdle = true
spring.datasource.validationQuery = SELECT 1

# Show or not log for each sql query
spring.jpa.show-sql = true

# Hibernate ddl auto (create, create-drop, update)
spring.jpa.hibernate.ddl-auto = update

# Naming strategy
spring.jpa.hibernate.naming-strategy = org.hibernate.cfg.ImprovedNamingStrategy

# Use spring.jpa.properties.* for Hibernate native properties (the prefix is
# stripped before adding them to the entity manager)

# The SQL dialect makes Hibernate generate better SQL for the chosen database
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect

Pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.7.4</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>demo</name>
    <properties>
        <java.version>17</java.version>
    </properties>
    <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-security</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-validation</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

I even provided an incorrect port in database url, it still started fine with following logs :

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

2022-10-01 22:01:21.025  INFO 20928 --- [           main] com.example.demo.DemoApplication         : Starting DemoApplication using Java 16.0.2 on DESKTOP-S112VSS with PID 20928 (C:\Users\Paras\Downloads\demo\demo\target\classes started by Paras in C:\Users\Paras\Downloads\demo\demo)
2022-10-01 22:01:21.036  INFO 20928 --- [           main] com.example.demo.DemoApplication         : No active profile set, falling back to 1 default profile: "default"
2022-10-01 22:01:22.278  INFO 20928 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data JPA repositories in DEFAULT mode.
2022-10-01 22:01:22.302  INFO 20928 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 9 ms. Found 0 JPA repository interfaces.
2022-10-01 22:01:23.557  INFO 20928 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2022-10-01 22:01:23.583  INFO 20928 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2022-10-01 22:01:23.584  INFO 20928 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.65]
2022-10-01 22:01:23.957  INFO 20928 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2022-10-01 22:01:23.957  INFO 20928 --- [           main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 2789 ms
2022-10-01 22:01:24.267  INFO 20928 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Starting...
2022-10-01 22:01:24.524  INFO 20928 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Start completed.
2022-10-01 22:01:24.600  INFO 20928 --- [           main] o.hibernate.jpa.internal.util.LogHelper  : HHH000204: Processing PersistenceUnitInfo [name: default]
2022-10-01 22:01:24.697  INFO 20928 --- [           main] org.hibernate.Version                    : HHH000412: Hibernate ORM core version 5.6.11.Final
2022-10-01 22:01:24.971  INFO 20928 --- [           main] o.hibernate.annotations.common.Version   : HCANN000001: Hibernate Commons Annotations {5.1.2.Final}
2022-10-01 22:01:25.150  INFO 20928 --- [           main] org.hibernate.dialect.Dialect            : HHH000400: Using dialect: org.hibernate.dialect.MySQL8Dialect
2022-10-01 22:01:25.472  INFO 20928 --- [           main] o.h.e.t.j.p.i.JtaPlatformInitiator       : HHH000490: Using JtaPlatform implementation: [org.hibernate.engine.transaction.jta.platform.internal.NoJtaPlatform]
2022-10-01 22:01:25.489  INFO 20928 --- [           main] j.LocalContainerEntityManagerFactoryBean : Initialized JPA EntityManagerFactory for persistence unit 'default'
2022-10-01 22:01:25.550  WARN 20928 --- [           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
2022-10-01 22:01:26.069  INFO 20928 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
2022-10-01 22:01:26.086  INFO 20928 --- [           main] com.example.demo.DemoApplication         : Started DemoApplication in 5.957 seconds (JVM running for 6.799)

Upvotes: 1

Views: 3305

Answers (5)

Paras
Paras

Reputation: 3481

The issue has been resolved, somehow there were environment variables being set from a previous project, so Spring was taking that as a priority instead of the values defined in the properties file.

Upvotes: 1

v.ladynev
v.ladynev

Reputation: 19956

Add this to the configuration

logging.level.com.zaxxer.hikari.HikariConfig=DEBUG

You will see the database URL in the logs.

The problem can be, IDE doesn't sync your changes to the build folder. Check application.properties in the build folder.

Also It is not necessary that application will connect to the database during startup. You can try to save() a simple entity.

https://stackoverflow.com/a/74019458/3405171

You can add spring-boot-starter-actuator library to check the connection during startup for sure.

Upvotes: 0

Shyam Patel
Shyam Patel

Reputation: 419

Please check the following line, driver class

Set it to like.

spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver

also, check your system's MySQL version if it's 5.7 then make your hibernate dialect version 57, if it's 5.8 then make your hibernate dialect to version 8.

Example, My SQL version is

mysql  Ver 8.0.30 for macos12.4 on x86_64 (Homebrew)

so, I have set my hibernate dialect

spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL8Dialect

I hope, it helps!

Upvotes: 0

windofrain
windofrain

Reputation: 1

I think you should see this log

[0;39m [2m:[0;39m Registered driver with driverClassName=com.mysql.jdbc.Driver

was not found, trying direct instantiation.

it,s the driver settings incorrect?

setting this configure properties correct spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver

Upvotes: 0

mclc
mclc

Reputation: 17

Look again at your "log" and your configuration file. In the first one it indicates a dialect of MySQL version 8, however in the configuration file, it shows version 5. That's for starters.

Upvotes: 0

Related Questions