@@ -44,20 +44,22 @@ public ServerSession(ILogger logger, ConnectionPool? pool, int poolGeneration, i
44
44
m_activityTags = [ ] ;
45
45
DataReader = new ( ) ;
46
46
Log . CreatedNewSession ( m_logger , Id ) ;
47
- Context = new Context ( 0 , null , 0 ) ;
47
+ Context = new Context ( default ) ;
48
48
}
49
49
50
50
public string Id { get ; }
51
51
public ServerVersion ServerVersion { get ; set ; }
52
52
public bool SupportsPerQueryVariables => ServerVersion . IsMariaDb && ServerVersion . Version >= ServerVersions . MariaDbSupportsPerQueryVariables ;
53
53
public int ActiveCommandId { get ; private set ; }
54
54
public int CancellationTimeout { get ; private set ; }
55
+ public int ConnectionId { get ; set ; }
55
56
public byte [ ] ? AuthPluginData { get ; set ; }
56
57
public long CreatedTimestamp { get ; }
57
58
public ConnectionPool ? Pool { get ; }
58
59
public int PoolGeneration { get ; }
59
60
public long LastLeasedTimestamp { get ; set ; }
60
61
public long LastReturnedTimestamp { get ; private set ; }
62
+ public string ? DatabaseOverride { get ; set ; }
61
63
62
64
public string HostName { get ; private set ; }
63
65
public IPEndPoint ? IPEndPoint => m_tcpClient ? . Client . RemoteEndPoint as IPEndPoint ;
@@ -338,8 +340,8 @@ public void FinishQuerying()
338
340
var activity = ActivitySourceHelper . StartActivity ( name , m_activityTags ) ;
339
341
if ( activity is { IsAllDataRequested : true } )
340
342
{
341
- if ( ! Context . IsInitialDatabase ( ) )
342
- activity . SetTag ( ActivitySourceHelper . DatabaseNameTagName , Context . Database ) ;
343
+ if ( DatabaseOverride is not null )
344
+ activity . SetTag ( ActivitySourceHelper . DatabaseNameTagName , DatabaseOverride ) ;
343
345
if ( tagName1 is not null )
344
346
activity . SetTag ( tagName1 , tagValue1 ) ;
345
347
}
@@ -452,15 +454,16 @@ public async Task DisposeAsync(IOBehavior ioBehavior, CancellationToken cancella
452
454
}
453
455
454
456
ServerVersion = new ( initialHandshake . ServerVersion ) ;
455
- Context = new Context ( initialHandshake . ProtocolCapabilities , cs . Database , initialHandshake . ConnectionId ) ;
457
+ ConnectionId = initialHandshake . ConnectionId ;
458
+ Context = new Context ( initialHandshake . ProtocolCapabilities ) ;
456
459
AuthPluginData = initialHandshake . AuthPluginData ;
457
460
m_useCompression = cs . UseCompression && ( initialHandshake . ProtocolCapabilities & ProtocolCapabilities . Compress ) != 0 ;
458
461
CancellationTimeout = cs . CancellationTimeout ;
459
462
UserID = cs . UserID ;
460
463
461
464
// set activity tags
462
465
{
463
- var connectionId = Context . ConnectionId . ToString ( CultureInfo . InvariantCulture ) ;
466
+ var connectionId = ConnectionId . ToString ( CultureInfo . InvariantCulture ) ;
464
467
m_activityTags [ ActivitySourceHelper . DatabaseConnectionIdTagName ] = connectionId ;
465
468
if ( activity is { IsAllDataRequested : true } )
466
469
activity . SetTag ( ActivitySourceHelper . DatabaseConnectionIdTagName , connectionId ) ;
@@ -499,7 +502,7 @@ public async Task DisposeAsync(IOBehavior ioBehavior, CancellationToken cancella
499
502
}
500
503
}
501
504
502
- Log . SessionMadeConnection ( m_logger , Id , ServerVersion . OriginalString , Context . ConnectionId , m_useCompression , m_supportsConnectionAttributes , Context . SupportsDeprecateEof , Context . SupportsCachedPreparedMetadata , serverSupportsSsl , Context . SupportsSessionTrack , m_supportsPipelining , Context . SupportsQueryAttributes ) ;
505
+ Log . SessionMadeConnection ( m_logger , Id , ServerVersion . OriginalString , ConnectionId , m_useCompression , m_supportsConnectionAttributes , Context . SupportsDeprecateEof , Context . SupportsCachedPreparedMetadata , serverSupportsSsl , Context . SupportsSessionTrack , m_supportsPipelining , Context . SupportsQueryAttributes ) ;
503
506
504
507
if ( cs . SslMode != MySqlSslMode . None && ( cs . SslMode != MySqlSslMode . Preferred || serverSupportsSsl ) )
505
508
{
@@ -532,18 +535,23 @@ public async Task DisposeAsync(IOBehavior ioBehavior, CancellationToken cancella
532
535
if ( m_useCompression )
533
536
m_payloadHandler = new CompressedPayloadHandler ( m_payloadHandler . ByteHandler ) ;
534
537
535
- // set 'collation_connection' to the server default
536
- if ( Context . ClientCharset == null || ServerVersion . Version >= ServerVersions . SupportsUtf8Mb4
537
- ? ! string . Equals ( Context . ClientCharset , "utf8mb4" , StringComparison . Ordinal )
538
- : ! string . Equals ( Context . ClientCharset , "utf8" , StringComparison . Ordinal ) )
538
+ if ( ok . ClientCharacterSet != ( ServerVersion . Version >= ServerVersions . SupportsUtf8Mb4 ? "utf8mb4" : "utf8" ) )
539
539
{
540
+ // set 'collation_connection' to the server default
540
541
await SendAsync ( m_setNamesPayload , ioBehavior , cancellationToken ) . ConfigureAwait ( false ) ;
541
542
payload = await ReceiveReplyAsync ( ioBehavior , cancellationToken ) . ConfigureAwait ( false ) ;
542
543
OkPayload . Verify ( payload . Span , Context ) ;
543
544
}
544
545
545
546
if ( ShouldGetRealServerDetails ( cs ) )
547
+ {
546
548
await GetRealServerDetailsAsync ( ioBehavior , CancellationToken . None ) . ConfigureAwait ( false ) ;
549
+ }
550
+ else if ( ok . ConnectionId is int newConnectionId && newConnectionId != ConnectionId )
551
+ {
552
+ Log . ChangingConnectionId ( m_logger , Id , ConnectionId , newConnectionId , ServerVersion . OriginalString , ServerVersion . OriginalString ) ;
553
+ ConnectionId = newConnectionId ;
554
+ }
547
555
548
556
m_payloadHandler . ByteHandler . RemainingTimeout = Constants . InfiniteTimeout ;
549
557
return statusInfo ;
@@ -570,9 +578,9 @@ public async Task<bool> TryResetConnectionAsync(ConnectionSettings cs, MySqlConn
570
578
ClearPreparedStatements ( ) ;
571
579
572
580
PayloadData payload ;
573
- if ( Context . IsInitialDatabase ( ) &&
574
- ( ( ! ServerVersion . IsMariaDb && ServerVersion . Version . CompareTo ( ServerVersions . SupportsResetConnection ) >= 0 ) ||
575
- ( ServerVersion . IsMariaDb && ServerVersion . Version . CompareTo ( ServerVersions . MariaDbSupportsResetConnection ) >= 0 ) ) )
581
+ if ( DatabaseOverride is null &&
582
+ ( ( ! ServerVersion . IsMariaDb && ServerVersion . Version . CompareTo ( ServerVersions . SupportsResetConnection ) >= 0 ) ||
583
+ ( ServerVersion . IsMariaDb && ServerVersion . Version . CompareTo ( ServerVersions . MariaDbSupportsResetConnection ) >= 0 ) ) )
576
584
{
577
585
if ( m_supportsPipelining )
578
586
{
@@ -599,14 +607,14 @@ public async Task<bool> TryResetConnectionAsync(ConnectionSettings cs, MySqlConn
599
607
else
600
608
{
601
609
// optimistically hash the password with the challenge from the initial handshake (supported by MariaDB; doesn't appear to be supported by MySQL)
602
- if ( Context . IsInitialDatabase ( ) )
610
+ if ( DatabaseOverride is null )
603
611
{
604
612
Log . SendingChangeUserRequest ( m_logger , Id , ServerVersion . OriginalString ) ;
605
613
}
606
614
else
607
615
{
608
- Log . SendingChangeUserRequestDueToChangedDatabase ( m_logger , Id , Context . Database ! ) ;
609
- Context . Database = cs . Database ;
616
+ Log . SendingChangeUserRequestDueToChangedDatabase ( m_logger , Id , DatabaseOverride ) ;
617
+ DatabaseOverride = null ;
610
618
}
611
619
var password = GetPassword ( cs , connection ) ;
612
620
var hashedPassword = AuthenticationUtility . CreateAuthenticationResponse ( AuthPluginData ! , password ) ;
@@ -1668,8 +1676,8 @@ static void ReadRow(ReadOnlySpan<byte> span, out int? connectionId, out ServerVe
1668
1676
1669
1677
if ( connectionId is int newConnectionId && serverVersion is not null )
1670
1678
{
1671
- Log . ChangingConnectionId ( m_logger , Id , Context . ConnectionId , newConnectionId , ServerVersion . OriginalString , serverVersion . OriginalString ) ;
1672
- Context . ConnectionId = newConnectionId ;
1679
+ Log . ChangingConnectionId ( m_logger , Id , ConnectionId , newConnectionId , ServerVersion . OriginalString , serverVersion . OriginalString ) ;
1680
+ ConnectionId = newConnectionId ;
1673
1681
ServerVersion = serverVersion ;
1674
1682
}
1675
1683
}
0 commit comments