Bug #10281 Clone issue with MySqlConnection
Submitted: 30 Apr 2005 18:00 Modified: 8 Jun 2005 23:55
Reporter: Roberto Rabe
Status: Closed
Category:Connector/Net Severity:S2 (Serious)
Version:1.0.4 OS:Microsoft Windows (XP Pro (SP2))
Assigned to: Reggie Burnett Target Version:

[30 Apr 2005 18:00] Roberto Rabe
Description:
See Bug #7478 for original comment.

Cloning a connection throws an unhandled exception if the original MySqlConnection still
has the default ConnectionString Hashtable values. 

How to repeat:
Simple/quick ConnectionTests NUnit test to throw exception:

[Test()]
public void TestConnectionClone()
{
	MySqlConnection c = new MySqlConnection();
	MySqlConnection clone = (MySqlConnection) ((ICloneable)c).Clone();
}

MySql.Data.MySqlClient.Tests.ConnectionTests.TestConnectionClone :
System.NullReferenceException : Object reference not set to an instance of an
object.

   at MySql.Data.MySqlClient.MySqlConnectionString.CreateConnectionString() in
\MySqlClient\ConnectionString.cs:line 300
   at MySql.Data.MySqlClient.MySqlConnectionString.GetConnectionString() in
\MySqlClient\ConnectionString.cs:line 261
   at MySql.Data.MySqlClient.MySqlConnection.get_ConnectionString() in
\MySqlClient\Connection.cs:line 172
   at MySql.Data.MySqlClient.MySqlConnection.System.ICloneable.Clone() in
\MySqlClient\Connection.cs:line 344
   at MySql.Data.MySqlClient.Tests.ConnectionTests.TestConnectionClone() in
\testsuite\connectiontests.cs:line 50

Suggested fix:
ConnectionString.CreateConnectionString() throws exception when looping through keys and
gets to "charset" key:

/// <summary>
/// Uses the values in the keyValues hash to create a
/// connection string
/// </summary>
/// <returns></returns>
public string CreateConnectionString()
{
	string cStr = String.Empty;

	Hashtable values = (Hashtable)keyValues.Clone();
	Hashtable defaultValues = GetDefaultValues();

	if (!PersistSecurityInfo && values.Contains("password") )
		values.Remove( "password" );

	// we always return the server key.  It's not needed but 
	// seems weird for it not to be there.
	cStr = "server=" + values["host"] + ";";
	values.Remove("server");

	foreach (string key in values.Keys)
	{
		if (!values[key].Equals( defaultValues[key]))
			cStr += key + "=" + values[key] + ";";
	}

	return cStr;
}

Fix by checking for charset null (or any undefined value) in loop before grabbing
values[key] to append to cStr.

Roberto
[17 May 2005 10:43] Vasily Kishkin
Could you send any example test suit for this bug with using CreateConnectionString() ?
[17 May 2005 23:43] Roberto Rabe
Not sure why an additional test is needed for CreateConnectionString().  The error happens
when clone is called and a connection string had not yet been set on the original
connection.  The TestConnectionClone() is a really simple and quick test that shows the
error.

Thanks!
[18 May 2005 0:16] Roberto Rabe
Hmmm...maybe this was missed in my original post:

[Test()]
public void TestConnectionClone()
{
	MySqlConnection c = new MySqlConnection();
	MySqlConnection clone = (MySqlConnection) ((ICloneable)c).Clone();
}
[18 May 2005 0:17] Roberto Rabe
That test was near the top of the original post.
[28 May 2005 8:30] Vasily Kishkin
Tested on Win 2000 Sp4, Microsoft Visual C# .NET , Connector .NET 1.0.4 Test case is in
text.
[7 Jun 2005 18:56] Reggie Burnett
Thank you for your bug report. This issue has been committed to our
source repository of that product and will be incorporated into the
next release.

If necessary, you can access the source repository and build the latest
available version, including the bugfix, yourself. More information 
about accessing the source trees is available at
    http://www.mysql.com/doc/en/Installing_source_tree.html

Additional info:

Fixed in 1.0.5
[8 Jun 2005 23:55] Mike Hillyer
Documented in Connector/NET changelog.
[15 Jun 2005 16:30] Wout Huntjens
I got the latest version with the command "bk clone http://mysql.bkbits.net/connector-net
connector-net
". In this version the problem seems not to be fixed. Did you checkin the change
already??
Thanks!