Mybatis.NET Oracle 线上神奇问题:Value does not fall within the expected range.

时间:2023-03-09 19:13:09
Mybatis.NET Oracle 线上神奇问题:Value does not fall within the expected range.

1、错误现象

  在向数据库查询一条数据的时候报如下错误:

    Value does not fall within the expected range.
at Oracle.ManagedDataAccess.Client.OracleParameter.set_Value(Object value)
at MyBatis.DataMapper.Data.DefaultPreparedCommand.ApplyParameterMap(IDbProvider dbProvider, IDbCommand command, RequestScope request, IStatement statement, Object parameterObject)
at MyBatis.DataMapper.Data.DefaultPreparedCommand.Create(RequestScope request, ISession session, IStatement statement, Object parameterObject)
at MyBatis.DataMapper.MappedStatements.MappedStatement.Execute[T](Object preEvent, Object postEvent, ISession session, Object parameterObject, Func` requestRunner)
at MyBatis.DataMapper.MappedStatements.MappedStatement.ExecuteInsert(ISession session, Object parameterObject)
at MyBatis.DataMapper.DataMapper.Insert(String statementId, Object parameterObject)

 错误信息也很简单。在向OracleParameter的属性Value赋值时报错。

2、找不出的错误原因

  源码如下:

  

 [Category("Data"), Description(""), DefaultValue((string) null)]
public override object Value
{
get =>
this.m_value;
set
{
if (((value != null) && (value != DBNull.Value)) && (this.m_enumType == PrmEnumType.NOTSET))
{
Type type = value.GetType();
if (((type == typeof(sbyte)) || (type == typeof(ushort))) || ((type == typeof(uint)) || (type == typeof(ulong))))
{
throw new ArgumentException();
}
object obj2 = OraDb_DbTypeTable.s_table[type];
if ((obj2 == null) && type.IsArray)
{
obj2 = OraDb_DbTypeTable.s_table[type.GetElementType()];
}
if (obj2 == null)
{
throw new ArgumentException();
}
this.m_oraDbType = (OracleDbType) obj2;
this.m_bSetDbType = false;
this.m_enumType = PrmEnumType.VALUE;
}
this.m_value = value;
}
}

  从源码可以看到,报错可能出现在两个地方:if (((type == typeof(sbyte)) || (type == typeof(ushort))) || ((type == typeof(uint)) || (type == typeof(ulong))))【更老的驱动版本bool类型也不支持】 和 if (obj2 == null)。出现这个错误说明无法将C#类型映射为Oracle类型。

  首先检查第一处可能,发现插入报错的类中使用的类型有DateTime,DateTime?,long,int,string。发现第一处不满足。

  检查第二次可能,第二处取类型是从 OraDb_DbTypeTable 获取到的。源码如下:

  

 internal static void InsertTableEntries()
{
s_table.Add(typeof(byte), OracleDbType.Byte);
s_table.Add(typeof(byte[]), OracleDbType.Raw);
s_table.Add(typeof(char), OracleDbType.Varchar2);
s_table.Add(typeof(char[]), OracleDbType.Varchar2);
s_table.Add(typeof(DateTime), OracleDbType.TimeStamp);
s_table.Add(typeof(short), OracleDbType.Int16);
s_table.Add(typeof(int), OracleDbType.Int32);
s_table.Add(typeof(long), OracleDbType.Int64);
s_table.Add(typeof(float), OracleDbType.Single);
s_table.Add(typeof(double), OracleDbType.Double);
s_table.Add(typeof(decimal), OracleDbType.Decimal);
s_table.Add(typeof(string), OracleDbType.Varchar2);
s_table.Add(typeof(TimeSpan), OracleDbType.IntervalDS);
s_table.Add(typeof(OracleBFile), OracleDbType.BFile);
s_table.Add(typeof(OracleBinary), OracleDbType.Raw);
s_table.Add(typeof(OracleBlob), OracleDbType.Blob);
s_table.Add(typeof(OracleClob), OracleDbType.Clob);
s_table.Add(typeof(OracleDate), OracleDbType.Date);
s_table.Add(typeof(OracleDecimal), OracleDbType.Decimal);
s_table.Add(typeof(OracleIntervalDS), OracleDbType.IntervalDS);
s_table.Add(typeof(OracleIntervalYM), OracleDbType.IntervalYM);
s_table.Add(typeof(OracleRefCursor), OracleDbType.RefCursor);
s_table.Add(typeof(OracleString), OracleDbType.Varchar2);
s_table.Add(typeof(OracleTimeStamp), OracleDbType.TimeStamp);
s_table.Add(typeof(OracleTimeStampLTZ), OracleDbType.TimeStampLTZ);
s_table.Add(typeof(OracleTimeStampTZ), OracleDbType.TimeStampTZ);
s_table.Add(typeof(OracleXmlType), OracleDbType.XmlType);
s_table.Add(typeof(bool), OracleDbType.Boolean);
s_table.Add(typeof(DateTimeOffset), OracleDbType.TimeStampTZ);
}

  发现里面没有DateTime?,难道是这个地方不行。那也不对啊,其它地方同样类型是可以保存到数据库的啊。继续找下去在 MyBatis.DataMapper.Data.DefaultPreparedCommand.ApplyParameterMap 找到了信息。源码如下:

  

 public virtual void SetParameter(IDataParameter dataParameter, object parameterValue, string dbType)
{
if (parameterValue != null)
{
dataParameter.Value = parameterValue;
}
else
{
dataParameter.Value = DBNull.Value;
}
}

  如果值为null,会自动赋值为 DBNull.Value 也就会不会报错。既然都不会出现问题,可问题又会出现在哪里呢?

3、柳暗花明

  在本地尝试各种方法后仍然不能重现,只能让领导将生产程序包拷贝一份下来进行尝试。

  包拿到之后,进行必要配置启动,出现了新的诡异问题 Redis报错 :No connection is available to service this operation。仔细检查Redis配置发现问题。抓下内存快照看看实际配置信息是什么:

  Mybatis.NET Oracle 线上神奇问题:Value does not fall within the expected range.

  看到这个一脸懵逼,Password竟然是null,明明已经配置了啊。查看了加载配置的源码,发现根本不会解析Password配置,还有这种操作,这程序包是有多老。弄好了Redis,启动保存数据,果然不行。反编译生产源码,发现惊天一幕:

  Mybatis.NET Oracle 线上神奇问题:Value does not fall within the expected range.

  Id的类型竟然ulong。我擦这是多久没有更新了。原因至此是找到了。

4、吐槽

  这个服务是公司内部自己用的,很古老了。以前都是手工拷贝程序包发布的。最近由于公司发生了些意外事情,原来服务器不能用,需要部署新的服务器上。由于古老,不知道老大当时拿了多么古老的程序包部署了。