【问题标题】:Postgres Error method org.postgresql.jdbc.PgConnection.createClob() is not implementedPostgres 错误方法 org.postgresql.jdbc.PgConnection.createClob() 未实现
【发布时间】:2017-10-09 20:21:55
【问题描述】:

当我使用连接对象调用createClob 方法时,如下所示:

Clob clob = con.createClob();

抛出以下异常:

Caused by: java.sql.SQLFeatureNotSupportedException: Method org.postgresql.jdbc.PgConnection.createClob() is not yet implemented.
        at org.postgresql.Driver.notImplemented(Driver.java:659)
        at org.postgresql.jdbc.PgConnection.createClob(PgConnection.java:1246)
        at org.apache.commons.dbcp2.DelegatingConnection.createClob(DelegatingConnection.java:868)
        at org.apache.commons.dbcp2.DelegatingConnection.createClob(DelegatingConnection.java:868)

我正在使用数据库 PostgreSQL 9.6.2 和 JDK8 并使用 commons-dbcp2 连接池,并在 pom.xml 中添加了以下 Postgres 依赖项

<dependency>
    <groupId>org.postgresql</groupId>
    <artifactId>postgresql</artifactId>
    <version>42.1.1</version>
</dependency>

org.postgresql.jdbc.PgConnection类中,createClob实现如下图所示,抛出异常:

@Override
public Clob createClob() throws SQLException {
    checkClosed();
    throw org.postgresql.Driver.notImplemented(this.getClass(), "createClob()");
}

解决此问题的解决方案或解决方法是什么?或者我们如何在 Postgres 查询中设置 CLOB 数据?

【问题讨论】:

    标签: java postgresql jdbc apache-commons-dbcp


    【解决方案1】:

    TL;DR

    • 在您的application.yml 中设置spring.jpa.properties.hibernate.jdbc.lob.non_contextual_creation=true 或,
    • 在您的persistence.xml 中设置hibernate.jdbc.lob.non_contextual_creation=true

    这是 JBoss 社区中的一个已知错误。

    此错误出现在旧版本和带有 Spring-Boot 2.0.0.RC1 及更高版本的新版本中。

    解决方案

    1. 使用更新的向后兼容版本更新您的 postgressql 驱动程序。
      • 在您的application.yml 中设置spring.jpa.properties.hibernate.jdbc.lob.non_contextual_creation=true 或,
      • 在您的 persistence.xml 中设置 hibernate.jdbc.lob.non_contextual_creation=true
    2. 如果它不起作用,请参阅下面的这个技巧:

    解决方案是在您的属性文件中添加这一行(如果您不使用 spring,则添加类似的内容)

    spring.jpa.properties.hibernate.temp.use_jdbc_metadata_defaults= false
    

    所以,您的 application.yml 应该如下所示:

    spring:
        application:
          name: employee-service
    
        datasource:
          url: jdbc:postgresql://localhost:5432/db_development
          platform: POSTGRESQL
          username: ...
          password: ...
    
        jpa:
          hibernate:
            ddl-auto: create-drop
            dialect: org.hibernate.dialect.PostgreSQL9Dialect
            show_sql: true
          properties.hibernate.temp.use_jdbc_metadata_defaults: false
    
    
    server:
      port: 8080
    

    参考:

    https://o7planning.org/en/11661/spring-boot-jpa-and-spring-transaction-tutorial

    hibernate with c3p0: createClob() is not yet implemented

    感谢 Binakot 的评论。我已经更新了帖子。

    【讨论】:

    • 如果您使用 Java 配置,请添加 org.hibernate.cfg.AvailableSettings#NON_CONTEXTUAL_LOB_CREATION 作为 JpaPropertyMap 的键和 "true" 作为值。
    • 您也可以在application.yml 中设置non_contextual_creation(不仅在persistence.xml 中):spring.jpa.properties.hibernate.jdbc.lob.non_contextual_creation=true
    【解决方案2】:

    将其放入 application.properties

    spring.jpa.properties.hibernate.jdbc.lob.non_contextual_creation=true
    

    【讨论】:

      【解决方案3】:

      PostgreSQL 并没有真正的“CLOB”。只需将setString(String)setObject(...)Types.STRING 一起使用。

      【讨论】:

        【解决方案4】:

        使用 spring-boot 2.1.9.RELEASE 我添加了以下内容并且它起作用了

        spring:
          jpa:
            properties.hibernate.temp.use_jdbc_metadata_defaults: false
            database-platform: org.hibernate.dialect.PostgreSQL94Dialect
        

        【讨论】:

          猜你喜欢
          • 2018-08-13
          • 2019-09-08
          • 2019-12-13
          • 2019-09-25
          • 1970-01-01
          • 2021-09-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多