c#\winform 应用,连接数据库采用随用随开,用完关闭机制。也即是短链接。

报错异常:

21:21:34: Oracle.ManagedDataAccess.Client.OracleException (0x80004005): 出现 Oracle 错误 ORA-12571 ---> OracleInternal.Network.NetworkException (0x80004005): 出现 Oracle 错误 ORA-12571 ---> System.Net.Sockets.SocketException (0x80004005): 远程主机强迫关闭了一个现有的连接。
   在 System.Net.Sockets.Socket.Send(Byte[] buffer, Int32 offset, Int32 size, SocketFlags socketFlags)
   在 OracleInternal.Network.TcpTransportAdapter.Send(OraBuf OB)
   在 OracleInternal.Network.TcpTransportAdapter.Send(OraBuf OB)
   在 OracleInternal.Network.WriterStream.Write(OraBuf OB)
   在 OracleInternal.TTC.OraBufWriter.FlushData()
   在 OracleInternal.TTC.TTCReExecuteSql.WriteMessage(Int16 ttcCallCode, Int32 cursorId, Int32 exerof, Int32 execFlags, Int64 numIterations, Boolean bArrayBinding, MarshalBindParameterValueHelper& marshalBindParamsHelper)
   在 Oracle.ManagedDataAccess.Client.OracleException.HandleError(OracleTraceLevel level, OracleTraceTag tag, Exception ex)
   在 OracleInternal.TTC.TTCReExecuteSql.WriteMessage(Int16 ttcCallCode, Int32 cursorId, Int32 exerof, Int32 execFlags, Int64 numIterations, Boolean bArrayBinding, MarshalBindParameterValueHelper& marshalBindParamsHelper)
   在 OracleInternal.TTC.TTCExecuteSql.SendReExecuteRequest(OracleConnectionImpl commImpl, Int32 cursorId, Int64 noOfRowsToFetch, Boolean bAutoCommit, Boolean bDisableCompressedFetch, SqlStatementType stmtType, Int32 arrayBindCount, MarshalBindParameterValueHelper& marshalArrayBindValuesHelper)
   在 OracleInternal.ServiceObjects.OracleCommandImpl.ExecuteReader(String commandText, OracleParameterCollection paramColl, CommandType commandType, OracleConnectionImpl connectionImpl, OracleDataReaderImpl& rdrImpl, Int32 longFetchSize, Int32 lobPrefetchSize, OracleDependencyImpl orclDependencyImpl, Int64[] scnForExecution, Int64[]& scnFromExecution, OracleParameterCollection& bindByPositionParamColl, Boolean& bBindParamPresent, Boolean isDescribeOnly, Boolean isFromEF)
   在 Oracle.ManagedDataAccess.Client.OracleCommand.ExecuteReader(Boolean requery, Boolean fillRequest, CommandBehavior behavior)
   在 Oracle.ManagedDataAccess.Client.OracleDataAdapter.Fill(DataTable[] dataTables, Int32 startRecord, Int32 maxRecords, IDbCommand command, CommandBehavior behavior)
   在 System.Data.Common.DbDataAdapter.Fill(DataTable dataTable)
   在 lxsx_auto_warehouse.MyDbHelper.queryData(String sql)

经长期观察发现,只有在数据库长时间不访问的时候就会有次报错,

1、

原因 :ManagedDataAccess  连接池机制为 OracleConnection  conn=new  OracleConnection  ()每次都会生成新的数据库连接,conn.close() dispose()都不能有效的清除。

解决:OracleConnection.ClearPool(conn);

数据库连接池有超时时间,客户端长时间不用,数据库会主动释放这个连接。这时候就需要定时查询一下数据库,保持连接,不让数据库释放连接解决。

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐