What is the difference between a connection and a session and how they are related?

up vote 49 down vote accepted

The connection is the physical communication channel between SQL Server and the application: the TCP socket, the named pipe, the shared memory region. The session in SQL Server corresponds to the Wikipedia definition of a session: a semi-permanent container of state for an information exchange. In other words the sessions stores settings like cache of your login information, current transaction isolation level, session level SET values etc etc.

Normally there is one session on each connection, but there could be multiple session on a single connection (Multiple Active Result Sets, MARS) and there are sessions that have no connection (SSB activated procedures, system sessions). There are also connections w/o sessions, namely connections used for non-TDS purposes, like database mirroring sys.dm_db_mirroring_connections or Service Broker connections sys.dm_broker_connections.

  • (mind-blowing noise) When you realize that SSMS is using MARS and therefore different sessions on each open file tab and that's why #temp tables can't be dropped or selected from in different open file tabs because they are session scoped. – GibralterTop Feb 15 '17 at 17:02

You can query the sys.dm_exec_sessions dynamic management views to learn information about successful and unsuccessful logins when common criteria compliance is enabled on a SQL Server 2012 instance.

The sys.dm_exec_connections dynamic management view provides information about connections established to the Database Engine instance. You can't query this dynamic management view for information about successful and unsuccessful logins when common criteria compliance is enabled on a SQL Server 2012 instance.

  • Connection represents connection to the server over a network or locally through shared memory .

  • A session represents a user process within SQL Server.

  • A connection may be linked with zero or more then one session.

Your Answer

 
discard

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.