Skip to main content

How to Set the Connect Timeout in MySql.Data

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

Connect Timeout is how long MySql.Data waits for a connection, and it defaults to 15 seconds. That looks harmless until retries multiply it. An unreachable server held one request for 64 seconds in my test, and a smaller value brought that to 24.

What It Does​

Oracle's documentation: "The length of time (in seconds) to wait for a connection to the server before terminating the attempt and generating an error."

In this driver, the same number does three jobs:

  1. It limits connecting to the server.
  2. It limits waiting for a connection from a full pool.
  3. It's a timer on the TLS handshake, with a sharp edge.

It has nothing to do with how long a query may run.

Set It​

Connection string
…;Connect Timeout=5

Connection Timeout means the same. Don't use 0: every pooled connection then fails.

See What It Costs​

A stopped server refuses at once, so it fails fast whatever the setting. An unreachable one just doesn't answer, and that's where this matters. I pointed a .NET API at an address that doesn't respond, 192.0.2.1, which is reserved for documentation. This API uses EF Core with 3 retries, so 4 attempts in all:

Output
== Connect Timeout=15 (the default)
GET /health/ready: 503 after 5.096208s
GET /api/v1/categories: 503 after 64.495866s
== Connect Timeout=5
GET /health/ready: 503 after 5.107095s
GET /api/v1/categories: 503 after 24.349170s

A request to an unreachable address taking 64 seconds with Connect Timeout=15, and 24 seconds with Connect Timeout=5.

One attempt4 attempts, plus delays between them
Connect Timeout=1515 s64 s
Connect Timeout=55 s24 s

Nobody waits a minute for an API. /health/ready is 5 seconds on both rows only because it has its own timeout. Your network may also reject that address outright, rather than ignore it, so it may fail faster for you.

Choosing a Number​

  • Local network, or the same data center: 5 seconds is plenty, because a healthy connection takes well under one.
  • Across the internet: a little more.
  • Work out the worst case: attempts × timeout, plus retry delays. That's what a client sees during an outage.

The Sharp Edge​

If this timer runs out during a TLS handshake, MySql.Data throws an exception that nothing can catch, and the whole process ends. A handshake takes milliseconds, so that takes a network stall or a starved thread pool. A longer timeout doesn't remove the risk, and a very short one makes it a bit easier to hit, so keep your number sensible rather than as low as it'll go, and run your app under something that restarts it.

Common Mistakes​

MistakeWhat happensDo this instead
Confusing it with CommandTimeoutNeither does the other's jobThis is only about connecting
Forgetting that retries multiply itOutages take far longer than you plannedWork out the worst case
Connect Timeout=0 for "no limit"Every pooled connection failsUse a number

Troubleshooting​

SymptomLikely causeFix
Unable to connect to any of the specified MySQL hosts after the timeoutThe server didn't answerThe fix
Timeout expired … from the poolThe pool was full for this longThe fix
The process ends with Unhandled exceptionThe timer ran out during a handshakeThe fix

Conclusion​

Connect Timeout limits connecting, not queries, and retries multiply it. Work out your worst case, and pick a number that a client can live with: 5 seconds suits most networks.

Additional Resources​

Stay Updated

Subscribe to our newsletter for the latest tutorials, tech insights, and developer news.

By subscribing, you agree to our privacy policy. Unsubscribe at any time.