SqlConnectionPoolKey.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. //------------------------------------------------------------------------------
  2. // <copyright file="ConnectionPoolKey.cs" company="Microsoft">
  3. // Copyright (c) Microsoft Corporation. All rights reserved.
  4. // </copyright>
  5. // <owner current="true" primary="true">[....]</owner>
  6. // <owner current="true" primary="false">[....]</owner>
  7. //------------------------------------------------------------------------------
  8. namespace System.Data.SqlClient
  9. {
  10. using System;
  11. using System.Collections;
  12. using System.Data.Common;
  13. using System.Diagnostics;
  14. // SqlConnectionPoolKey: Implementation of a key to connection pool groups for specifically to be used for SqlConnection
  15. // Connection string and SqlCredential are used as a key
  16. internal class SqlConnectionPoolKey : DbConnectionPoolKey, ICloneable
  17. {
  18. private SqlCredential _credential;
  19. private int _hashValue;
  20. private readonly string _accessToken;
  21. internal SqlConnectionPoolKey(string connectionString, SqlCredential credential, string accessToken) : base(connectionString)
  22. {
  23. Debug.Assert(_credential == null || _accessToken == null, "Credential and AccessToken can't have the value at the same time.");
  24. _credential = credential;
  25. _accessToken = accessToken;
  26. CalculateHashCode();
  27. }
  28. private SqlConnectionPoolKey(SqlConnectionPoolKey key) : base (key)
  29. {
  30. _credential = key.Credential;
  31. _accessToken = key.AccessToken;
  32. CalculateHashCode();
  33. }
  34. object ICloneable.Clone()
  35. {
  36. return new SqlConnectionPoolKey(this);
  37. }
  38. internal override string ConnectionString
  39. {
  40. get
  41. {
  42. return base.ConnectionString;
  43. }
  44. set
  45. {
  46. base.ConnectionString = value;
  47. CalculateHashCode();
  48. }
  49. }
  50. internal SqlCredential Credential
  51. {
  52. get
  53. {
  54. return _credential;
  55. }
  56. }
  57. internal string AccessToken
  58. {
  59. get
  60. {
  61. return _accessToken;
  62. }
  63. }
  64. public override bool Equals(object obj)
  65. {
  66. SqlConnectionPoolKey key = obj as SqlConnectionPoolKey;
  67. return (key != null && _credential == key._credential && ConnectionString == key.ConnectionString && Object.ReferenceEquals(_accessToken, key._accessToken));
  68. }
  69. public override int GetHashCode()
  70. {
  71. return _hashValue;
  72. }
  73. private void CalculateHashCode()
  74. {
  75. _hashValue = base.GetHashCode();
  76. if (_credential != null)
  77. {
  78. unchecked
  79. {
  80. _hashValue = _hashValue * 17 + _credential.GetHashCode();
  81. }
  82. }
  83. else if (_accessToken != null)
  84. {
  85. unchecked
  86. {
  87. _hashValue = _hashValue * 17 + _accessToken.GetHashCode();
  88. }
  89. }
  90. }
  91. }
  92. }