Skip to content

Commit 1c24adb

Browse files
authored
Merge pull request #83 from DataObjects-NET/async-singleconnection-close
Implements async SingleConnection closing on Domain.DisposeAsync()
2 parents d5111a9 + a02bd33 commit 1c24adb

File tree

1 file changed

+31
-23
lines changed

1 file changed

+31
-23
lines changed

Orm/Xtensive.Orm/Orm/Domain.cs

Lines changed: 31 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -436,41 +436,49 @@ internal Domain(DomainConfiguration configuration, object upgradeContextCookie,
436436

437437
public ValueTask DisposeAsync() => InnerDispose(true);
438438

439-
public ValueTask InnerDispose(bool isAsync)
439+
public async ValueTask InnerDispose(bool isAsync)
440440
{
441441
lock (disposeGuard) {
442442
if (isDisposed) {
443-
return default;
443+
return;
444444
}
445445

446446
isDisposed = true;
447+
}
447448

448-
if (isDebugEventLoggingEnabled) {
449-
OrmLog.Debug(Strings.LogDomainIsDisposing);
450-
}
449+
if (isDebugEventLoggingEnabled) {
450+
OrmLog.Debug(Strings.LogDomainIsDisposing);
451+
}
451452

452-
NotifyDisposing();
453-
Services.Dispose();
453+
NotifyDisposing();
454+
Services.Dispose();
454455

455-
if (SingleConnection==null) {
456-
return default;
457-
}
456+
if (SingleConnection == null) {
457+
return;
458+
}
458459

459-
// TODO: implement async dispose of the SingleConnection
460-
lock (singleConnectionGuard) {
461-
if (singleConnectionOwner==null) {
462-
var driver = Handlers.StorageDriver;
463-
driver.CloseConnection(null, SingleConnection);
464-
driver.DisposeConnection(null, SingleConnection);
465-
}
466-
else {
467-
OrmLog.Warning(
468-
Strings.LogUnableToCloseSingleAvailableConnectionItIsStillUsedBySessionX,
469-
singleConnectionOwner);
470-
}
460+
SqlConnection singleConnectionLocal;
461+
lock (singleConnectionGuard) {
462+
if (singleConnectionOwner != null) {
463+
OrmLog.Warning(
464+
Strings.LogUnableToCloseSingleAvailableConnectionItIsStillUsedBySessionX,
465+
singleConnectionOwner);
466+
return;
467+
}
468+
else {
469+
singleConnectionLocal = SingleConnection;
470+
SingleConnection = null;
471471
}
472+
}
472473

473-
return default;
474+
var driver = Handlers.StorageDriver;
475+
if (isAsync) {
476+
await driver.CloseConnectionAsync(null, singleConnectionLocal).ConfigureAwait(false);
477+
await driver.DisposeConnectionAsync(null, singleConnectionLocal).ConfigureAwait(false);
478+
}
479+
else {
480+
driver.CloseConnection(null, singleConnectionLocal);
481+
driver.DisposeConnection(null, singleConnectionLocal);
474482
}
475483
}
476484
}

0 commit comments

Comments
 (0)