Bug #17106 MySql.Data.MySqlClient.CharSetMap.GetEncoding thread synchronization issue
Description:
Under ASP.NET the following errors (among many others) occur if multiple threads try to call MySqlConnection.Open concurrently:
System.ArgumentException: Item has already been added. Key in dictionary: "gb2312" Key being added: "gb2312"
at System.Collections.Hashtable.Insert(Object key, Object nvalue, Boolean add)
at System.Collections.Hashtable.Add(Object key, Object value)
at MySql.Data.MySqlClient.CharSetMap.LoadCharsetMap()
at MySql.Data.MySqlClient.CharSetMap.GetEncoding(DBVersion version, String CharSetName)
at MySql.Data.MySqlClient.Driver.Configure(MySqlConnection connection)
at MySql.Data.MySqlClient.NativeDriver.Configure(MySqlConnection connection)
at MySql.Data.MySqlClient.MySqlConnection.Open()
MySql.Data.MySqlClient.MySqlException: Character set 'latin1' is not supported
at MySql.Data.MySqlClient.CharSetMap.GetEncoding(DBVersion version, String CharSetName)
at MySql.Data.MySqlClient.Driver.Configure(MySqlConnection connection)
at MySql.Data.MySqlClient.NativeDriver.Configure(MySqlConnection connection)
at MySql.Data.MySqlClient.MySqlConnection.Open()
System.ArgumentException: Item has already been added. Key in dictionary: "latin4" Key being added: "latin4"
at System.Collections.Hashtable.Insert(Object key, Object nvalue, Boolean add)
at System.Collections.Hashtable.Add(Object key, Object value)
at MySql.Data.MySqlClient.CharSetMap.LoadCharsetMap()
at MySql.Data.MySqlClient.CharSetMap.GetEncoding(DBVersion version, String CharSetName)
at MySql.Data.MySqlClient.Driver.Configure(MySqlConnection connection)
at MySql.Data.MySqlClient.NativeDriver.Configure(MySqlConnection connection)
at MySql.Data.MySqlClient.MySqlConnection.Open()
The problem seems to be in: MySql.Data.MySqlClient.CharSetMap.GetEncoding
if (mapping == null )
InitializeMapping();
How to repeat:
Can only be repeated if multiple threads call MySqlConnection.Open() at the same time.
Suggested fix:
Please move the InitializeMapping call into a static constructor which is guaranteed to be thread safe:
static CharSetMap()
{
InitializeMapping();
}
or (more expensive)
public static Encoding GetEncoding( DBVersion version, string CharSetName )
{
lock(typeof(CharSetMap))
{
if (mapping == null )
InitializeMapping();
}
...
}
Portions of this website are copyright © 2001, 2002 The PHP Group
Page generated in 0.053 sec. using MySQL 8.4.7-u6-cloud
Timestamp references displayed by the system are UTC.
Content reproduced on this site is the property of the
respective copyright holders.
It is not reviewed in advance
by Oracle and does not necessarily represent the opinion of
Oracle or any other party.
Description: Under ASP.NET the following errors (among many others) occur if multiple threads try to call MySqlConnection.Open concurrently: System.ArgumentException: Item has already been added. Key in dictionary: "gb2312" Key being added: "gb2312" at System.Collections.Hashtable.Insert(Object key, Object nvalue, Boolean add) at System.Collections.Hashtable.Add(Object key, Object value) at MySql.Data.MySqlClient.CharSetMap.LoadCharsetMap() at MySql.Data.MySqlClient.CharSetMap.GetEncoding(DBVersion version, String CharSetName) at MySql.Data.MySqlClient.Driver.Configure(MySqlConnection connection) at MySql.Data.MySqlClient.NativeDriver.Configure(MySqlConnection connection) at MySql.Data.MySqlClient.MySqlConnection.Open() MySql.Data.MySqlClient.MySqlException: Character set 'latin1' is not supported at MySql.Data.MySqlClient.CharSetMap.GetEncoding(DBVersion version, String CharSetName) at MySql.Data.MySqlClient.Driver.Configure(MySqlConnection connection) at MySql.Data.MySqlClient.NativeDriver.Configure(MySqlConnection connection) at MySql.Data.MySqlClient.MySqlConnection.Open() System.ArgumentException: Item has already been added. Key in dictionary: "latin4" Key being added: "latin4" at System.Collections.Hashtable.Insert(Object key, Object nvalue, Boolean add) at System.Collections.Hashtable.Add(Object key, Object value) at MySql.Data.MySqlClient.CharSetMap.LoadCharsetMap() at MySql.Data.MySqlClient.CharSetMap.GetEncoding(DBVersion version, String CharSetName) at MySql.Data.MySqlClient.Driver.Configure(MySqlConnection connection) at MySql.Data.MySqlClient.NativeDriver.Configure(MySqlConnection connection) at MySql.Data.MySqlClient.MySqlConnection.Open() The problem seems to be in: MySql.Data.MySqlClient.CharSetMap.GetEncoding if (mapping == null ) InitializeMapping(); How to repeat: Can only be repeated if multiple threads call MySqlConnection.Open() at the same time. Suggested fix: Please move the InitializeMapping call into a static constructor which is guaranteed to be thread safe: static CharSetMap() { InitializeMapping(); } or (more expensive) public static Encoding GetEncoding( DBVersion version, string CharSetName ) { lock(typeof(CharSetMap)) { if (mapping == null ) InitializeMapping(); } ... }