@@ -314,6 +314,7 @@ public async Task ConnectAsync(ConnectionSettings cs, ILoadBalancer loadBalancer
314314 m_supportsSessionTrack = ( initialHandshake . ProtocolCapabilities & ProtocolCapabilities . SessionTrack ) != 0 ;
315315 var serverSupportsSsl = ( initialHandshake . ProtocolCapabilities & ProtocolCapabilities . Ssl ) != 0 ;
316316 m_characterSet = ServerVersion . Version >= ServerVersions . SupportsUtf8Mb4 ? CharacterSet . Utf8Mb4GeneralCaseInsensitive : CharacterSet . Utf8GeneralCaseInsensitive ;
317+ m_setNamesPayload = ServerVersion . Version >= ServerVersions . SupportsUtf8Mb4 ? s_setNamesUtf8mb4Payload : s_setNamesUtf8Payload ;
317318
318319 Log . Info ( "Session{0} made connection; ServerVersion={1}; ConnectionId={2}; Compression={3}; Attributes={4}; DeprecateEof={5}; Ssl={6}; SessionTrack={7}" ,
319320 m_logArguments [ 0 ] , ServerVersion . OriginalString , ConnectionId ,
@@ -370,6 +371,11 @@ public async Task ConnectAsync(ConnectionSettings cs, ILoadBalancer loadBalancer
370371 if ( m_useCompression )
371372 m_payloadHandler = new CompressedPayloadHandler ( m_payloadHandler . ByteHandler ) ;
372373
374+ // set 'collation_connection' to the server default
375+ await SendAsync ( m_setNamesPayload , ioBehavior , cancellationToken ) . ConfigureAwait ( false ) ;
376+ payload = await ReceiveReplyAsync ( ioBehavior , cancellationToken ) . ConfigureAwait ( false ) ;
377+ OkPayload . Create ( payload . AsSpan ( ) , SupportsDeprecateEof , SupportsSessionTrack ) ;
378+
373379 if ( ShouldGetRealServerDetails ( ) )
374380 await GetRealServerDetailsAsync ( ioBehavior , CancellationToken . None ) . ConfigureAwait ( false ) ;
375381 }
@@ -394,16 +400,12 @@ public async Task<bool> TryResetConnectionAsync(ConnectionSettings cs, IOBehavio
394400 // clear all prepared statements; resetting the connection will clear them on the server
395401 ClearPreparedStatements ( ) ;
396402
403+ PayloadData payload ;
397404 if ( DatabaseOverride is null && ( ServerVersion . Version . CompareTo ( ServerVersions . SupportsResetConnection ) >= 0 || ServerVersion . MariaDbVersion ? . CompareTo ( ServerVersions . MariaDbSupportsResetConnection ) >= 0 ) )
398405 {
399406 m_logArguments [ 1 ] = ServerVersion . OriginalString ;
400407 Log . Debug ( "Session{0} ServerVersion={1} supports reset connection; sending reset connection request" , m_logArguments ) ;
401408 await SendAsync ( ResetConnectionPayload . Instance , ioBehavior , cancellationToken ) . ConfigureAwait ( false ) ;
402- var payload = await ReceiveReplyAsync ( ioBehavior , cancellationToken ) . ConfigureAwait ( false ) ;
403- OkPayload . Create ( payload . AsSpan ( ) , SupportsDeprecateEof , SupportsSessionTrack ) ;
404-
405- // the "reset connection" packet also resets the connection charset, so we need to change that back to our default
406- await SendAsync ( s_setNamesUtf8mb4Payload , ioBehavior , cancellationToken ) . ConfigureAwait ( false ) ;
407409 payload = await ReceiveReplyAsync ( ioBehavior , cancellationToken ) . ConfigureAwait ( false ) ;
408410 OkPayload . Create ( payload . AsSpan ( ) , SupportsDeprecateEof , SupportsSessionTrack ) ;
409411 }
@@ -424,7 +426,7 @@ public async Task<bool> TryResetConnectionAsync(ConnectionSettings cs, IOBehavio
424426 var hashedPassword = AuthenticationUtility . CreateAuthenticationResponse ( AuthPluginData , 0 , cs . Password ) ;
425427 using ( var changeUserPayload = ChangeUserPayload . Create ( cs . UserID , hashedPassword , cs . Database , m_characterSet , m_supportsConnectionAttributes ? cs . ConnectionAttributes : null ) )
426428 await SendAsync ( changeUserPayload , ioBehavior , cancellationToken ) . ConfigureAwait ( false ) ;
427- var payload = await ReceiveReplyAsync ( ioBehavior , cancellationToken ) . ConfigureAwait ( false ) ;
429+ payload = await ReceiveReplyAsync ( ioBehavior , cancellationToken ) . ConfigureAwait ( false ) ;
428430 if ( payload . HeaderByte == AuthenticationMethodSwitchRequestPayload . Signature )
429431 {
430432 Log . Debug ( "Session{0} optimistic reauthentication failed; logging in again" , m_logArguments ) ;
@@ -433,6 +435,11 @@ public async Task<bool> TryResetConnectionAsync(ConnectionSettings cs, IOBehavio
433435 OkPayload . Create ( payload . AsSpan ( ) , SupportsDeprecateEof , SupportsSessionTrack ) ;
434436 }
435437
438+ // set 'collation_connection' to the server default
439+ await SendAsync ( m_setNamesPayload , ioBehavior , cancellationToken ) . ConfigureAwait ( false ) ;
440+ payload = await ReceiveReplyAsync ( ioBehavior , cancellationToken ) . ConfigureAwait ( false ) ;
441+ OkPayload . Create ( payload . AsSpan ( ) , SupportsDeprecateEof , SupportsSessionTrack ) ;
442+
436443 return true ;
437444 }
438445 catch ( IOException ex )
@@ -1393,7 +1400,8 @@ private enum State
13931400 static ReadOnlySpan < byte > BeginCertificateBytes => new byte [ ] { 45 , 45 , 45 , 45 , 45 , 66 , 69 , 71 , 73 , 78 , 32 , 67 , 69 , 82 , 84 , 73 , 70 , 73 , 67 , 65 , 84 , 69 , 45 , 45 , 45 , 45 , 45 } ; // -----BEGIN CERTIFICATE-----
13941401 static int s_lastId ;
13951402 static readonly IMySqlConnectorLogger Log = MySqlConnectorLogManager . CreateLogger ( nameof ( ServerSession ) ) ;
1396- static readonly PayloadData s_setNamesUtf8mb4Payload = QueryPayload . Create ( "SET NAMES utf8mb4 COLLATE utf8mb4_general_ci;" ) ;
1403+ static readonly PayloadData s_setNamesUtf8Payload = QueryPayload . Create ( "SET NAMES utf8;" ) ;
1404+ static readonly PayloadData s_setNamesUtf8mb4Payload = QueryPayload . Create ( "SET NAMES utf8mb4;" ) ;
13971405
13981406 readonly object m_lock ;
13991407 readonly object [ ] m_logArguments ;
@@ -1412,6 +1420,7 @@ private enum State
14121420 bool m_supportsDeprecateEof ;
14131421 bool m_supportsSessionTrack ;
14141422 CharacterSet m_characterSet ;
1423+ PayloadData m_setNamesPayload ;
14151424 Dictionary < string , PreparedStatements > m_preparedStatements ;
14161425 }
14171426}
0 commit comments