Differences between CreateConnection() and GetConnection()
- Last UpdatedJul 22, 2024
- 1 minute read
The CreateConnection() method always creates a new connection object, even for calls with identical connection strings and even if GetConnection() has already been called with the same connection string.
Multiple calls to GetConnection() with identical—not similar— connection strings return the same connection object, over and over. If the connection string has never been used in a call to GetConnection(), a new connection object is created, but subsequent calls with the same connection string return the same connection object.
A call to CreateConnection() followed by a call to GetConnection() with the same connection string returns two different connection objects. That is, the connection object returned by GetConnection() is never the same as the connection object that has been returned by CreateConnection().
Note: Because GetConnection() shares the same connection object across multiple scripts, connection pooling is effectively disabled for a specific connection string. All commands or transactions that run on a shared connection are routed through a single queue. Therefore, multiple physical connections for a single unique connection string cannot occur. If you want to use connection pooling, use the CreateConnection() method.
For more information, see Connecting to databases other than SQL Server.