😺
Spring BootアプリケーションがPostgreSQLに接続できない場合の解決法
先日、PostgreSQLを17.4に更新した後、動作確認していたSpring Bootアプリケーション。
後日、IntelliJ IDEAを更新したため、再度Spring Bootアプリケーションを動作確認しようとしたところ、PostgreSQLに接続できない事象発生。
実行時のログでは、JDBC Driverが読み込めないとの指摘。
...
2025-03-08T13:13:10.879+09:00 WARN 8688 --- [ restartedMain] o.m.s.mapper.ClassPathMapperScanner : No MyBatis mapper was found in '[com.example.demo]' package. Please check your configuration.
2025-03-08T13:13:11.090+09:00 WARN 8688 --- [ restartedMain] s.c.a.AnnotationConfigApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]: Failed to initialize dependency 'dataSourceScriptDatabaseInitializer' of LoadTimeWeaverAware bean 'entityManagerFactory': Error creating bean with name 'dataSourceScriptDatabaseInitializer' defined in class path resource [org/springframework/boot/autoconfigure/sql/init/DataSourceInitializationConfiguration.class]: Unsatisfied dependency expressed through method 'dataSourceScriptDatabaseInitializer' parameter 0: Error creating bean with name 'dataSource' defined in class path resource [org/springframework/boot/autoconfigure/jdbc/DataSourceConfiguration$Hikari.class]: Failed to instantiate [com.zaxxer.hikari.HikariDataSource]: Factory method 'dataSource' threw exception with message: Cannot load driver class: org.postgresql.Driver
...
おおよそこの状況の場合、Webで検索すると、データソースの設定を確認するよう指摘する。
まぁ、こんな感じで問題ない。
...
datasource:
driver-class-name: org.postgresql.Driver
url: jdbc:postgresql://localhost:5432/batch
...
JDBCドライバの話なので、何らかの問題で接続できないことになっているかもと、ビルド設定を確認
以下のような感じで依存パッケージで指定されている。
dependencies {
implementation("org.springframework.boot:spring-boot-starter-batch")
implementation("org.springframework.boot:spring-boot-starter-data-jdbc")
implementation("org.springframework.boot:spring-boot-starter-data-jpa")
implementation("org.springframework.boot:spring-boot-starter-jdbc")
implementation("org.jetbrains.kotlin:kotlin-reflect")
implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
implementation("org.projectlombok:lombok:1.18.22")
developmentOnly("org.springframework.boot:spring-boot-devtools")
runtimeOnly("org.postgresql:postgresql")
testImplementation("org.springframework.boot:spring-boot-starter-test")
testImplementation("org.springframework.batch:spring-batch-test")
}
パッケージの格納先(org.postgresql)に接続して最新バージョンを確認して、明に指定するよう変更
dependencies {
...
runtimeOnly("org.postgresql:postgresql:42.7.5")
...
}
そうすると、接続でき、アプリケーションは正常実行できました。
2025-03-08T13:06:04.693+09:00 WARN 16192 --- [ restartedMain] o.m.s.mapper.ClassPathMapperScanner : No MyBatis mapper was found in '[com.example.demo]' package. Please check your configuration.
2025-03-08T13:06:04.998+09:00 INFO 16192 --- [ restartedMain] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Starting...
2025-03-08T13:06:05.308+09:00 INFO 16192 --- [ restartedMain] com.zaxxer.hikari.pool.HikariPool : HikariPool-1 - Added connection org.postgresql.jdbc.PgConnection@4b130b1f
2025-03-08T13:06:05.309+09:00 INFO 16192 --- [ restartedMain] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Start completed.
2025-03-08T13:06:05.499+09:00 INFO 16192 --- [ restartedMain] o.hibernate.jpa.internal.util.LogHelper : HHH000204: Processing PersistenceUnitInfo [name: default]
2025-03-08T13:06:05.633+09:00 INFO 16192 --- [ restartedMain] org.hibernate.Version : HHH000412: Hibernate ORM core version 6.6.2.Final
2025-03-08T13:06:05.697+09:00 INFO 16192 --- [ restartedMain] o.h.c.internal.RegionFactoryInitiator : HHH000026: Second-level cache disabled
2025-03-08T13:06:06.132+09:00 INFO 16192 --- [ restartedMain] o.s.o.j.p.SpringPersistenceUnitInfo : No LoadTimeWeaver setup: ignoring JPA class transformer
2025-03-08T13:06:06.210+09:00 INFO 16192 --- [ restartedMain] org.hibernate.orm.connections.pooling : HHH10001005: Database info:
Database JDBC URL [Connecting through datasource 'HikariDataSource (HikariPool-1)']
Database driver: undefined/unknown
Database version: 17.4
Autocommit mode: undefined/unknown
Isolation level: undefined/unknown
Minimum pool size: undefined/unknown
Maximum pool size: undefined/unknown
PostgreSQLに接続できない場合、設定を確認するだけでなく、依存パッケージを見直すことも必要ですね。
Discussion