I want to apply some sql-session level settings for certain processes in my c#
app.
For instance, I want to set DEADLOCK_PRIORITY
for some background processes to LOW
.
The questions are:
If I open a new sql connection, does that start a new sql-session?
Will the sql-session live until the connection is closed? If I apply my settings right after the SqlConnection
is opened, will they be valid for all queries executed in context of that same SqlConnection
?
What about connection pooling? Is this possible that my SET DEADLOCK_PRIORITY LOW
setting will be reused by other processes in my system (which I don't want to) because the SqlConnection
is not actually closed ( asp.net connection pooling decides to reuse it).
Thank you!
If you want to access a database multiple times, you should establish a connection using the Connection object. You can also make a connection to a database by passing a connection string via a Command or Recordset object. However, this type of connection is only good for one specific, single query.
Connection is the relationship between a client and a SQL Server database. Session is the period of time between a client logging in (connecting to) a SQL Server database and the client logging out (exiting) the SQL Server database.
A connection pool is created for each unique connection string. When a pool is created, multiple connection objects are created and added to the pool so that the minimum pool size requirement is satisfied. Connections are added to the pool as needed, up to the maximum pool size specified (100 is the default).
Connection pools generally contains maximum connection configuration parameter, which specifies maximum connnections setup with db. As soon as connection is freed, it's returned to pool.
ADO.NET executes sp_reset_connection
when you take a SqlConnection
from the pool (after having closed it so that it gets returned to the pool). According to What does "exec sp_reset_connection" mean in Sql Server Profiler? all SET
options are being reset. That would include DEADLOCK_PRIORITY
.
I would still suggest that you write a tiny test program to confirm this. ADO.NET session pooling is not perfect, for example it does not reset the ISOLATION LEVEL
and does not rollback transactions when closing.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With