Skip to content

Commit 45731e6

Browse files
init commit
1 parent 69b5378 commit 45731e6

File tree

2 files changed

+38
-36
lines changed

2 files changed

+38
-36
lines changed

src/Ydb.Sdk/src/Ado/YdbConnection.cs

Lines changed: 37 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -102,30 +102,37 @@ 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+
return;
113+
default:
114+
try
115+
{
116+
if (LastReader is { IsClosed: false })
117+
{
118+
await LastReader.CloseAsync();
119+
}
120+
121+
if (CurrentTransaction is { Completed: false })
122+
{
123+
await CurrentTransaction.RollbackAsync();
124+
}
125+
126+
OnStateChange(OpenToClosedEventArgs);
127+
128+
ConnectionState = ConnectionState.Closed;
129+
}
130+
finally
131+
{
132+
_session.Close();
133+
}
134+
135+
break;
129136
}
130137
}
131138

@@ -145,19 +152,14 @@ public override string ConnectionString
145152

146153
public override string Database => _connectionStringBuilder?.Database ?? string.Empty;
147154

148-
public override ConnectionState State => ConnectionState;
155+
public override ConnectionState State =>
156+
ConnectionState != ConnectionState.Closed && _session.IsBroken // maybe is updated asynchronously
157+
? ConnectionState.Broken
158+
: ConnectionState;
149159

150160
private ConnectionState ConnectionState { get; set; } = ConnectionState.Closed; // Invoke AsyncOpen()
151161

152-
internal void OnNotSuccessStatusCode(StatusCode code)
153-
{
154-
_session.OnNotSuccessStatusCode(code);
155-
156-
if (_session.IsBroken)
157-
{
158-
ConnectionState = ConnectionState.Broken;
159-
}
160-
}
162+
internal void OnNotSuccessStatusCode(StatusCode code) => _session.OnNotSuccessStatusCode(code);
161163

162164
internal YdbDataReader? LastReader { get; set; }
163165
internal string LastCommand { get; set; } = string.Empty;
@@ -203,15 +205,15 @@ public override Task<DataTable> GetSchemaAsync(
203205

204206
internal void ThrowIfConnectionClosed()
205207
{
206-
if (ConnectionState is ConnectionState.Closed or ConnectionState.Broken)
208+
if (State is ConnectionState.Closed or ConnectionState.Broken)
207209
{
208210
throw new InvalidOperationException("Connection is closed");
209211
}
210212
}
211213

212214
private void ThrowIfConnectionOpen()
213215
{
214-
if (ConnectionState == ConnectionState.Open)
216+
if (State == ConnectionState.Open)
215217
{
216218
throw new InvalidOperationException("Connection already open");
217219
}

src/Ydb.Sdk/src/IDriver.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ protected async ValueTask<CallOptions> GetCallOptions(GrpcRequestSettings settin
210210
}
211211

212212
public ILoggerFactory LoggerFactory { get; }
213-
public void RegisterOwner() => _ownerCount++;
213+
public void RegisterOwner() => Interlocked.Increment(ref _ownerCount);
214214
public bool IsDisposed => Disposed == 1;
215215

216216
public void Dispose() => DisposeAsync().AsTask().GetAwaiter().GetResult();

0 commit comments

Comments
 (0)