You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Spring应用连接PostgreSQL失败:entityManagerFactory创建异常求助

解决PostgreSQL连接失败:ClassNotFoundException找不到ScramClient$ChannelBinding类

这个异常是因为PostgreSQL JDBC驱动缺少SCRAM认证相关类,核心原因是驱动版本不兼容或依赖冲突,以下是具体修复方案:

1. 统一PostgreSQL驱动版本

推荐借助Spring Boot的依赖管理自动适配驱动版本,避免手动指定版本导致的兼容问题:

  • Maven pom.xml 配置:
<dependency>
    <groupId>org.postgresql</groupId>
    <artifactId>postgresql</artifactId>
</dependency>
  • Gradle 配置:
implementation 'org.postgresql:postgresql'

如果必须手动指定版本,选择42.2.20及以上版本(该版本后SCRAM支持更完善)。

2. 排查并解决依赖冲突

检查项目中是否存在多个版本的PostgreSQL驱动:

  • Maven 查看依赖树:
mvn dependency:tree | grep postgresql
  • Gradle 查看依赖树:
./gradlew dependencies | grep postgresql

若发现重复依赖,在冲突的依赖中排除旧版本,比如Maven中:

<dependency>
    <!-- 存在冲突的依赖坐标 -->
    <exclusions>
        <exclusion>
            <groupId>org.postgresql</groupId>
            <artifactId>postgresql</artifactId>
        </exclusion>
    </exclusions>
</dependency>

3. 优化数据库连接URL配置(可选)

如果你的PostgreSQL启用了SCRAM认证,确保URL配置正确(驱动版本正确时通常无需手动指定):

spring.datasource.url=jdbc:postgresql://localhost:5432/your_db?sslmode=prefer

若仍有问题,可尝试添加认证插件参数:

spring.datasource.url=jdbc:postgresql://localhost:5432/your_db?sslmode=prefer&authenticationPluginClassName=org.postgresql.plugin.ScramAuthenticator

4. 清理缓存并重新构建

  • Maven 清理并重新构建:
mvn clean install -U
  • Gradle 清理并刷新依赖:
./gradlew clean build --refresh-dependencies

重启IDE,确保依赖被正确加载。

验证时先确认依赖树中只有一个PostgreSQL驱动版本,再启动应用检查entityManagerFactory Bean是否正常初始化。

内容的提问来源于stack exchange,提问作者Nikita

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.15 13:01:17