opengauss 中的所有时间字段类型都报错。求救!

opengauss 中的时间字段,对应的类型是timestamp(6),
代码实体类对应的字段是  
date@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
@TableField("ESTIMATED_ENTER_TIME")
private Date estimatedEnterTime;

 报错信息如下:

“data”: "异常: org.springframework.dao.DataIntegrityViolationException - Error attempting to get column ‘estimatedEnterTime’ from result set. Cause: org.postgresql.util.PSQLException: Unsupported binary encoding of timestamp.\n; Unsupported binary encoding of timestamp.; nested exception is org.postgresql.util.PSQLException: Unsupported binary encoding of timestamp.\n at;

你用的是 openGauss 6.0.2,不是原生 PostgreSQL,这个异常:

Unsupported binary encoding of timestamp

在 openGauss 中确实存在类似问题,尤其是 JDBC 驱动在二进制协议下解析 timestamp 字段时容易出错,这是驱动兼容性问题,不是 SQL 写错了。


:white_check_mark: 根因总结(基于 openGauss 6.x 实测与社区反馈)

  1. openGauss JDBC 驱动(尤其是 6.0.2 版本)对 timestamp 类型的二进制格式支持不完整;
  2. 使用 LocalDateTimeOffsetDateTime 等 Java 8 时间类型时,二进制传输模式下解析失败;
  3. 问题在 B兼容性数据库 和 timestamp/timestamptz 类型下更易触发 ;
  4. 与 PostgreSQL 驱动不同,openGauss 驱动不能直接用 binaryTransfer=true 解析 timestamp。

:white_check_mark: 解决方案(按优先级)

:white_check_mark: 1. 禁用二进制传输(最稳妥)
在 JDBC URL 中显式关闭二进制传输:

spring.datasource.url=jdbc:opengauss://host:port/db?binaryTransfer=false

或在连接池配置中:

spring.datasource.hikari.data-source-properties.binaryTransfer=false

:warning: 性能略有下降,但兼容性最好。


:white_check_mark: 2. 使用 String 类型接收(临时规避)
如果你只是读取展示,不改数据,可以把 estimatedEnterTime 字段改为 String 类型接收:

private String estimatedEnterTime;

:warning: 不推荐用于时间计算或排序,仅适合展示。


:white_check_mark: 3. 升级 JDBC 驱动(建议)
openGauss 6.0.2 对应的 JDBC 驱动版本是:

<dependency>
  <groupId>org.opengauss</groupId>
  <artifactId>opengauss-jdbc</artifactId>
  <version>6.0.2</version>
</dependency>

但你可尝试升级到 6.0.3 或 6.1.0 预览版(社区已修复部分 timestamp 解析问题):

<version>6.0.3</version>

:white_check_mark: 4. 数据库字段类型调整(可选)
如果你不需要秒级精度,可以考虑把字段类型从 timestamp 改为 datetimestamp(0),但注意:

openGauss 中 date 类型实际会被映射为 timestamp(0) without time zone ,行为与 PostgreSQL 不同。


:white_check_mark: 一句话总结

openGauss 6.0.2 的 JDBC 驱动在二进制模式下解析 timestamp 不稳定,建议先关闭 binaryTransfer=false,再考虑升级驱动或调整字段类型。


以上是ai答的,不懂对您有没有帮助