Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SqlConnection vs Sql Session. Do their lifetimes coincide?

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:

  1. If I open a new sql connection, does that start a new sql-session?

  2. 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?

  3. 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!

like image 612
Artur Udod Avatar asked May 17 '13 15:05

Artur Udod


People also ask

When should you use the SqlConnection object?

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.

What is the difference between the connections and sessions to the database?

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.

How does SQL connection pool work?

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).

What happens when connection pool is full?

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.


1 Answers

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.

like image 148
usr Avatar answered Sep 21 '22 09:09

usr