|
| 1 | +using System; |
| 2 | +using System.Data.SqlClient; |
| 3 | + |
| 4 | +namespace SqlConnectionExamples |
| 5 | +{ |
| 6 | + public class SqlConnectionWrapper : IDisposable |
| 7 | + { |
| 8 | + SqlConnection connection; |
| 9 | + |
| 10 | + public SqlConnectionWrapper(SqlConnection con) |
| 11 | + { |
| 12 | + this.connection = con; |
| 13 | + } |
| 14 | + |
| 15 | + public SqlConnection GetConnection() |
| 16 | + { |
| 17 | + return connection; |
| 18 | + } |
| 19 | + |
| 20 | + public void Dispose() |
| 21 | + { |
| 22 | + this.connection.Dispose(); |
| 23 | + } |
| 24 | + } |
| 25 | + |
| 26 | + public class Aliases |
| 27 | + { |
| 28 | + public static SqlConnection GetSqlConnection() |
| 29 | + { |
| 30 | + SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder(); |
| 31 | + builder.ConnectionString = "Server=tcp:database.windows.net,1433;Initial Catalog=leaks;Persist Security Info=False;User ID=resourceleaks;Password=ResourceLe@ks;MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False;Connection Timeout=300;"; |
| 32 | + var sqlconnection = new SqlConnection(builder.ConnectionString); |
| 33 | + |
| 34 | + return sqlconnection; |
| 35 | + } |
| 36 | + |
| 37 | + public static void runSqlQuery(System.Data.SqlClient.SqlConnection con) |
| 38 | + { |
| 39 | + String sql = "select top 10 FirstName, LastName from [SalesLT].[Customer]"; |
| 40 | + |
| 41 | + using (SqlCommand command = new SqlCommand(sql, con)) |
| 42 | + { |
| 43 | + using (SqlDataReader reader = command.ExecuteReader()) |
| 44 | + { |
| 45 | + while (reader.Read()) |
| 46 | + { |
| 47 | + Console.WriteLine("{0} {1}", reader.GetString(0), reader.GetString(1)); |
| 48 | + } |
| 49 | + } |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + public static void findAliases() |
| 54 | + { |
| 55 | + SqlConnection con = GetSqlConnection(); // No resource leak |
| 56 | + |
| 57 | + SqlConnectionWrapper cw = new SqlConnectionWrapper(con); // Resource aliases |
| 58 | + |
| 59 | + runSqlQuery(cw.GetConnection()); |
| 60 | + |
| 61 | + cw.Dispose(); |
| 62 | + } |
| 63 | + |
| 64 | + public static void usingStmt() |
| 65 | + { |
| 66 | + using SqlConnection con = GetSqlConnection(); // No resource leak because of using statement |
| 67 | + |
| 68 | + SqlConnectionWrapper cw = new SqlConnectionWrapper(con); // Resource aliases |
| 69 | + |
| 70 | + runSqlQuery(cw.GetConnection()); |
| 71 | + } |
| 72 | + |
| 73 | + public static void exceptionalPaths1() |
| 74 | + { |
| 75 | + try |
| 76 | + { |
| 77 | + SqlConnection con = GetSqlConnection(); // Resource leak along exceptional path |
| 78 | + |
| 79 | + SqlConnectionWrapper cw = new SqlConnectionWrapper(con); // Resource aliases |
| 80 | + |
| 81 | + runSqlQuery(cw.GetConnection()); |
| 82 | + |
| 83 | + cw.Dispose(); |
| 84 | + } |
| 85 | + catch (Exception ex) { } |
| 86 | + |
| 87 | + } |
| 88 | + |
| 89 | + public static void exceptionalPaths2() |
| 90 | + { |
| 91 | + SqlConnection con = null; |
| 92 | + |
| 93 | + try |
| 94 | + { |
| 95 | + con = GetSqlConnection(); // No resource leak |
| 96 | + |
| 97 | + SqlConnectionWrapper cw = new SqlConnectionWrapper(con); // Resource aliases |
| 98 | + |
| 99 | + runSqlQuery(cw.GetConnection()); |
| 100 | + |
| 101 | + cw.Dispose(); |
| 102 | + } |
| 103 | + catch (Exception ex) { } |
| 104 | + finally |
| 105 | + { |
| 106 | + if (con != null) |
| 107 | + con.Dispose(); |
| 108 | + } |
| 109 | + |
| 110 | + } |
| 111 | + |
| 112 | + public static void multipleSinks() |
| 113 | + { |
| 114 | + Console.WriteLine("Enter count: "); |
| 115 | + int count = Convert.ToInt32(Console.ReadLine()); |
| 116 | + |
| 117 | + SqlConnection con = GetSqlConnection(); // No resource leak |
| 118 | + |
| 119 | + SqlConnectionWrapper cw = new SqlConnectionWrapper(con); // Resource aliases |
| 120 | + |
| 121 | + runSqlQuery(cw.GetConnection()); |
| 122 | + |
| 123 | + if(count > 10) |
| 124 | + cw.Dispose(); // Dispose 1 |
| 125 | + else |
| 126 | + con.Dispose(); // Dispose 2 |
| 127 | + } |
| 128 | + |
| 129 | + public static void main() |
| 130 | + { |
| 131 | + Console.WriteLine($"Total memory: {GC.GetTotalMemory(true) / 1024f / 1024f} MB" + "\n---------------------------------------\n"); |
| 132 | + |
| 133 | + findAliases(); |
| 134 | + exceptionalPaths1(); |
| 135 | + exceptionalPaths2(); |
| 136 | + multipleSinks(); |
| 137 | + usingStmt(); |
| 138 | + |
| 139 | + GC.Collect(); |
| 140 | + GC.WaitForFullGCComplete(); |
| 141 | + Console.WriteLine("---------------------------------------\n" + $"Total memory: {GC.GetTotalMemory(true) / 1024f / 1024f} MB"); |
| 142 | + } |
| 143 | + |
| 144 | + } |
| 145 | +} |
0 commit comments