I am running a service that queries on regular intervals. I initially set it up so that the connection formed onStart, and closed onStop. This of course leaves a connection open all the time, which isn't what I want. So I moved the open and closed to inside my timer event handler.

My question: Is there a difference between

try{connection.Open();}
catch{error;}
someAction();
try{connection.Close();}
catch(Exception)
{error}

and

try{ connection.Open();
someAction(); 
connection.Close();
}
catch(Exception)
{error}

If so which is better?

Dani AI

Generated

Short answer: both of the examples as written can leak a connection when an exception occurs. was right to flag the gap: if someAction() throws, the code after it (including your close) will usually not run unless you explicitly handle that exception. The safe patterns are either a using (which suggested) or an explicit try/finally that guarantees Close/Dispose runs.

Why this matters: SqlConnection implements IDisposable, and calling Close/Dispose returns the physical connection to the pool (pooling is enabled by default). That makes open/close-per-operation the recommended approach for services: open late, close early. Also avoid swallowing all exceptions — catch specific exceptions (for example SqlException), log, and decide whether to retry or rethrow.

Example of a safe pattern (different from the earlier snippets):

SqlConnection conn = new SqlConnection(connString);
try
{
    conn.Open();
    using (SqlCommand cmd = conn.CreateCommand())
    {
        cmd.CommandText = "...";
        // perform DB work here
    }
}
finally
{
    if (conn != null)
        conn.Dispose(); // Dispose calls Close internally
}

Practical tips: don’t catch and ignore Exception; prefer specific exceptions and logging. For long-running services consider transient-fault retry logic (e.g., Polly) rather than silently retrying inside a catch. Ensure your timer prevents overlapping executions so one run can finish and close its connection before the next starts.

Recommended Answers

All 4 Replies

kinda hard to tell, but what happens with your first example if there is an exception during someAction()

I am running a service that queries on regular intervals. I initially set it up so that the connection formed onStart, and closed onStop. This of course leaves a connection open all the time, which isn't what I want. So I moved the open and closed to inside my timer event handler.

My question: Is there a difference between

try{connection.Open();}
catch{error;}
someAction();
try{connection.Close();}
catch(Exception)
{error}

and

try{ connection.Open();
someAction(); 
connection.Close();
}
catch(Exception)
{error}

If so which is better?

Why not just use a using statement? Then you don't have to worry about closing the connection. :)

try {
  using ( connection = new SqlConnection( connectionString ) ) {
    someAction();
  }
}
catch ( Exception ) {
  // Error
}

kyti

ahmad

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.