@@ -102,30 +102,38 @@ public override async Task OpenAsync(CancellationToken cancellationToken)
102102
103103 public override async Task CloseAsync ( )
104104 {
105- if ( State == ConnectionState . Closed )
105+ // ReSharper disable once SwitchStatementHandlesSomeKnownEnumValuesWithDefault
106+ switch ( State )
106107 {
107- return ;
108- }
109-
110- try
111- {
112- if ( LastReader is { IsClosed : false } )
113- {
114- await LastReader . CloseAsync ( ) ;
115- }
116-
117- if ( CurrentTransaction is { Completed : false } )
118- {
119- await CurrentTransaction . RollbackAsync ( ) ;
120- }
121-
122- OnStateChange ( OpenToClosedEventArgs ) ;
123-
124- ConnectionState = ConnectionState . Closed ;
125- }
126- finally
127- {
128- _session . Close ( ) ;
108+ case ConnectionState . Closed :
109+ return ;
110+ case ConnectionState . Broken :
111+ ConnectionState = ConnectionState . Closed ;
112+ _session . Close ( ) ;
113+ return ;
114+ default :
115+ try
116+ {
117+ if ( LastReader is { IsClosed : false } )
118+ {
119+ await LastReader . CloseAsync ( ) ;
120+ }
121+
122+ if ( CurrentTransaction is { Completed : false } )
123+ {
124+ await CurrentTransaction . RollbackAsync ( ) ;
125+ }
126+
127+ OnStateChange ( OpenToClosedEventArgs ) ;
128+
129+ ConnectionState = ConnectionState . Closed ;
130+ }
131+ finally
132+ {
133+ _session . Close ( ) ;
134+ }
135+
136+ break ;
129137 }
130138 }
131139
@@ -145,19 +153,14 @@ public override string ConnectionString
145153
146154 public override string Database => _connectionStringBuilder ? . Database ?? string . Empty ;
147155
148- public override ConnectionState State => ConnectionState ;
156+ public override ConnectionState State =>
157+ ConnectionState != ConnectionState . Closed && _session . IsBroken // maybe is updated asynchronously
158+ ? ConnectionState . Broken
159+ : ConnectionState ;
149160
150161 private ConnectionState ConnectionState { get ; set ; } = ConnectionState . Closed ; // Invoke AsyncOpen()
151162
152- internal void OnNotSuccessStatusCode ( StatusCode code )
153- {
154- _session . OnNotSuccessStatusCode ( code ) ;
155-
156- if ( _session . IsBroken )
157- {
158- ConnectionState = ConnectionState . Broken ;
159- }
160- }
163+ internal void OnNotSuccessStatusCode ( StatusCode code ) => _session . OnNotSuccessStatusCode ( code ) ;
161164
162165 internal YdbDataReader ? LastReader { get ; set ; }
163166 internal string LastCommand { get ; set ; } = string . Empty ;
@@ -203,15 +206,15 @@ public override Task<DataTable> GetSchemaAsync(
203206
204207 internal void ThrowIfConnectionClosed ( )
205208 {
206- if ( ConnectionState is ConnectionState . Closed or ConnectionState . Broken )
209+ if ( State is ConnectionState . Closed or ConnectionState . Broken )
207210 {
208211 throw new InvalidOperationException ( "Connection is closed" ) ;
209212 }
210213 }
211214
212215 private void ThrowIfConnectionOpen ( )
213216 {
214- if ( ConnectionState == ConnectionState . Open )
217+ if ( State == ConnectionState . Open )
215218 {
216219 throw new InvalidOperationException ( "Connection already open" ) ;
217220 }
0 commit comments