I have a .Net service that connects to an Oracle database on every request. It works fine at the beginning, but after some number of requests I start getting:
Oracle.DataAccess.Client.OracleException ORA-03135: connection lost contact
at Oracle.DataAccess.Client.OracleException.HandleErrorHelper(Int32 errCode, OracleConnection开发者_JAVA百科 conn, IntPtr opsErrCtx, OpoSqlValCtx* pOpoSqlValCtx, Object src, String procedure)
at Oracle.DataAccess.Client.OracleException.HandleError(Int32 errCode, OracleConnection conn, String procedure, IntPtr opsErrCtx, OpoSqlValCtx* pOpoSqlValCtx, Object src)
at Oracle.DataAccess.Client.OracleCommand.ExecuteReader(Boolean requery, Boolean fillRequest, CommandBehavior behavior)
at Oracle.DataAccess.Client.OracleCommand.ExecuteReader()
at MyApp.Services.OracleConnectionWithRetry.ExecuteReader(OracleCommand command)
...
Any idea what might be the problem? I dispose all the connections, results and parameters. The load on this service is, well, very low.
It happens because your code requests a connection from the Oracle Connection Pool and the connection pool returns a disconnected / stale connection to the Oracle DB. ODP.NET does not itself test the connection status of the connection sent to client.
So to be safe, either you check the connection status == Open
for the connection received from the pool when you do a Connection.Open()
OR
let ODP.NET do the checking for you by setting Validate Connection = true
in your connection string in web.config.
Both this methods have a impact on performance as they test the connection status each time you need to connect to the database.
A third option which I use is use of exceptions. First be optimistic and use whateven connection is returned from the connection pool. If you get a ORA - 3135 then request a new connection and execute your query again like a while loop. In best case, you can get your 1st connection as valid and your query will execute. In worst case, all the connections in your pool are stale in which case the code will be executed N time (where N is the connection pool size).
I have seen this happen too; try turning off connection pooling with "Pooling=false" in the connection string. I have a theory that idle connections in the pool expire, but ODP.NET does not realize that they have expired, and then when your app grabs one and tries to do something you get that exception.
精彩评论