Click here to Skip to main content
15,912,021 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
What is difference between Session and Connection? Could any one explain it in simple way. I read many articles but I didn't get it. The more I read, the more I am getting confused.

Thanks in advance,
Pushkar
Posted

1 solution

Let's take a step back, and ignore computers for a moment.

You want to drive to the shops.
To do this, you need to do two things:
1) Get into the car.
2) Use your driving skills to navigate your way.
These are separate "events" - there is no point in trying to navigate unless you are in the car! "Getting into the car" establishes a connection between you are the car, and while that connection exists, you can have a session of navigation. If at any point you get out of the car, you have broken the connection, and the navigation session ends.

From an SQL perspective, it's the same, your application establishes a connection to SQL Server - the "physical" link between them - and SQL Server establishes a Session within which you can issue commands over the connection. If at any time the connection is closed, your session ends - and if you are using Transactions for example, they will automatically be "rolled back" at that point because you didn't Commit them.

In reality, it's more complex than that - you can have more than one session per connection (but not the other way round, two connections cannot "share" a session) under some circumstances, and there are some conditions under which you can have a session without a connection.

But in practice (and for 99.99% of your working life) you can treat the two as the same: a connection establishes a session and the session ends when the connection is terminated.
 
Share this answer
 
Comments
Pushkar Prabhu 31-May-15 6:11am    
1.I can have more than one session in single connection but one session cannot have multiple connections right?
2.Can one session have multiple transactions?
OriginalGriff 31-May-15 6:33am    
1. Yes - under some circumstances - but it's unusual.
https://msdn.microsoft.com/en-us/library/ms131686.aspx
2. Yes - you can "nest" transactions so you can Rollback or Commit as you go, without forcing a Rollback or Commit on all transactions. Rolling back an "Outer" transaction will Rollback even Committed "Inner" transactions:
https://technet.microsoft.com/en-us/library/ms189336(v=sql.105).aspx

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900