升级到 nHibernate v5.x 后导致 Oracle DB 出现日期时间错误
After upgrade to nHibernate v5.x causing DateTime errors on Oracle DB
我们最近从 nHibernate v4.x 迁移到 v5.3.9,我们开始遇到日期时间字段的问题。经过长时间的分析,我发现问题是由 DateTime.MaxValue 引起的,我们将其用于无日期值。在迁移之前,日期被连接起来并存储到数据库中,没有毫秒。升级后,该值存储在 Oracle 数据库中,但读取日期时间戳字段对于 Oracle 失败,但对于 Microsoft SQL 服务器则不然。错误堆栈跟踪:
2021-10-22 14:23:05,141 [9] ERROR EafManagement.Core.NHibernateExtensions - Error during transaction commit
NHibernate.Exceptions.GenericADOException: could not execute query
[ select id1_12_,isdeleted2_12_,islocked3_12_,uuid4_12_,creationdate5_12_,isretired6_12_,retiredate7_12_,........... where rownum <=1 ]
Name:p1 - Value:Test Name:p2 - Value:1 Name:p3 - Value:0 Name:p4 - Value:0
[SQL: select id1_12_,isdeleted2_12_,islocked3_12_,uuid4_12_,creationdate5_12_,isretired6_12_,retiredate7_12_,........ where rownum <=1] ---> System.ArgumentOutOfRangeException: Year, Month, and Day parameters describe an un-representable DateTime.
at System.DateTime.DateToTicks(Int32 year, Int32 month, Int32 day)
at System.DateTime..ctor(Int32 year, Int32 month, Int32 day, Int32 hour, Int32 minute, Int32 second, Int32 millisecond)
at Oracle.ManagedDataAccess.Types.DateTimeConv.ToDateTime(Byte[] byteRep, Boolean isNotTimeStampTZ, Int32 offset, Int32 length)
at Oracle.ManagedDataAccess.Client.OracleDataReader.GetDateTime(Int32 i)
at Oracle.ManagedDataAccess.Client.OracleDataReader.GetValue(Int32 i)
at NHibernate.Type.AbstractDateTimeType.GetDateTime(DbDataReader rs, Int32 index, ISessionImplementor session) in D:\BuildAgent\work546188361a242\src\NHibernate\Type\AbstractDateTimeType.cs:line 80
at NHibernate.Type.AbstractDateTimeType.Get(DbDataReader rs, Int32 index, ISessionImplementor session) in D:\BuildAgent\work546188361a242\src\NHibernate\Type\AbstractDateTimeType.cs:line 59
at NHibernate.Type.NullableType.NullSafeGet(DbDataReader rs, String name, ISessionImplementor session) in D:\BuildAgent\work546188361a242\src\NHibernate\Type\NullableType.cs:line 235
at NHibernate.Persister.Entity.AbstractEntityPersister.Hydrate(DbDataReader rs, Object id, Object obj, String[][] suffixedPropertyColumns, ISet`1 fetchedLazyProperties, Boolean allProperties, Int32[] indexes, ISessionImplementor session) in D:\BuildAgent\work546188361a242\src\NHibernate\Persister\Entity\AbstractEntityPersister.cs:line 2848
at NHibernate.Persister.Entity.LoadableExtensions.Hydrate(ILoadable loadable, DbDataReader rs, Object id, Object obj, String[][] suffixedPropertyColumns, ISet`1 fetchedLazyProperties, Boolean allProperties, ISessionImplementor session) in D:\BuildAgent\work546188361a242\src\NHibernate\Persister\Entity\ILoadable.cs:line 101
at NHibernate.Loader.Loader.LoadFromResultSet(DbDataReader rs, Int32 i, Object obj, ILoadable persister, EntityKey key, LockMode lockMode, ILoadable rootPersister, ISessionImplementor session) in D:\BuildAgent\work546188361a242\src\NHibernate\Loader\Loader.cs:line 1301
at NHibernate.Loader.Loader.InstanceNotYetLoaded(DbDataReader dr, Int32 i, ILoadable persister, EntityKey key, LockMode lockMode, EntityKey optionalObjectKey, Object optionalObject, IList hydratedObjects, ISessionImplementor session) in D:\BuildAgent\work546188361a242\src\NHibernate\Loader\Loader.cs:line 1164
at NHibernate.Loader.Loader.GetRow(DbDataReader rs, ILoadable[] persisters, EntityKey[] keys, Object optionalObject, EntityKey optionalObjectKey, LockMode[] lockModes, IList hydratedObjects, ISessionImplementor session, Boolean mustLoadMissingEntity, Action`2 cacheBatchingHandler) in D:\BuildAgent\work546188361a242\src\NHibernate\Loader\Loader.cs:line 1041
at NHibernate.Loader.Loader.GetRowFromResultSet(DbDataReader resultSet, ISessionImplementor session, QueryParameters queryParameters, LockMode[] lockModeArray, EntityKey optionalObjectKey, IList hydratedObjects, EntityKey[] keys, Boolean returnProxies, IResultTransformer forcedResultTransformer, QueryCacheResultBuilder queryCacheResultBuilder, Action`2 cacheBatchingHandler) in D:\BuildAgent\work546188361a242\src\NHibernate\Loader\Loader.cs:line 405
at NHibernate.Loader.Loader.DoQuery(ISessionImplementor session, QueryParameters queryParameters, Boolean returnProxies, IResultTransformer forcedResultTransformer, QueryCacheResultBuilder queryCacheResultBuilder) in D:\BuildAgent\work546188361a242\src\NHibernate\Loader\Loader.cs:line 558
at NHibernate.Loader.Loader.DoQueryAndInitializeNonLazyCollections(ISessionImplementor session, QueryParameters queryParameters, Boolean returnProxies, IResultTransformer forcedResultTransformer, QueryCacheResultBuilder queryCacheResultBuilder) in D:\BuildAgent\work546188361a242\src\NHibernate\Loader\Loader.cs:line 303
at NHibernate.Loader.Loader.DoList(ISessionImplementor session, QueryParameters queryParameters, IResultTransformer forcedResultTransformer, QueryCacheResultBuilder queryCacheResultBuilder) in D:\BuildAgent\work546188361a242\src\NHibernate\Loader\Loader.cs:line 1972
--- End of inner exception stack trace ---
at NHibernate.Loader.Loader.DoList(ISessionImplementor session, QueryParameters queryParameters, IResultTransformer forcedResultTransformer, QueryCacheResultBuilder queryCacheResultBuilder) in D:\BuildAgent\work546188361a242\src\NHibernate\Loader\Loader.cs:line 1981
at NHibernate.Loader.Loader.ListIgnoreQueryCache(ISessionImplementor session, QueryParameters queryParameters) in D:\BuildAgent\work546188361a242\src\NHibernate\Loader\Loader.cs:line 1837
at NHibernate.Loader.Hql.QueryLoader.List(ISessionImplementor session, QueryParameters queryParameters) in D:\BuildAgent\work546188361a242\src\NHibernate\Loader\Hql\QueryLoader.cs:line 325
at NHibernate.Hql.Ast.ANTLR.QueryTranslatorImpl.List(ISessionImplementor session, QueryParameters queryParameters) in D:\BuildAgent\work546188361a242\src\NHibernate\Hql\Ast\ANTLR\QueryTranslatorImpl.cs:line 119
at NHibernate.Engine.Query.HQLQueryPlan.PerformList(QueryParameters queryParameters, ISessionImplementor session, IList results) in D:\BuildAgent\work546188361a242\src\NHibernate\Engine\Query\HQLQueryPlan.cs:line 116
at NHibernate.Impl.SessionImpl.List(IQueryExpression queryExpression, QueryParameters queryParameters, IList results, Object filterConnection) in D:\BuildAgent\work546188361a242\src\NHibernate\Impl\SessionImpl.cs:line 559
at NHibernate.Impl.SessionImpl.List(IQueryExpression queryExpression, QueryParameters queryParameters, IList results) in D:\BuildAgent\work546188361a242\src\NHibernate\Impl\SessionImpl.cs:line 524
at NHibernate.Impl.AbstractSessionImpl.List(IQueryExpression queryExpression, QueryParameters parameters) in D:\BuildAgent\work546188361a242\src\NHibernate\Impl\AbstractSessionImpl.cs:line 172
at NHibernate.Impl.AbstractQueryImpl2.List() in D:\BuildAgent\work546188361a242\src\NHibernate\Impl\AbstractQueryImpl2.cs:line 78
at NHibernate.Linq.DefaultQueryProvider.ExecuteQuery(NhLinqExpression nhLinqExpression, IQuery query, NhLinqExpression nhQuery) in D:\BuildAgent\work546188361a242\src\NHibernate\Linq\DefaultQueryProvider.cs:line 226
at NHibernate.Linq.DefaultQueryProvider.Execute(Expression expression) in D:\BuildAgent\work546188361a242\src\NHibernate\Linq\DefaultQueryProvider.cs:line 96
at NHibernate.Linq.DefaultQueryProvider.Execute[TResult](Expression expression) in D:\BuildAgent\work546188361a242\src\NHibernate\Linq\DefaultQueryProvider.cs:line 101
at .....
这个问题显然是由于 nHibernate 中提到的日期时间戳的准确性发生变化引起的 breaking changes 第 1421 行:
- Oracle 9g+ 方言现在对所有日期时间类型使用 timestamp(7),而不是 timestamp(4)。
尝试从存储值中读取 Oracle 数据库中的世纪值不仅对于应用程序而且对于 Oracle SQL 开发人员工具都是不可能的。可以返回没有世纪的格式,它显示值为 01.01.00 00:00:00,00000000。所以听起来 Oracle 正在将值四舍五入到更高的值,然后它在稍后返回时失败了。这会影响生产代码,转换为空日期时间需要时间,但现在需要解决方案,因为降级不是很好的选择。
问题:如何将 nHibernate 配置回 v5.x 之前的较低精度?
在我们完全转换为空日期时间戳字段值之前,还有其他可能的安全快速修复吗?
真实 DateTime.MaxValue = 12/31/9999 23:59:59.999999999
Microsoft SQL DB 服务器值存储到数据库:
升级前:12/31/9999 23:59:59.000
升级后:12/31/9999 23:59:59.997
Oracle DB 服务器值存储到数据库:
升级前:31.12.99 23:59:59,000000(格式化日期后显示为 9999 年)
升级后显示为:01.01.0000:00:00,0000000
由于我没有收到任何建议,我找到了一个选项来修改映射,方法是在 nHibernate 映射文件中添加引入的新 type DateTimeNoMs mapping:
<property name="RetireDate" column="RETIREDATE" not-null="false" type="DateTimeNoMs"/>
当使用 DateTime.MaxValue 时,这消除了导致 Oracle DB 出现问题的时间戳的毫秒精度。
NHibernate 类型 = DateTimeNoMs
.NET 类型 = System.DateTime
数据库类型 = DbType.DateTime / DbType.DateTime2
备注 = type="DateTimeNoMs"
必须指定。忽略小数秒。自 NHibernate v5.0 起可用。
我们最近从 nHibernate v4.x 迁移到 v5.3.9,我们开始遇到日期时间字段的问题。经过长时间的分析,我发现问题是由 DateTime.MaxValue 引起的,我们将其用于无日期值。在迁移之前,日期被连接起来并存储到数据库中,没有毫秒。升级后,该值存储在 Oracle 数据库中,但读取日期时间戳字段对于 Oracle 失败,但对于 Microsoft SQL 服务器则不然。错误堆栈跟踪:
2021-10-22 14:23:05,141 [9] ERROR EafManagement.Core.NHibernateExtensions - Error during transaction commit
NHibernate.Exceptions.GenericADOException: could not execute query
[ select id1_12_,isdeleted2_12_,islocked3_12_,uuid4_12_,creationdate5_12_,isretired6_12_,retiredate7_12_,........... where rownum <=1 ]
Name:p1 - Value:Test Name:p2 - Value:1 Name:p3 - Value:0 Name:p4 - Value:0
[SQL: select id1_12_,isdeleted2_12_,islocked3_12_,uuid4_12_,creationdate5_12_,isretired6_12_,retiredate7_12_,........ where rownum <=1] ---> System.ArgumentOutOfRangeException: Year, Month, and Day parameters describe an un-representable DateTime.
at System.DateTime.DateToTicks(Int32 year, Int32 month, Int32 day)
at System.DateTime..ctor(Int32 year, Int32 month, Int32 day, Int32 hour, Int32 minute, Int32 second, Int32 millisecond)
at Oracle.ManagedDataAccess.Types.DateTimeConv.ToDateTime(Byte[] byteRep, Boolean isNotTimeStampTZ, Int32 offset, Int32 length)
at Oracle.ManagedDataAccess.Client.OracleDataReader.GetDateTime(Int32 i)
at Oracle.ManagedDataAccess.Client.OracleDataReader.GetValue(Int32 i)
at NHibernate.Type.AbstractDateTimeType.GetDateTime(DbDataReader rs, Int32 index, ISessionImplementor session) in D:\BuildAgent\work546188361a242\src\NHibernate\Type\AbstractDateTimeType.cs:line 80
at NHibernate.Type.AbstractDateTimeType.Get(DbDataReader rs, Int32 index, ISessionImplementor session) in D:\BuildAgent\work546188361a242\src\NHibernate\Type\AbstractDateTimeType.cs:line 59
at NHibernate.Type.NullableType.NullSafeGet(DbDataReader rs, String name, ISessionImplementor session) in D:\BuildAgent\work546188361a242\src\NHibernate\Type\NullableType.cs:line 235
at NHibernate.Persister.Entity.AbstractEntityPersister.Hydrate(DbDataReader rs, Object id, Object obj, String[][] suffixedPropertyColumns, ISet`1 fetchedLazyProperties, Boolean allProperties, Int32[] indexes, ISessionImplementor session) in D:\BuildAgent\work546188361a242\src\NHibernate\Persister\Entity\AbstractEntityPersister.cs:line 2848
at NHibernate.Persister.Entity.LoadableExtensions.Hydrate(ILoadable loadable, DbDataReader rs, Object id, Object obj, String[][] suffixedPropertyColumns, ISet`1 fetchedLazyProperties, Boolean allProperties, ISessionImplementor session) in D:\BuildAgent\work546188361a242\src\NHibernate\Persister\Entity\ILoadable.cs:line 101
at NHibernate.Loader.Loader.LoadFromResultSet(DbDataReader rs, Int32 i, Object obj, ILoadable persister, EntityKey key, LockMode lockMode, ILoadable rootPersister, ISessionImplementor session) in D:\BuildAgent\work546188361a242\src\NHibernate\Loader\Loader.cs:line 1301
at NHibernate.Loader.Loader.InstanceNotYetLoaded(DbDataReader dr, Int32 i, ILoadable persister, EntityKey key, LockMode lockMode, EntityKey optionalObjectKey, Object optionalObject, IList hydratedObjects, ISessionImplementor session) in D:\BuildAgent\work546188361a242\src\NHibernate\Loader\Loader.cs:line 1164
at NHibernate.Loader.Loader.GetRow(DbDataReader rs, ILoadable[] persisters, EntityKey[] keys, Object optionalObject, EntityKey optionalObjectKey, LockMode[] lockModes, IList hydratedObjects, ISessionImplementor session, Boolean mustLoadMissingEntity, Action`2 cacheBatchingHandler) in D:\BuildAgent\work546188361a242\src\NHibernate\Loader\Loader.cs:line 1041
at NHibernate.Loader.Loader.GetRowFromResultSet(DbDataReader resultSet, ISessionImplementor session, QueryParameters queryParameters, LockMode[] lockModeArray, EntityKey optionalObjectKey, IList hydratedObjects, EntityKey[] keys, Boolean returnProxies, IResultTransformer forcedResultTransformer, QueryCacheResultBuilder queryCacheResultBuilder, Action`2 cacheBatchingHandler) in D:\BuildAgent\work546188361a242\src\NHibernate\Loader\Loader.cs:line 405
at NHibernate.Loader.Loader.DoQuery(ISessionImplementor session, QueryParameters queryParameters, Boolean returnProxies, IResultTransformer forcedResultTransformer, QueryCacheResultBuilder queryCacheResultBuilder) in D:\BuildAgent\work546188361a242\src\NHibernate\Loader\Loader.cs:line 558
at NHibernate.Loader.Loader.DoQueryAndInitializeNonLazyCollections(ISessionImplementor session, QueryParameters queryParameters, Boolean returnProxies, IResultTransformer forcedResultTransformer, QueryCacheResultBuilder queryCacheResultBuilder) in D:\BuildAgent\work546188361a242\src\NHibernate\Loader\Loader.cs:line 303
at NHibernate.Loader.Loader.DoList(ISessionImplementor session, QueryParameters queryParameters, IResultTransformer forcedResultTransformer, QueryCacheResultBuilder queryCacheResultBuilder) in D:\BuildAgent\work546188361a242\src\NHibernate\Loader\Loader.cs:line 1972
--- End of inner exception stack trace ---
at NHibernate.Loader.Loader.DoList(ISessionImplementor session, QueryParameters queryParameters, IResultTransformer forcedResultTransformer, QueryCacheResultBuilder queryCacheResultBuilder) in D:\BuildAgent\work546188361a242\src\NHibernate\Loader\Loader.cs:line 1981
at NHibernate.Loader.Loader.ListIgnoreQueryCache(ISessionImplementor session, QueryParameters queryParameters) in D:\BuildAgent\work546188361a242\src\NHibernate\Loader\Loader.cs:line 1837
at NHibernate.Loader.Hql.QueryLoader.List(ISessionImplementor session, QueryParameters queryParameters) in D:\BuildAgent\work546188361a242\src\NHibernate\Loader\Hql\QueryLoader.cs:line 325
at NHibernate.Hql.Ast.ANTLR.QueryTranslatorImpl.List(ISessionImplementor session, QueryParameters queryParameters) in D:\BuildAgent\work546188361a242\src\NHibernate\Hql\Ast\ANTLR\QueryTranslatorImpl.cs:line 119
at NHibernate.Engine.Query.HQLQueryPlan.PerformList(QueryParameters queryParameters, ISessionImplementor session, IList results) in D:\BuildAgent\work546188361a242\src\NHibernate\Engine\Query\HQLQueryPlan.cs:line 116
at NHibernate.Impl.SessionImpl.List(IQueryExpression queryExpression, QueryParameters queryParameters, IList results, Object filterConnection) in D:\BuildAgent\work546188361a242\src\NHibernate\Impl\SessionImpl.cs:line 559
at NHibernate.Impl.SessionImpl.List(IQueryExpression queryExpression, QueryParameters queryParameters, IList results) in D:\BuildAgent\work546188361a242\src\NHibernate\Impl\SessionImpl.cs:line 524
at NHibernate.Impl.AbstractSessionImpl.List(IQueryExpression queryExpression, QueryParameters parameters) in D:\BuildAgent\work546188361a242\src\NHibernate\Impl\AbstractSessionImpl.cs:line 172
at NHibernate.Impl.AbstractQueryImpl2.List() in D:\BuildAgent\work546188361a242\src\NHibernate\Impl\AbstractQueryImpl2.cs:line 78
at NHibernate.Linq.DefaultQueryProvider.ExecuteQuery(NhLinqExpression nhLinqExpression, IQuery query, NhLinqExpression nhQuery) in D:\BuildAgent\work546188361a242\src\NHibernate\Linq\DefaultQueryProvider.cs:line 226
at NHibernate.Linq.DefaultQueryProvider.Execute(Expression expression) in D:\BuildAgent\work546188361a242\src\NHibernate\Linq\DefaultQueryProvider.cs:line 96
at NHibernate.Linq.DefaultQueryProvider.Execute[TResult](Expression expression) in D:\BuildAgent\work546188361a242\src\NHibernate\Linq\DefaultQueryProvider.cs:line 101
at .....
这个问题显然是由于 nHibernate 中提到的日期时间戳的准确性发生变化引起的 breaking changes 第 1421 行:
- Oracle 9g+ 方言现在对所有日期时间类型使用 timestamp(7),而不是 timestamp(4)。
尝试从存储值中读取 Oracle 数据库中的世纪值不仅对于应用程序而且对于 Oracle SQL 开发人员工具都是不可能的。可以返回没有世纪的格式,它显示值为 01.01.00 00:00:00,00000000。所以听起来 Oracle 正在将值四舍五入到更高的值,然后它在稍后返回时失败了。这会影响生产代码,转换为空日期时间需要时间,但现在需要解决方案,因为降级不是很好的选择。
问题:如何将 nHibernate 配置回 v5.x 之前的较低精度? 在我们完全转换为空日期时间戳字段值之前,还有其他可能的安全快速修复吗?
真实 DateTime.MaxValue = 12/31/9999 23:59:59.999999999
Microsoft SQL DB 服务器值存储到数据库: 升级前:12/31/9999 23:59:59.000 升级后:12/31/9999 23:59:59.997
Oracle DB 服务器值存储到数据库: 升级前:31.12.99 23:59:59,000000(格式化日期后显示为 9999 年) 升级后显示为:01.01.0000:00:00,0000000
由于我没有收到任何建议,我找到了一个选项来修改映射,方法是在 nHibernate 映射文件中添加引入的新 type DateTimeNoMs mapping:
<property name="RetireDate" column="RETIREDATE" not-null="false" type="DateTimeNoMs"/>
当使用 DateTime.MaxValue 时,这消除了导致 Oracle DB 出现问题的时间戳的毫秒精度。
NHibernate 类型 = DateTimeNoMs
.NET 类型 = System.DateTime
数据库类型 = DbType.DateTime / DbType.DateTime2
备注 = type="DateTimeNoMs"
必须指定。忽略小数秒。自 NHibernate v5.0 起可用。