Difference between Session and Connection in SQL Server - sql

In case of temporary tables, we see that they are connection dependent. I mean that tables created in one connection is only available to that connection and automatically dropped when the connection is lost or destroyed.
What is the difference between connection and session in SQL Server?

Literally : Connection is Physical Communication Channel and Session is a state of information exchange. A Connection may have multiple sessions.
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.
I got the reference from here

common concepts that get used with SQL Server thread management and scheduling :
Sessions – when the client application connects to SQL Server the two sides establish a “session” on which to exchange information. Strictly speaking a session is not the same as the underlying physical connection, it is a SQL Server logical representation of a connection. But for practical purposes, you can think of this as being a connection (session =~ connection). See sys.dm_exec_sessions. This is the old SPID that existed in SQL Server 2000 and earlier. You may sometimes notice a single session repeating multiple times in a DMV output. This happens because of parallel queries. A parallel query uses the same session to communicate with the client, but on the SQL Server side multiple worker (threads) are assigned to service this request. So if you see multiple rows with the same session ID, know that the query request is being serviced by multiple threads.
-
Connections – this is the actual physical connection established at the lower protocol level with all of its characteristics sys.dm_exec_connections . There is a 1:1 mapping between a Session and a Connection.

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.

Related

Same database - different output from different clients

I have a docker container with Oracle DB which I try to access from different clients with same credentials and its behaviour is quite strange.
I have set up connection in IDEA with following JDBC URL: jdbc:oracle:thin:#//localhost:1526/XEPDB1
Also I work with it via Spring Data framework in another container, url is jdbc:oracle:thin:#oracle:1521/XEPDB1 (port is forwarded to outer world as 1526, container name is oracle)
And finally, I tried to access the db using sqlplus from within db host: sqlplus LOGIN/PASSWORD#localhost:1521/XEPDB1
All connections are successful, but simple inserts and selects for same table show that each client sees only it's own modifications. What is wrong? How do I share same table between clients?
According to the connection strings, all the users are connected to the same database, so that isn't the issue.
The issue is probably that each user that modifies the data (e.g., inserts new rows), does so without committing, and thus this data is only available in their current session. Once they commit the data, it will be available to all the other sessions too.

SQL connection vb.net – one sql connection object for the application.?

SQL connection – I planned to create one sql connection object inside my own singleton class (say connectionmanager with Reference counting for ‘SQL connection object’ which is a member of the class) and open/close the connection whenever needed. As I like to give high importance for performance. And my application is a desktop based application accessing a remote database server (SQL server 2008), and will use only one connection string, 50 concurrent desktop users may access db server. Please advice.
As like many articles say if the answer is “Connection pooling is taken care by ado.net” – Does that mean that scope of connection pool is entire life of Application instance? Or is it in the scope of SQLConnection object?
Connection pooling is taken care by ado.net” – Does that mean that scope of connection pool is entire life of Application instance?
Yes. Read MSDN article - SQL Server Connection Pooling (ADO.NET)
Connections are pooled per process, per application domain, per
connection string and when integrated security is used, per Windows
identity. Connection strings must also be an exact match; keywords
supplied in a different order for the same connection will be pooled
separately.
The scope of the connection string. Change the string, a new connection pool is created.

How to limit the number of connections to a SQL Server server from my tomcat deployed java application?

I have an application that is deployed on tomcat on server A and sends queries to a huge variety of SQL Server databases on an server B.
I am concerned that my application could overload this SQL Server database server and would like some way to preventing it making requests to connect to any database on that server if some arbitrary number of connections were already in existence and unclosed.
I am looking at using connection pooling but am under the impression that this will only pool connections to a specific database on the SQL Server server, I want to control the total of these combined connections that will occur to many different databases (incidentally I can only find out the names of individual db's dynamically as they change day to day). Will connection pooling take care of this for me, are am I looking at this from the wrong perspective?
I have no access to the configuration of the SQL Server server.
Links to tutorials or working examples of your suggested solution are most welcome!
Have you considered using DBCP? See here for more info
You're correct. A database pool will limit connections to either the database, or to all databases, depending on how the pool is configured.
Here are several open source packages that implement database connection pools. One of them probably does what you want.
connection pooling ... this will only pool connections to a specific database on the mssql server
AFAIK, this is correct. Your connection pool will only limit the number of connections to the specific database it is defined for.
want to control the total of these combined connections that will occur to many different databases
I dont think you can control the number of connections to all databases from the pool. You've written you don't have access to change things on the MSSQL server. so creating SYNONYMs to the various databases on MSSQL itself is not an option.
You can write your own class within the application, say a ConnPoolManager, which has an internal counter prior to getting and releasing Connections from any of the pools.
This class should cache all the JNDI lookups to each pool.
For the app to get a connection to ANY pool, it goes through the ConnPoolManager and if the counter shows the maxlimit is not yet crossed, only then does it fetch the connection.
Else it throws some exception for you to try later.
There might be a design pattern for this on the lines of Business Delegate.
Having said that, I think a bigger problem for you will be
incidentally I can only find out the names of individual db's dynamically as they change day to day
since you will be expected to create new or edit the connection pool settings in Tomcat each day? This is a maintenance nightmare in the future.

mirroring state of the database went to disconnected state in SQL Server 2008

I have 4 databases mirrored using High Protection mode without witness server between two servers(principal and mirror) that are in the same domain. Manual failover worked fine for several days. But later somehow the IP of the principal server was changed in the DNS, then onwards the mirror state of these databses went to disconneted state and it remained in that state only though I did change the principal servers IP to its original in the DNS server.
Why it remained in disconnected state and how to make the mirror state as synchronized?
The IP is irelevant, unless you set the partner name using IPs (which you shouldn't). The mirror is diconnected betcause the principal cannot connetc to it. You will need to investigate why this is the case:
Validate the correctness of partner names on both sides (principal and mirror). Check sys.database_mirroring
Verify the errorlog for any connectivity related messages, on both sides
use SQL Profiler to monitor for events in the Audit Database Mirroring Login Event Class, Database Mirroring State Change Event Class and Broker:Connection Event Class, on both sides
Initiate a manual mirroring session resume: alter database <dbname> set partner resume; on principal

SQL Server replication for HA and LB

I'm currently trying to build a high-availability and load-balanced web application with SQL Server Replication Services technologies. Automatic fail-over is built into the application logic. Basically, there are two groups of application servers running the same, each with its own SQL Server instance. They are set to use the other instance in case of failure. Data is continously replicated between the two SQL Server instances via transactional replication. (A few seconds lag exists, that's okay.)
I set up both servers in a way that Distribution agents run on the Distributor (= Publisher). My idea is, that as long as Server A (publisher) is working, it 'collects' the transactions and forwards them to Server B (subscriber) as soon as it's available. The same for Server B. The default option (distributor on the subscriber) would 'lose' changes while the subscriber is offline. Am I right with that? UPDATE to this first question: transactions (waiting to be delivered) are stored in the distribution database, so they are "safe" anyway. This leads be to another question: if the Distributor Agent is on the subscriber, how will it know about new transactions to be delivered? Via frequent polling?
The two databases have the same schema (except identity seed and increment). Each read-write table is cross-replicated. Why don't I see a loop, when I insert a row into a table? When I read about bi-directional replication in documents or blogs, is this scenario what they mean or rather updateable transactional replication?
What do you think about this scenario in general? This is my first time with replication, and I fear the risks. Therefore any comments are very welcome.