2

This is really simple, all i'm trying to do is fill a datatable, i have everything surrounded by using() and the connection will not dispose/close. Please help

DataTable loginTbl = MySQLProcessing.MySQLProcessor.StoreProcedureDTTable("Login", ParamArgs, "Login");


 public static DataTable StoreProcedureDTTable(string mysqlQuery, List<string> CommandArgs, string queryName)
    {

        DataTable DTTableTable = new DataTable();
        using (MySqlCommand MySQLCommandFunc = new MySqlCommand(mysqlQuery))
        {
            MySQLCommandFunc.CommandType = CommandType.StoredProcedure;
            foreach (string args in CommandArgs)
            {
                string[] splitArgs = args.Split('|');
                MySQLCommandFunc.Parameters.AddWithValue(splitArgs[0], splitArgs[1]);
            }
            using (MySqlDataAdapter DataDTTables = new MySqlDataAdapter(MySQLCommandFunc))
            {
                DataDTTables.SelectCommand.CommandTimeout = 240000;
                lock (_object)
                {
                    using (MySqlConnection con = new MySqlConnection(ConfigurationManager.ConnectionStrings["mysqlCon"].ConnectionString))
                    {
                        MySQLCommandFunc.Connection = con;
                        DataDTTables.Fill(DTTableTable);
                    }

                }
            }
        }
        DataTable catchConnectionTable = DTTableTable;
        DTTableTable.Dispose();
        return catchConnectionTable;
    }
  • Is an exception being thrown? – Tim Goodman Jul 13 '12 at 14:45
  • nope, runs perfectly just never closes the connection –  Jul 13 '12 at 14:45
  • 1
    Maybe similar to this answer: http://stackoverflow.com/questions/5567097/using-mysqlconnection-in-c-sharp-does-not-close-properly – Tim Goodman Jul 13 '12 at 14:53
  • 1
    The issue there seems to be that the asker didn't understand .NET connection pooling, the end of the using calls the Close method but that just releases the connection back to the connection pool, it doesn't destroy the connection. – Tim Goodman Jul 13 '12 at 14:55

3 Answers3

3

Add Pooling=False to the connection string

1

Have you tried:

      using (MySqlConnection con = new MySqlConnection(ConfigurationManager.ConnectionStrings["mysqlCon"].ConnectionString))
      {
           con.open();
           MySqlDataAdapter DataDTTables = new MySqlDataAdapter(MySQLCommandFunc)
           DataDTTables.SelectCommand.CommandTimeout = 240000;
           MySQLCommandFunc.Connection = con;
           DataDTTables.Fill(DTTableTable);
           con.close();
     }

?

Dan
  • 45,079
  • 17
  • 88
  • 157
  • Dan - Nope odesn't close the connection, the connection is actually onped on at DataDTTables.Fill(DTTableTable);, so i'm assuming the MySqlDataAdapter is holding the connection –  Jul 13 '12 at 14:58
  • Have you tried con.open and con.close though? Otherwise can't help you. – Dan Jul 13 '12 at 15:00
  • Dan - Yes but by default MySqlDataAdapter tries to open the connection –  Jul 13 '12 at 15:02
0

Alternatives to Pooling=false are SqlConnection.ClearPool and SqlConnection.ClearAllPools methods. For more information read: SQL Server Connection Pooling (ADO.NET)

watbywbarif
  • 6,487
  • 8
  • 50
  • 64