@@ -102,30 +102,38 @@ public override async Task OpenAsync(CancellationToken cancellationToken)
102
102
103
103
public override async Task CloseAsync ( )
104
104
{
105
- if ( State == ConnectionState . Closed )
105
+ // ReSharper disable once SwitchStatementHandlesSomeKnownEnumValuesWithDefault
106
+ switch ( State )
106
107
{
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 ;
129
137
}
130
138
}
131
139
@@ -145,19 +153,14 @@ public override string ConnectionString
145
153
146
154
public override string Database => _connectionStringBuilder ? . Database ?? string . Empty ;
147
155
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 ;
149
160
150
161
private ConnectionState ConnectionState { get ; set ; } = ConnectionState . Closed ; // Invoke AsyncOpen()
151
162
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 ) ;
161
164
162
165
internal YdbDataReader ? LastReader { get ; set ; }
163
166
internal string LastCommand { get ; set ; } = string . Empty ;
@@ -203,15 +206,15 @@ public override Task<DataTable> GetSchemaAsync(
203
206
204
207
internal void ThrowIfConnectionClosed ( )
205
208
{
206
- if ( ConnectionState is ConnectionState . Closed or ConnectionState . Broken )
209
+ if ( State is ConnectionState . Closed or ConnectionState . Broken )
207
210
{
208
211
throw new InvalidOperationException ( "Connection is closed" ) ;
209
212
}
210
213
}
211
214
212
215
private void ThrowIfConnectionOpen ( )
213
216
{
214
- if ( ConnectionState == ConnectionState . Open )
217
+ if ( State == ConnectionState . Open )
215
218
{
216
219
throw new InvalidOperationException ( "Connection already open" ) ;
217
220
}
0 commit comments