Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

KeyNotFoundException in MySqlBulkLoader.GetAndRemoveStream() #757

Closed
wangjia184 opened this issue Jan 9, 2020 · 3 comments
Closed

KeyNotFoundException in MySqlBulkLoader.GetAndRemoveStream() #757

wangjia184 opened this issue Jan 9, 2020 · 3 comments
Assignees
Labels

Comments

@wangjia184
Copy link

wangjia184 commented Jan 9, 2020

When loading a streaming into a table, MySqlBulkLoader.LoadAsync() may fail with KeyNotFoundException

  at System.ThrowHelper.ThrowKeyNotFoundException[T](T key)
   at System.Collections.Generic.Dictionary`2.get_Item(TKey key)
   at MySql.Data.MySqlClient.MySqlBulkLoader.GetAndRemoveStream(String streamKey) in C:\projects\mysqlconnector\src\MySqlConnector\MySql.Data.MySqlClient\MySqlBulkLoader.cs:line 241
   at MySql.Data.MySqlClient.MySqlBulkLoader.<LoadAsync>d__80.MoveNext() in C:\projects\mysqlconnector\src\MySqlConnector\MySql.Data.MySqlClient\MySqlBulkLoader.cs:line 207
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.ConfiguredTaskAwaitable`1.ConfiguredTaskAwaiter.GetResult()

My code:

                MySqlBulkLoader loader = new MySqlBulkLoader(conn);
                loader.TableName = "tournament_result";
                loader.FieldTerminator = DataFileGenerator.FIELDS_TERMINATER;
                loader.EscapeCharacter = '\b';
                loader.LineTerminator = DataFileGenerator.LINES_TERMINATER;
                loader.Columns.Add("ID");
                loader.Columns.Add("UserID");
                loader.Columns.Add("Score");
                loader.Columns.Add("Rank");
                loader.Columns.Add("Comments");
                loader.Timeout = 60 * 5;
                loader.Local = true;
                loader.ConflictOption = MySqlBulkLoaderConflictOption.Replace;
                using (loader.SourceStream = generator.GetStream())
                {
                    await loader.LoadAsync().ConfigureAwait(false);
                }

The reason is that, when ExecuteNonQueryAsync fails, closeStream is not set to false but the key does not exist in s_sources.

KeyNotFoundException is raised in the finally block when calling GetAndRemoveSource

((IDisposable) GetAndRemoveSource(FileName!)).Dispose();

@bgrainger bgrainger added the bug label Jan 9, 2020
@bgrainger bgrainger self-assigned this Jan 10, 2020
@bgrainger
Copy link
Member

bgrainger commented Jan 10, 2020

The reason is that, when ExecuteNonQueryAsync fails

Your original exception is probably being overwritten by the KeyNotFoundException, but can you explain how or why the original failure is occurring during the bulk load?

@wangjia184
Copy link
Author

wangjia184 commented Jan 11, 2020

The supplied stream contains NULL but quotation is not used hence it cannot be recognized.

Anyway, because the exception is overridden in finally block, it makes harder to locate the issue.

@bgrainger
Copy link
Member

bgrainger commented Mar 7, 2020

Fixed in 0.62.0.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

No branches or pull requests

2 participants