Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Databases and Redis

C# INSERT fails with 'Fatal error encountered during command execution'

ErrorFatal error encountered during command execution.

Short answer

A fatal error during command execution means the connection broke while the INSERT was in flight. The server usually closed it: the statement was bigger than max_allowed_packet, or the connection sat idle past wait_timeout. A connection shared by two operations at once breaks it from the client side too.

  • Applies to MySQL 8.4, MySqlConnector 2.x, .NET 8
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Read the two server limits that most often close a connection during a write.

    SHOW VARIABLES LIKE 'max_allowed_packet';
    SHOW VARIABLES LIKE 'wait_timeout';
  2. 2Match what you see, then jump to its fix

    1. 1Only big inserts fail: a large blob, long text, or one INSERT with thousands of rowsThe INSERT is larger than max_allowed_packetFix
    2. 2The first write after a quiet period fails, then the next one worksThe server closed an idle pooled connectionFix
    3. 3Errors under parallel load, or an INSERT inside a loop that is still reading from the same connectionTwo operations used the same connection at the same timeFix

What does the error look like?

  • Fatal error encountered during command execution.
  • MySql.Data.MySqlClient.MySqlException: Fatal error encountered during command execution

Cause 1 of 3

The INSERT is larger than max_allowed_packet

1Check

If mysqld receives a packet that is too large it treats the client as broken and closes the connection. Compare your largest statement or row with the server value above.

SHOW VARIABLES LIKE 'max_allowed_packet';

2Fix

Raise the limit on the server, and split very large multi-row INSERT statements into smaller batches, because all rows in one statement travel in one request.

SET GLOBAL max_allowed_packet = 134217728;
-- or in my.cnf under [mysqld]:
-- max_allowed_packet = 128M

3Verify

Open a new connection and read the value; the setting applies to new connections only.

SHOW VARIABLES LIKE 'max_allowed_packet';

Confirmed by dev.mysql.com

Did this fix it?

Cause 2 of 3

The server closed an idle pooled connection

1Check

The server closes idle connections after wait_timeout seconds (28800, eight hours, is the documented default). A pooled connection that lives longer than the limit set on your server is dead when reused.

SHOW VARIABLES LIKE 'wait_timeout';

2Fix

Raise wait_timeout, or make the pool retire connections sooner with Connection Lifetime so they never outlive the server limit.

Server=db.example.com;Database=app;User ID=app;Password=...;Connection Lifetime=300

3Verify

Run the same insert after an idle period longer than before.

dotnet run

Confirmed by mysqlconnector.net

Did this fix it?

Cause 3 of 3

Two operations used the same connection at the same time

1Check

A MySqlConnection can run only one operation at a time. Parallel ExecuteAsync calls, a command issued inside a reader.Read() loop, or a Dispose while an operation runs all break it.

grep -R -n "Task.WhenAll" --include=*.cs .

2Fix

Give each parallel task its own connection, or await operations one after another. Finish and close a reader before running another command, and use using or await using for the connection.

await using var conn = new MySqlConnection(connectionString);
await conn.OpenAsync();
await using var cmd = new MySqlCommand("INSERT INTO items (name) VALUES (@n)", conn);
cmd.Parameters.AddWithValue("@n", name);
await cmd.ExecuteNonQueryAsync();

3Verify

Repeat the load that failed before.

dotnet test

Confirmed by mysqlconnector.net

Did this fix it?

How do I stop it coming back?

  • Insert large data in batches, not in one giant statement.
  • Create a connection per operation and let pooling reuse it.
  • Know the max_allowed_packet and wait_timeout values on every server you deploy to.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.