Search code examples
asp.net-coreodp.netasp.net-core-2.2odp.net-managed

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


With ASP.NET Core 2.2, Dapper Oracle 1.0.1 and managed Oracle ODP drivers 2.19.50 Using the following code to issue a simple read query. The code below is being called maybe ~10 times a minute by different API consumers.

// 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;
}

Most of the time the above works fine, but have been getting seemingly random .NET exceptions like the one below.

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)

After turning on tracing via OracleConfiguration.TraceLevel = 7 There are a lot of exceptions being logged from the driver that occur in these chunks:

(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()

The above exception led me to believe that I might not be creating/disposing/etc. the connection correctly since it looks like it's trying to read from a closed connection?

Is there anything in the code above that's obviously causing this?


Solution

  • This fixed the random errors that were happening. Not sure why though. Update the connection string to include a timeout, e.g. ;Connection Timeout=600