Question

In a “using” block is an OracleConnection closed if System.Environment.Exit(0) is issued in the catch statement?

Example:

        OracleConnection oracleConnection = getOracleConnection();

        using (oracleConnection)
        {

            try
            {

                oracleConnection.Open();
                OracleCommand cmd = getApplicantsCmd(oracleConnection);
                OracleDataReader rdr = cmd.ExecuteReader(); 

                List<Applicant> applicants = new List<Applicant>();
                while (rdr.Read())
                {
                    Applicant applicant = new Applicant();
                    applicant.email = (string)rdr["GOREMAL_EMAIL_ADDRESS"];
                    applicants.Add(applicant);                   
                }

                return applicants;

            }
            catch (Exception ex)
            {
                Console.WriteLine("Failure getting applicant records: " + ex.Message);
                System.Environment.Exit(0);
                return null;
            }
        }

I want execution to stop if an exception is thrown while looking up the records.

Is there a better way to handle this?

Was it helpful?

Solution

There will be no call to oracleConnection's Dispose() method after the call of System.Environment.Exit, even though the variable is wrapped in using. If you would like to make sure that the connection is disposed, either put the using inside try/catch, or do not call System.Environment.Exit inside using. You can still exit if you set a flag, and act upon it after the using statement:

var mustExit = false;
using (var oracleConnection = getOracleConnection()) {
    try {
        ...
    } catch {
        Console.WriteLine("Failure getting applicant records: " + ex.Message);    
        mustExit = true;
    }
}
if (mustExit) {
    System.Environment.Exit(0);                
}

OTHER TIPS

The physical connection to the database is not closed. It's just returned to the ADO.NET connection pool for reuse. ADO.NET keeps a pool of connections to avoid opening a physical connection to the database everytime you want to perform a SQL query. You should also wrap your OracleCommand and OracleDataReader in using statements to ensure proper disposal even in the event of an exception.

Yes, it is, and is disposed therein too shortly afterwards.

EDIT: unless it is pooled, and as is explained, will be available in the pool again.

I believe that your question in general is whether the Dispose method of object used in an using statement will be called if Environment.Exit is called withing using block.

The answer is no. If you call Environment.Exit or Environment.FailFast, the process is terminated and finally block (the implicit one, resulting from using statement) will not be executed.

On the other hand, if you exit your app by calling Application.Exit or Application.ExitThread it will be called.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top