2

I have a C# program that connects to a remote server to query data. The data is quite big so the query takes about 2 mins to finish. During this 2 min window, the internet went down. This resulted in the job being unable to finish with the program stuck in the getting data routine.

It established connection but during the select query it was cut off. Setting the command timeout to 30 seconds did not work. I need the query to fail when encountering this error because the program can handle failure but it cannot handle being stuck. Thanks!

UPDATE: included code

OracleConnection connection = new OracleConnection(connectionstring);
OracleDataAdapter oracleDataAdapter = new OracleDataAdapter(new OracleCommand(query, connection));
oracleDataAdapter.SelectCommand.CommandTimeout = 30;

  DataSet dataSet = new DataSet();
  try
  {
    oracleDataAdapter.Fill(dataSet, table); //Hangs on this line when connection is lost
    return dataSet;
  }
  catch
  {
    throw;
  }
  finally
  {
    dataSet.Dispose();
    oracleDataAdapter.Dispose();
  }

UPDATE AGAIN:

What I need to do is handle this situation because I don't want a dangling process.

Simplest would be once the connection is lost is that the program will throw an error. That is what I don't know how to do. I assumed that the commandtimeout will fix it but it did not.

Jeremy Thompson
  • 61,933
  • 36
  • 195
  • 321
furansu
  • 43
  • 1
  • 6
  • If this is while querying data, is it not an OracleDataReader? Post up the code (minus connection string details) for how you are opening your connection and executing your query, including any exception handling. – Steve Py Jul 05 '17 at 02:45
  • Hi Steve, I just updated my question. I am using DataSet to fill in the data – furansu Jul 05 '17 at 02:50
  • Are you using ODP.net (Provided by Oracle) or Microsoft's deprecated OracleClient? – Steve Py Jul 05 '17 at 03:09
  • I'm using Oracle.DataAccess.Client – furansu Jul 05 '17 at 03:16
  • do you mean ui freeze when internet went down? consider **threads** to query database? – Lei Yang Jul 05 '17 at 03:19
  • @LeiYang the program is running in console, I have messages after every routine. the program is stuck on the getting data routine. Sorry if this sounds stupid but I don't know how to use threads here because i need the data before i can proceed to the next routine. It will be easier for me if the program just fails and throws an exception – furansu Jul 05 '17 at 03:23

3 Answers3

1

I see a couple issues with your statement, assuming it's using ODP.Net. Try the following:

DataSet dataSet = new DataSet();
using (OracleConnection connection = new OracleConnection(connectionstring))
{
  using (OracleDataAdapter oracleDataAdapter = new OracleDataAdapter(new OracleCommand(query, connection)))
  {
    oracleDataAdapter.Fill(dataSet, table);
  }
}
return dataSet;

The using blocks will handle disposing of the connection and data adapter. In your example the connection did not get disposed which may have been part of your issue. Additionally I don't think you want to dispose the dataset if you intend to return it.

Since you were bubbling up the exception with a Throw I removed the exception handling. Keep in mind that this will bubble the exception so somewhere in your calling code chain you will need to catch the exception and handle it. If the app is just sitting there then be wary of any empty "catch" blocks eating exceptions.

Steve Py
  • 26,149
  • 3
  • 25
  • 43
  • Hi Steve, i tried your answer but it is still the same. It is still stuck in the select query routine. reestablishing the connection didn't do anything so I'm still stuck with an unending program :| – furansu Jul 05 '17 at 03:39
  • if you run it in a debug session, set your Visual Studio exception handling to break on all exceptions, and then yank your network cable, do you not get an exception at all? (and if no exception, does hitting the "pause" button to break execution sit on the .Fill() statement?) – Steve Py Jul 05 '17 at 04:01
  • Hi Steve, yes I am disconnecting my machine and it throws no exception, just stuck there. But I tried adding jeremy's suggestion to explicitly open a connection and it worked. Thanks a lot for your help! – furansu Jul 05 '17 at 04:07
1

There are a few duplicates reporting this problem, eg: System being hang when the connection is lost while adapter is filling the datatables

I found a good thread on MSDN where the OP answers:

I have solved this problem a while back, sorry i forgot to come and let you all know. I worked out that the code stopped executing at that line because (for some reason) there was already an open connection to the database.

Since DA.Fill would open a connection itself if there wasnt one previously opened, it was having a hissy fit and bombing out.

I solved this by putting Connection.Close(); before and after any connection to the database is needed.

Based on this we can see you are not explicitly opening a Connection to the Database. Suggest you do a:

connection.Open();

Also follow Steve Py's answer with the using to confirm you are closing the connection and disposing unmanaged resources.

Jeremy Thompson
  • 61,933
  • 36
  • 195
  • 321
  • Hi Jeremy, I updated my code with connection.open and now it works!. Thanks a lot! – furansu Jul 05 '17 at 04:05
  • Great! Can you please mark my answer correct (its the tickbox to the left of this answer, under the voting up/down buttons) - doing so will give you a couple of rep points and let everyone know your problem is solved. Good luck! – Jeremy Thompson Jul 05 '17 at 04:17
  • I don't think it is very smart to check particular error based on error message text (which can be also given in another language depending on your local settings). Better check on error type and/or error number. – Wernfried Domscheit Jul 05 '17 at 05:49
  • I was just observing an answer from you Wernfried. I agree with your comment... let me edit. – Jeremy Thompson Jul 05 '17 at 05:50
1

Updated answer:

        DataSet dataset = new DataSet();

        using (OracleConnection connection = new OracleConnection(connection))
        {
            using (OracleDataAdapter oracleDataAdapter = new OracleDataAdapter(new OracleCommand(query, connection)))
            {
                oracleDataAdapter.SelectCommand.CommandTimeout = 30;
                connection.Open();
                oracleDataAdapter.Fill(dataset, table);
            }
        }

        return dataset;
furansu
  • 43
  • 1
  • 6