2

Problem is how to reconnect to database Oracle after troubles

I have .net Core web-api project in docker, where I connect to Oracle database. For connecting I use nugget package oracleClientCore

How I connect and call stoted procedure:

string cs = "Data Source = 172.10.200.100:1521/dev;PERSIST SECURITY INFO=True;USER ID=test; Password=devtest;";
using (OracleConnection connection = new OracleConnection(cs)){
  connection.Open();
  using (OracleCommand cmd = connection.CreateCommand()) {
    cmd.CommandType = CommandType.StoredProcedure;
    cmd.CommandText = "sp_check_db";
    cmd.ExecuteNonQuery();
    connection.Close();
  }
}

In stored procedure sp_check_db just make insert into table

Sometimes the connection to database falling down and I got exception ORA-03114: not connected to ORACLE. After the database was enabled again I continue to receive same error ORA-03114: not connected to ORACLE till I rebuild and redeploy the project.

What can I do in this situation, it's not right behavior? Something wrong with my code or connection string?

1
  • 1
    @MarcusHöglund It's my imaginary IP :) Commented Jul 2, 2018 at 10:22

1 Answer 1

2

When your application is dependent on external integrations (in this case your db) which can be unreachable for small periods of time due to patches, network failare etc, its suitable to implement an retry policy. The retry logic will, depend on how you configure it, rerun the code a certain of time if a specific exception occours.

Here's an example of how to implement a simple retry logic with the framework Polly which will rerun your code three times in a ten second span if an exception containing the ORA-03114 occours. If the code still throws the same exception after the third time the exception will go throw.

var retryTimes = 3;
var waitBetweenExceptions = TimeSpan.FromSeconds(10);

var retryPolicy = Policy
    .Handle<OracleException>(e => e.Message.Contains("ORA-03114"))
    .WaitAndRetry(retryTimes, i => waitBetweenExceptions);

await retryPolicy.Execute(() =>
{
    string cs = "Data Source = 172.10.200.100:1521/dev;PERSIST SECURITY INFO=True;USER ID=test; Password=devtest;";
    using (OracleConnection connection = new OracleConnection(cs)){
      connection.Open();
      using (OracleCommand cmd = connection.CreateCommand()) 
      {
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.CommandText = "sp_check_db";
        cmd.ExecuteNonQuery();
        connection.Close();
      }
    }
});
Sign up to request clarification or add additional context in comments.

3 Comments

It's good answer and I'll use this code, but it isn't that I expected - error happens when already database enabled for calling sp - and anly rebuild and redeploy project helps me - maybe we can reset connection?
@IgorCova this might has something to do with the docker container configuration rather then on how you call the oracle db
Well, then I need to investigate the problem deeper. and at the moment Your answer is correct if we think that the connection problem occurred at the time of calling api

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.