@@ -25,21 +25,16 @@ namespace MySqlConnector.Core;
25
25
26
26
internal sealed partial class ServerSession : IServerCapabilities
27
27
{
28
- public ServerSession ( ILogger logger )
29
- : this ( logger , null , 0 , Interlocked . Increment ( ref s_lastId ) )
30
- {
31
- }
32
-
33
- public ServerSession ( ILogger logger , ConnectionPool ? pool , int poolGeneration , int id )
28
+ public ServerSession ( ILogger logger , IConnectionPoolMetadata pool )
34
29
{
35
30
m_logger = logger ;
36
31
m_lock = new ( ) ;
37
32
m_payloadCache = new ( ) ;
38
- Id = ( pool ? . Id ?? 0 ) + "." + id ;
33
+ Id = pool . Id + "." + pool . GetNewSessionId ( ) ;
39
34
ServerVersion = ServerVersion . Empty ;
40
35
CreatedTimestamp = Stopwatch . GetTimestamp ( ) ;
41
- Pool = pool ;
42
- PoolGeneration = poolGeneration ;
36
+ Pool = pool . ConnectionPool ;
37
+ PoolGeneration = pool . Generation ;
43
38
HostName = "" ;
44
39
m_activityTags = [ ] ;
45
40
DataReader = new ( ) ;
@@ -573,10 +568,11 @@ public async Task DisposeAsync(IOBehavior ioBehavior, CancellationToken cancella
573
568
}
574
569
}
575
570
576
- public static async ValueTask < ServerSession > ConnectAndRedirectAsync ( Func < ServerSession > createSession , ILogger logger , int ? poolId , ConnectionSettings cs , ILoadBalancer ? loadBalancer , MySqlConnection connection , Action < ILogger , int , string , Exception ? > ? logMessage , long startingTimestamp , Activity ? activity , IOBehavior ioBehavior , CancellationToken cancellationToken )
571
+ public static async ValueTask < ServerSession > ConnectAndRedirectAsync ( ILogger connectionLogger , ILogger poolLogger , IConnectionPoolMetadata pool , ConnectionSettings cs , ILoadBalancer ? loadBalancer , MySqlConnection connection , Action < ILogger , int , string , Exception ? > ? logMessage , long startingTimestamp , Activity ? activity , IOBehavior ioBehavior , CancellationToken cancellationToken )
577
572
{
578
- var session = createSession ( ) ;
579
- if ( poolId is not null && logger . IsEnabled ( LogLevel . Debug ) ) logMessage ! ( logger , poolId . Value , session . Id , null ) ;
573
+ var session = new ServerSession ( connectionLogger , pool ) ;
574
+ if ( logMessage is not null && poolLogger . IsEnabled ( LogLevel . Debug ) )
575
+ logMessage ( poolLogger , pool . Id , session . Id , null ) ;
580
576
581
577
string ? redirectionUrl ;
582
578
try
@@ -592,10 +588,10 @@ public static async ValueTask<ServerSession> ConnectAndRedirectAsync(Func<Server
592
588
Exception ? redirectionException = null ;
593
589
if ( redirectionUrl is not null )
594
590
{
595
- Log . HasServerRedirectionHeader ( logger , session . Id , redirectionUrl ) ;
591
+ Log . HasServerRedirectionHeader ( poolLogger , session . Id , redirectionUrl ) ;
596
592
if ( cs . ServerRedirectionMode == MySqlServerRedirectionMode . Disabled )
597
593
{
598
- Log . ServerRedirectionIsDisabled ( logger , session . Id ) ;
594
+ Log . ServerRedirectionIsDisabled ( poolLogger , session . Id ) ;
599
595
return session ;
600
596
}
601
597
@@ -604,19 +600,19 @@ public static async ValueTask<ServerSession> ConnectAndRedirectAsync(Func<Server
604
600
if ( host != cs . HostNames ! [ 0 ] || port != cs . Port || user != cs . UserID )
605
601
{
606
602
var redirectedSettings = cs . CloneWith ( host , port , user ) ;
607
- Log . OpeningNewConnection ( logger , host , port , user ) ;
608
- var redirectedSession = createSession ( ) ;
603
+ Log . OpeningNewConnection ( poolLogger , host , port , user ) ;
604
+ var redirectedSession = new ServerSession ( connectionLogger , pool ) ;
609
605
try
610
606
{
611
607
await redirectedSession . ConnectAsync ( redirectedSettings , connection , startingTimestamp , loadBalancer , activity , ioBehavior , cancellationToken ) . ConfigureAwait ( false ) ;
612
- Log . ClosingSessionToUseRedirectedSession ( logger , session . Id , redirectedSession . Id ) ;
608
+ Log . ClosingSessionToUseRedirectedSession ( poolLogger , session . Id , redirectedSession . Id ) ;
613
609
await session . DisposeAsync ( ioBehavior , cancellationToken ) . ConfigureAwait ( false ) ;
614
610
return redirectedSession ;
615
611
}
616
612
catch ( Exception ex )
617
613
{
618
614
redirectionException = ex ;
619
- Log . FailedToConnectRedirectedSession ( logger , ex , redirectedSession . Id ) ;
615
+ Log . FailedToConnectRedirectedSession ( poolLogger , ex , redirectedSession . Id ) ;
620
616
try
621
617
{
622
618
await redirectedSession . DisposeAsync ( ioBehavior , cancellationToken ) . ConfigureAwait ( false ) ;
@@ -628,14 +624,14 @@ public static async ValueTask<ServerSession> ConnectAndRedirectAsync(Func<Server
628
624
}
629
625
else
630
626
{
631
- Log . SessionAlreadyConnectedToServer ( logger , session . Id ) ;
627
+ Log . SessionAlreadyConnectedToServer ( poolLogger , session . Id ) ;
632
628
}
633
629
}
634
630
}
635
631
636
632
if ( cs . ServerRedirectionMode == MySqlServerRedirectionMode . Required )
637
633
{
638
- Log . RequiresServerRedirection ( logger , session . Id ) ;
634
+ Log . RequiresServerRedirection ( poolLogger , session . Id ) ;
639
635
throw new MySqlException ( MySqlErrorCode . UnableToConnectToHost , "Server does not support redirection" , redirectionException ) ;
640
636
}
641
637
return session ;
@@ -1991,7 +1987,6 @@ protected override void OnStatementBegin(int index)
1991
1987
private static readonly PayloadData s_sleepWithAttributesPayload = QueryPayload . Create ( true , "SELECT SLEEP(0) INTO @\uE001 MySqlConnector\uE001 Sleep;"u8 ) ;
1992
1988
private static readonly PayloadData s_selectConnectionIdVersionNoAttributesPayload = QueryPayload . Create ( false , "SELECT CONNECTION_ID(), VERSION();"u8 ) ;
1993
1989
private static readonly PayloadData s_selectConnectionIdVersionWithAttributesPayload = QueryPayload . Create ( true , "SELECT CONNECTION_ID(), VERSION();"u8 ) ;
1994
- private static int s_lastId ;
1995
1990
1996
1991
private readonly ILogger m_logger ;
1997
1992
private readonly object m_lock ;
0 commit comments