如何安全地创建与 oracle 和 asp.net 核心的连接?

How to safely create a connection with oracle and asp.net core?

使用 ASP.NET Core 2.2、Dapper Oracle 1.0.1 和托管 Oracle ODP 驱动程序 2.19.50 使用以下代码发出简单的读取查询。下面的代码可能被不同的 API 消费者每分钟调用约 10 次。

// connectionString = Data Source=thehost:1521/blah;User Id=bob;Password=bob
using (IDbConnection dbConnection = new OracleConnection(this.connectionString))
{
    dbConnection.Open();
    var results = dbConnection.Query<MyDBModelClass>(myQuery);
    return results;
}

在大多数情况下,上面的方法工作正常,但出现了看似随机的 .NET 异常,如下所示。

SocketException: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond

OracleInternal.Network.ReaderStream.Read(OraBuf OB) NetworkException: ORA-12570: Network Session: Unexpected packet read error

OracleInternal.Network.ReaderStream.Read(OraBuf OB) OracleException:

ORA-12570: Network Session: Unexpected packet read error Oracle.ManagedDataAccess.Client.OracleException.HandleError(OracleTraceLevel level, OracleTraceTag tag, OracleTraceClassName className, OracleTraceFuncName funcName, Exception ex, OracleLogicalTransaction oracleLogicalTransaction)

通过 OracleConfiguration.TraceLevel = 7 打开跟踪后,驱动程序记录了很多异常发生在这些块中:

(PRI) (EXT) Connection.connect()
(PRI) (ONS) (EXT) Connection.connect()
(PRI) (ONS) (EXT) ReceiverThread.establishConnection()
(PRI) (ONS) (ENT) Concurrency.setListFailed()
(PRI) (ONS) (EXT) Concurrency.setListFailed()
(PRI) (ONS) (ENT) ONS.nodeListFailOver()
(PRI) (ONS) (EXT) ONS.nodeListFailOver()
(PRI) (ONS) (ENT) ReceiverThread.establishConnection()
(PRI) (ONS) (ENT) Connection.connect()
(PRI) (ENT) Connection.connect()
(PRI) (ONS) (ERR) Connection.connect() (txnid=n/a) System.IO.IOException: The operation is not allowed on non-connected sockets.
   at System.Net.Sockets.NetworkStream..ctor(Socket socket, FileAccess access, Boolean ownsSocket)
   at OracleInternal.NotificationServices.Connection.connect()

以上异常让我相信我可能不是 creating/disposing/etc。连接是否正确,因为它看起来像是在尝试从已关闭的连接中读取数据?

上面的代码中是否有任何明显导致此问题的地方?

这修复了发生的随机错误。不知道为什么。更新连接字符串以包含超时,例如;Connection Timeout=600