Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Connection pool doesn't work well with access tokens #438

Closed
CurtHagenlocher opened this issue Feb 21, 2020 · 3 comments · Fixed by #443
Closed

Connection pool doesn't work well with access tokens #438

CurtHagenlocher opened this issue Feb 21, 2020 · 3 comments · Fixed by #443
Milestone

Comments

@CurtHagenlocher
Copy link

Describe the bug

The connection pool doesn't work well with explicit access tokens because SqlConnectionPoolKey uses reference equality to compare access tokens. It should use string.Equals instead.

The error is in line

Object.ReferenceEquals(_accessToken, key._accessToken) &&

To reproduce

Create two strings at1 and at2 which contain the same valid access token.
Open a connection with pooling enabled (which is the default); set SqlConnection.AccessToken to at1 and run a query. Close the connection. Open a second connection with pooling enabled; set SqlConnection.AccessToken to at2 and run a second query. Check the number of open connections on the server; it will be 2.

Repeat the experiment except using at1 both times. There will only be a single open connection on the server.

Expected behavior

Pooled connections using an explicit access token should be shared even if different string objects containing the same value were used to initialize the connection.

Further technical details

N/A

@cheenamalhotra
Copy link
Member

Hi @CurtHagenlocher

I'm using a simple test to verify this behavior with Microsoft.Data.SqlClient (targeting .NET Framework) but I don't see multiple connections getting created, please find my repro app attached below, and let me know if I understood the use-case same way.

  • I created 6 connections with different objects for access tokens, some global, and some local scope, but below was my result:
    image
  • App is kept running to keep pooled connection active.
  • On querying database with sp_who2, I see my application has only 1 active connection.
  • On querying database with the SPID of the active connection, with query:
    select num_reads from sys.dm_exec_connections where session_id = @@SPID,
    I get output: 6.

Please help me reproduce the problem. Thanks!
TestAAD.zip

@mpanasiuk-apptio
Copy link

Could it be that compiler optimized all these variables with the same string value and made the all be "reference-equal"?

@cheenamalhotra
Copy link
Member

@mpanasiuk-apptio
This issue was fixed in #443

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants