0

I am not quite sure if I am experiencing a bug or I don't get how it supposed to work. So recently I've been trying to resolve this exception MySql.Data.MySqlClient.MySqlException: 'There is already an open DataReader associated with this Connection which must be closed first.' and I started a debugging session to see what is going on. What I saw doesn't quite compute in my head (see the attached screenshot).

After I call reader.Close(), the property IsClosed stays false, and repeated calls to .Close() (in the immediate windows) do not change that.

Is this expected behavior and my issue is somewhere else, or is this a bug and the cause of the exception?

This happens when using MySql.Data v6.10.3-rc (from nuget.org), which is the only one supporting .NET Standard as of now. Also, the code is compiled to a .NET Core 2.0 app.

UPD: There are tasks around, but the reader is only worked with from the main thread. Also, reader is declared as DbDataReader, from which MySqlDataReader is inherited.

UPD2: Apparently, if I call ((MySql.Data.MySqlClient.MySqlDataReader)reader).Close(), then the reader closes properly. Looks to me like a bug in the connector and how they use virtual methods. Right?

Debugger session

bazzilic
  • 671
  • 1
  • 6
  • 17
  • 1
    are you inside an async task?? – apomene Sep 25 '17 at 14:49
  • @apomene Nope, outside the task, normal single-threaded code. There are tasks around but they don't deal with the reader, only the data taken from the reader. – bazzilic Sep 25 '17 at 14:51
  • 1
    What happens if you call `reader.Dispose()`? Does it get disposed of? – waka Sep 25 '17 at 14:55
  • @waka Nothing happens. However, if I do this: `((MySql.Data.MySqlClient.MySqlDataReader)reader).Close()`, then it works. `reader` is declared as `DbDataReader` in my code. – bazzilic Sep 25 '17 at 14:59

2 Answers2

0

Make sure you always use all your readers within a using clause. I use every reader and connection object within using clauses and have not once noticed any error or not closing. I don't see many ways things could go wrong with the using clause.

Neville Nazerane
  • 5,682
  • 3
  • 31
  • 68
  • There's a bug in MySqlDataReader implementation, they do not override the method so the virtual call resolves to empty implementation of DbDatReader. – bazzilic Oct 15 '17 at 07:44
  • i just noticed you talked about using v6.x since only that is supported on standard. I am currently using 8.0.8-dmr and it works fine. There is a newer version as well if you want to try – Neville Nazerane Oct 16 '17 at 03:19
  • Even after reading the documentation, I still don't get what's the difference between the 6.x and 8.x connectors. – bazzilic Oct 18 '17 at 04:21
  • 1
    I had created this nuget called `mysql.simple` and I had been using that one since. https://www.nuget.org/packages/MySql.Simple It contains wrapper classes which I initially made to handle issues such as this. Right now it got many other helper functions too. You can always pull out the mysqlreader object out of it. I it wouldn't be too much work you can try it out. You shouldn't be facing any closing issues with this – Neville Nazerane Oct 19 '17 at 03:26
0

I would suggest doing more than just adding in a using statement, since as I just witnessed an exception will still leave a reader open.

While it does add some overhead, unless you can be 100% sure that you will not have an error or some bad data such as a wonderful null exception error, use exception handling.

use a simple try/catch statement and be sure the reader close and declare the reader outside the exception handling so that even on an error the reader is closed.

James Mac
  • 1
  • 3