Hi Experts,

I have a windows service running every minute and has many database operations to perform.
It runs perfectly for many days, sometimes it suddenly stops and throws this exception. Later when we start to debug it, the exceptions goes off, but if we don't debug, the exception continues to come. We are tired of this totally random scenario. Please assist us with this.

We are using for database interaction.

"Timeout expired. The timeout period elapsed prior to completion of the operation or the server is not responding."

Best Regards,
Wajid Ahmed Ansari.

Dani AI

Generated

described a long-running Windows service that intermittently starts getting database command timeouts after days of normal operation; correctly pointed out that the immediate symptom is an operation taking longer than its allowed time. The debugger hiding the problem is a strong clue: attaching a debugger changes timing and concurrency, which often masks blocking, race conditions, or resource contention on the server.

Likely causes to prioritize:

  • Connection-pool exhaustion or leaked connections (connections not closed/disposed).
  • Blocking by an open/uncommitted transaction or a long-running statement that serializes access.
  • Periodic server-side events (auto-grow, backups, index maintenance, tempdb pressure) causing spikes in I/O latency.
  • Plan changes / parameter sniffing producing very slow plans intermittently.
  • Network hiccups or authentication timeouts are less common but possible.

Practical, immediate checks and fixes:

  • Add lightweight timing/logging around every DB call (log command text, parameters, start/end times, and whether connection open/close happened).
  • Enforce proper disposal of connections/commands (using pattern) and avoid leaving transactions open.
  • Temporarily increase CommandTimeout only to help capture the offending SQL and plan; do not use it as the permanent fix.

Example C# pattern to ensure disposal and measure duration:

using (var conn = new SqlConnection(connString))
{
    using (var cmd = conn.CreateCommand())
    {
        cmd.CommandText = "/* actual SQL */";
        cmd.CommandTimeout = 60;
        var sw = Stopwatch.StartNew();
        conn.Open();
        cmd.ExecuteNonQuery();
        sw.Stop();
        // log sw.ElapsedMilliseconds and command text
    }
}

Server-side diagnostics to capture:

  • Capture blocking chains and currently executing SQL (sp_who2 or sys.dm_exec_requests / sys.dm_tran_locks).
  • Collect execution plans for slow statements and check wait stats (LCK*, PAGEIOLATCH*, ASYNC_IO).
  • Monitor connection-pool counters and key perf counters (Page life expectancy, disk queue length, CPU, I/O latency).
  • Use Extended Events (or Profiler with caution) to trace long-running queries and deadlocks.

Do not rely on simply increasing timeouts; gather the slow SQL text, execution plan, blocking chain, and server perf snapshots to identify and fix the root cause.

Recommended Answers

All 5 Replies

It's exactly as it says. The operation you are trying to perform is taking longer than the specified time-out. You should look at where the exception occurs, what operation is being performed and the data it is working with.

Also check that the database service is up and running and you can connect to it and perform queries on it.

It's exactly as it says. The operation you are trying to perform is taking longer than the specified time-out. You should look at where the exception occurs, what operation is being performed and the data it is working with.

Also check that the database service is up and running and you can connect to it and perform queries on it.

Already did that, database is up and running perfectly.

Did you try the bit I wrote above it?

It seems a DB problem, but i connected through Sql Management studio and iam able to do everything through it (Like querying,updating etc...), just through code is the problem.

The service is calling DB very frequently. Since three hours the service is responding well and no sign of DB overloads. Also it had been on for 20 days without rest and everything was well.

I will post the message if i get this exception again, i already cleared my logs before.

The additional insight into the problem is every method going to DB suffers the same exception. Like simple update statement running within a second now takes more than 30 seconds when this problem comes up.

So i got curious as whats the main issue.

Hope this helps to get hold of problem.

Did you try the bit I wrote above it?

It seems a DB problem, but i connected through Sql Management studio and iam able to do everything through it (Like querying,updating etc...), just through code is the problem.

The service is calling DB very frequently. Since three hours the service is responding well and no sign of DB overloads. Also it had been on for 20 days without rest and everything was well.

I will post the message if i get this exception again, i already cleared my logs before.

The additional insight into the problem is every method going to DB suffers the same exception. Like simple update statement running within a second now takes more than 30 seconds when this problem comes up.

So i got curious as whats the main issue.

Hope this helps to get hold of problem.

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.