extend session events to cover raw connections and commands - #498
Open
SergeiPavlov wants to merge 5 commits into
Open
extend session events to cover raw connections and commands#498SergeiPavlov wants to merge 5 commits into
SergeiPavlov wants to merge 5 commits into
Conversation
The ORM's own DbCommandExecuting/Executed events only fire for commands run through its own execution path, and there was no hook for a connection's first genuine open or for a raw connection handed out without a DbCommand. Code using DirectSqlAccessor, or needing the raw connection directly, had no way to observe any of this. Adds a DbConnectionOpened event fired on the Closed to Open transition, an EventNotifyingDbCommand wrapper so DirectSqlAccessor.CreateCommand() raises the same execute events ORM commands do, and an async GetConnectionAsync with an awaitable raw-connection-accessed hook so callers that need the connection itself can hook in without blocking a thread.
TagOpenConnection/TagOpenConnectionAsync create their own command on the raw connection, but if that connection has a pending local transaction, SqlClient requires the command's Transaction be set or it throws instead of tagging. GetConnectionAsync's caller has no way to supply it. Adds Transaction to DbConnectionEventArgs and threads DirectSqlAccessor's already-tracked active transaction through both raw-connection-accessed hooks, so ITenantConnectionTagger consumers can set it on their own command.
botinko
approved these changes
Aug 6, 2026
Collaborator
|
previously approved on #493, running some manual tests before merge |
Raises a session event before a connection's init SQL runs so a subscriber can append to that batch (e.g. USE [db]; SET CONTEXT_INFO ...) instead of paying a separate round-trip. Appending here takes effect before the first command runs on the connection. Fires on the same closed-to-open transition as DbConnectionOpened, before the open, on both sync and async paths.
dagreen-st
approved these changes
Aug 7, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Recreating of #493
@dagreen-st Please dont merge it until testing with the Monolith
Summary
DbConnectionOpenedevent onSessionEventAccessor, fired once per genuine Closed→Open connection transitionEventNotifyingDbCommandwrapper soDirectSqlAccessor.CreateCommand()raises the sameDbCommandExecuting/DbCommandExecutedevents the ORM's own commands doGetConnectionAsynconIDirectSqlService/DirectSqlAccessor, backed by the existing async prepare path, plus an awaitable raw-connection-accessed hookRelated:
Test plan
dotnet buildonXtensive.OrmandXtensive.Orm.TestsDbConnectionOpenedEventTest,EventNotifyingDbCommandTest,DirectSqlGetConnectionAsyncTest) pass against a local SQL Server containerXtensive.Orm.Tests.Storagesuite run before/after: identical pre-existing failures, no regressionsReapplies #493, which was reverted after merge (commits 8e9144d and 1f127aa).
Discussion: https://servicetitan.enterprise.slack.com/archives/C0BP3S50BGQ and on the original PR #493