• MySQL知识库 :: c#
  • When sending large data packets using compression it fails sometimes.

  • Problem

    When you attempt to send data packets larger than 16M to a server using compression, it will fail in unpredictable ways. Most likely, the server will disconnect from the client immediately.

    Cause

    Connector/Net is not properly managing the byte stream when the size of a packet crosses the 16M barrier and we are using compression. A further explanation would require a lengthy explanation of the wire-level protocol.

    Solution

    Don’t use compression

    More Information

    Steps to reproduce behavior:

    1. Set max_allowed_packet on your server to a value greater than 16 megabytes.
    2. Create a connection object including “compress=true” on the connection string.
    3. Create a table with a LONGBLOB column
    4. Create an array of bytes greater than 16 megabytes long
    5. Attempt to insert the array of bytes into the table using an insert statement.
    MySqlConnection conn = new MySqlConnection(“server=;uid=;pwd=;database=;compress=true”);
    conn.Open();
    
    MySqlCommand cmd = new MySqlCommand(“CREATE TABLE Test (testval LONGBLOB)”, conn);
    cmd.ExecuteNonQuery();
    
    byte[] byteArray = new byte[ 20000000 ];
    ….put some values in the array ….
    
    cmd.CommandText = “INSERT INTO Test VALUES (?myval)”;
    cmd.Parameters.Add( “?myval”, byteArray );
    cmd.ExecuteNonQuery();
    
    conn.Close()