Scenario C in This Microsoft Doc describes how temp tables scoped to a connection can be replaced with Memory-Optimized Tables. The scheme uses a Filter Security Policy which calls a function to determine if @@spid matches the SpidFilter column in the Memory-Optimized table.
Will this work with .NET connection pooling? I would expect @@spid will return the same number as a connection is re-used over and over again. .NET clears the session scoped temp tables by calling sp_reset_connection, but that will not clear Memory-Optimized tables, or change @@spid. Maybe sys.dm_exec_sessions's session_id could be added to make it work in a connection pooling environment?
@@spidyou could utilize context info or session context function Of course during each call you have to set it to correct value before doing your query and after reset to default, but it should handle pooling.CREATE TABLE #tempSessionCstatements in your code withDELETE FROM dbo.soSessionC, to ensure a session is not exposed to table contents inserted by a previous session with the same session_id."