MySQL .NET Connector Connection Pooling Bug

If you are using the MySQL .NET connectors and run across the message "Stream does not support writing" like I did, you will probably have a few unpleasant days ahead. Have no fear though, the solution isn't too hard to implement. The problem is that the connection pooling mechanism in the drivers is broken. If an error occurs at the packet level, the underlying streams to the connection are closed, but the connection is not removed from the connection pool. As a result, the connection (which is permanently broken when the underlying streams are closed) gets returned to subsequent connection calls and results in your server crashing for all calls made on that pooled connection. I've reported the bug to the MySQL team, so hopefully it will be officially fixed--but, in the mean time, you can simply add a property to the PacketReader and PacketWriter that ensures that the stream can be read from / written to and then a property on the NativeDriver / Driver class that ensures that this value is true. Then, from the connection pool manager, in the loop that searches idle connections, do a sanity check on the connection before returning it to the app. If the connection's streams have been broken, remove it from the idle pool instead of continuing.

If you don't want to deal with this, the root of the problem for me was that the max_packet_size variable in the config needed to be bumped from the default of 1MB up to something large enough to handle my blob columns. Setting it high enough eliminates the packet level error that was putting connections in the bad state, but I am sure there is another situation that could put them in an invalid state, so fixing the pooling bug is probably a good idea regardless.

You might also be able to avoid the problem by switching to named pipe connections... but I need IP based connections ATM.

2 Comments

Comments have been disabled for this content.