Oralce TIMESTAMP 类型转换为 JSON 失败 | oracle.sql.TIMESTAMP 问题

问题场景

使用 springboot 做的某业务查询,使用的数据库是 Oracle 数据库,数据库中的字段 TRANS_TIME 使用的数据库字段类型是 TIMESTAMP。程序中在 MyBatis 的 XML 中编写了常规的 select 查询,<select id="queryData" resultType="java.util.Map">,这里因为实际业务需求原因没有将查询结果自动转换为 Java 对象,而是直接放到 Map 中返回给前端。

本意是希望前端能获取到查询的数据集合渲染,实际上发现这个 TIMESTAMP 类型的字段没有正常在 json 中显示时间。通过分析发现 map 中的 TRANS_TIME 字段对应的类型是 oracle.sql.TIMESTAMP,而不是 java.sql.Timestamp 类型(如果数据库字段是 DATE 类型则没有问题)。进一步得出结论 oracle.sql.TIMESTAMP 序列化和反序列化有问题,所以不能正常被 spring 的 MessageConvert 转换为 json。

鉴于上述问题和结论,查阅了 oracle 官方文档,获得如下原文说明

bash 复制代码
Footnote 1 ResultSet.getObject returns java.sql.Timestamp only if the oracle.jdbc.J2EE13Compliant connection property is set to TRUE, else the method returns oracle.sql.TIMESTAMP.

Note:

The ResultSet.getObject method returns java.sql.Timestamp for the TIMESTAMP SQL type, only when the connection property oracle.jdbc.J2EE13Compliant is set to TRUE. This property has to be set when the connection is obtained. If this connection property is not set or if it is set after the connection is obtained, then the ResultSet.getObject method returns oracle.sql.TIMESTAMP for the TIMESTAMP SQL type.
The oracle.jdbc.J2EE13Compliant connection property can also be set without changing the code in the following ways:

Including the ojdbc5dms.jar or ojdbc6dms.jar files in the CLASSPATH. These files set oracle.jdbc.J2EE13Compliant to TRUE by default. These are specific to the Oracle Application Server release and are not available as part of the general JDBC release. They are located in $ORACLE_HOME/jdbc/lib.

Setting the system property by calling the java command with the flag -Doracle.jdbc.J2EE13Compliant=true. For example,

java -Doracle.jdbc.J2EE13Compliant=true ...
When the J2EE13Compliant is set to TRUE the action is as in Table B-3 of the JDBC specification.

文中描述大概说的意思就是,oracle 驱动类默认使用 oracle.sql.TIMESTAMP 类型,如果希望使用 J2EE1.3及以后的标准,需要通过设置 JVM 参数 -Doracle.jdbc.J2EE13Compliant=true 来明确让它使用 java.sql.Timestamp 类型。

具体两种方式的示例就是:

1、启动 java 服务的时候通过 -Doracle.jdbc.J2EE13Compliant=true 设定。

2、在 java 程序的入口处硬编码使用 System.setProperty("oracle.jdbc.J2EE13Compliant", "true") 来设定。

这两种方式,做了测试验证,都有效,可以按需选择其一使用。

有兴趣的话,可以进一步查看 oracle 驱动源码,在 oracle.jdbc.driver.GeneratedPhysicalConnection 类的 readConnectionProperties 方法、oracle.jdbc.driver.TimestampAccessorgetObject 方法、oracle.jdbc.driver.DateTimeCommonAccessorgetTimestampgetTIMESTAMP 方法中,能看到关于该配置属性和对应时间类型的处理逻辑。

总结

通过设置属性 oracle.jdbc.J2EE13Compliant=true,可以使 oracle 的 timestamp 类型字段以 java.sql.Timestamp 类型返回,不设置时默认 false,返回 oracle.sql.TIMESTAMP,可能会导致序列化时类型转换失败等问题。


(END)

相关推荐
Elastic 中国社区官方博客几秒前
使用 Mastra 和 Elasticsearch 构建具有语义回忆功能的知识 agent
大数据·数据库·人工智能·elasticsearch·搜索引擎·ai·全文检索
老邓计算机毕设7 分钟前
SSM危险品运输车辆信息管理系统b2z1o(程序+源码+数据库+调试部署+开发环境)带论文文档1万字以上,文末可获取,系统界面在最后面。
数据库·ssm 框架
MuYiLuck23 分钟前
redis持久化与集群
java·数据库·redis
卓码软件测评35 分钟前
软件数据库测试:【数据库质量保障:从单元测试到性能优化】
运维·数据库·测试用例·压力测试
LilySesy43 分钟前
ABAP+在select的时候,可以A=B A=C B=C这样子JOIN吗?
数据库·sql·ai·excel·sap·abap
升鲜宝供应链及收银系统源代码服务1 小时前
升鲜宝生鲜配送供应链管理系统--- 《多语言商品查询优化方案(Redis + 翻译表 + 模糊匹配)》
java·数据库·redis·bootstrap·供应链系统·生鲜配送·生鲜配送源代码
JH30731 小时前
Redis 中被忽视的“键过期策略”与内存回收机制
数据库·redis·缓存
Microsoft Word1 小时前
Redis常见面试题
数据库·redis·缓存
bing.shao1 小时前
mongodb与redis在聊天场景中的选择
数据库·redis·mongodb
dudke1 小时前
c#实现redis的调用与基础类
数据库·redis·缓存