Session, Transaction, DbContext, Connection理解

  • 1633
  • 0
  • SQL
  • 2019-03-12

Session, Transaction, DbContext, Connection理解

Session, Transaction, DbContext, Connection理解
(仍有待整理)

Transaction:
SELECT CURRENT_TRANSACTION_ID();
-Applies to SQL Server (starting with 2016) and Azure SQL Database
-Return transaction ID of the current transaction in the current session.
-Return bigint type value
參考:
CURRENT_TRANSACTION_ID (Transact-SQL)
https://docs.microsoft.com/en-us/sql/t-sql/functions/current-transaction-id-transact-sql?view=sql-server-2017

DbContext
-Entity Framework 是 .NET 跟資料庫溝通好用的 Object-Relational Mapper (O/RM) 框架
-DbContext 是 EF Core 跟資料庫溝通的主要類別
參考:
[Day24] ASP.NET Core 2 系列 - Entity Framework Core
https://ithelp.ithome.com.tw/articles/10196856

Connection:
-Physical communication channel between SQL Server and the application (the TCP socket, the named pipe, the shared memory region)

Session:
-A semi-permanent container of state for an information exchange
-Store setting (like cache of your login information, current transaction isolation level, session level SET values etc etc.)
-View in sys.dm_exec_sessions
-There may have many transaction in a session
EDIT:
-Context (DbContext) represents a session with the underlying database in Entity Framework
參考:
What is the difference between a session and a transaction in JPA 2.0?
https://stackoverflow.com/questions/5409085/what-is-the-difference-between-a-session-and-a-transaction-in-jpa-2-0
sys.dm_exec_sessions (Transact-SQL)
https://docs.microsoft.com/en-us/sql/relational-databases/system-dynamic-management-views/sys-dm-exec-sessions-transact-sql?view=sql-server-2017
Context Class in Entity Framework
http://www.entityframeworktutorial.net/basics/context-class-in-entity-framework.aspx

Combination of connection and session
Normally One session on each connection
Multiple Active Result Sets, MARS Multiple session on one single connection
Connection used for non-TDS purposes Connection without sessions
SSB activated procedures, system sessions Sessions with no connection


參考:
What is the difference between a connection and a session?
https://dba.stackexchange.com/questions/13698/what-is-the-difference-between-a-connection-and-a-session/13700#13700
What is the differencs between sessions and connections in SQLServer?
https://social.msdn.microsoft.com/Forums/sqlserver/en-US/3975aea5-bc92-424a-a1c0-75c6e5e2f8a6/what-is-the-differencs-between-sessions-and-connections-in-sqlserver?forum=sqldataaccess
Difference between Session and Connection in SQL Server
https://stackoverflow.com/questions/39199173/difference-between-session-and-connection-in-sql-server