SqlConnectionPoolKey.cs 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. // SqlConnectionPoolKey: Implementation of a key to connection pool groups for specifically to be used for SqlConnection
  14. // Connection string and SqlCredential are used as a key
  15. internal class SqlConnectionPoolKey : DbConnectionPoolKey, ICloneable
  16. {
  17. private SqlCredential _credential;
  18. private int _hashValue;
  19. internal SqlConnectionPoolKey(string connectionString, SqlCredential credential) : base(connectionString)
  20. {
  21. _credential = credential;
  22. CalculateHashCode();
  23. }
  24. private SqlConnectionPoolKey(SqlConnectionPoolKey key) : base (key)
  25. {
  26. _credential = key.Credential;
  27. CalculateHashCode();
  28. }
  29. object ICloneable.Clone()
  30. {
  31. return new SqlConnectionPoolKey(this);
  32. }
  33. internal override string ConnectionString
  34. {
  35. get
  36. {
  37. return base.ConnectionString;
  38. }
  39. set
  40. {
  41. base.ConnectionString = value;
  42. CalculateHashCode();
  43. }
  44. }
  45. internal SqlCredential Credential
  46. {
  47. get
  48. {
  49. return _credential;
  50. }
  51. }
  52. public override bool Equals(object obj)
  53. {
  54. SqlConnectionPoolKey key = obj as SqlConnectionPoolKey;
  55. return (key != null && _credential == key._credential && ConnectionString == key.ConnectionString);
  56. }
  57. public override int GetHashCode()
  58. {
  59. return _hashValue;
  60. }
  61. private void CalculateHashCode()
  62. {
  63. _hashValue = base.GetHashCode();
  64. if (_credential != null)
  65. {
  66. unchecked
  67. {
  68. _hashValue = _hashValue * 17 + _credential.GetHashCode();
  69. }
  70. }
  71. }
  72. }
  73. }