Description:
The following code will fail with a strange Exception:
MysqlDataSource mds = new MysqlDataSource();
mds.setDatabaseName("test");
mds.getConnection();
java.sql.SQLException:
Unable to connect to any hosts due to exception:
java.net.UnknownHostException: 3306
How to repeat:
// Add to your Regression Tests please:
package testsuite.regression;
import java.sql.SQLException;
import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;
import testsuite.BaseTestCase;
/**
* Regression tests for DatabaseMetaData
*
* @author Mark Matthews
* @author <a href="mailto:klaus.halfmann@top-logic.com">Klaus Halfmann</a>
* @version $Id: $
*/
public class DataSourceRegressionTest extends BaseTestCase {
/**
* Creates a new DataSourceRegressionTest.
*
* @param name the name of the test
*/
public DataSourceRegressionTest(String name) {
super(name);
}
/**
* Runs all test cases in this test suite
*
* @param args
*/
public static void main(String[] args) {
junit.textui.TestRunner.run(DataSourceRegressionTest.class);
}
/**
* No Need to setup a Connection here.
*
* @throws Exception if an error occurs.
*/
public void setUp() throws Exception {
}
/**
* Tests using a Default CTor.
*
* Should not work, need at lest database name.
*
* @throws Exception if the test fails.
*/
public void testDefaultCTor() throws Exception {
MysqlDataSource mds = new MysqlDataSource();
try {
this.conn = mds.getConnection();
fail("Need at least name of database to use");
}
catch (SQLException expected) { }
}
/**
* Tests for BUG when no Host is given.
*
* Should use localhost in this case.
*
* @throws Exception if the test fails.
*/
public void testMinimumSet() throws Exception {
MysqlDataSource mds = new MysqlDataSource();
mds.setDatabaseName("test");
this.conn = mds.getConnection();
}
/**
* Tests for Host and dbname given.
*
* @throws Exception if the test fails.
*/
public void testCTorWithHost() throws Exception {
MysqlDataSource mds = new MysqlDataSource();
mds.setServerName ("localhost");
mds.setDatabaseName("test");
this.conn = mds.getConnection();
}
}
Suggested fix:
1) Throw some better Exception like
java.sql.SQLException: Unable to connect, no host given
2) Use localhost a default (as was in 3.0.11)
This can be accomplished by.
a) Use "localhost" a default server name for DataSources
b) Fix the code to parse an URL to not confuse the host with the port 3306
c) Change the code for MySQLDataSource to create a Connection in some
other way than first creating an URL and than parsing it.
Thank you for the sources and Testcases
Description: The following code will fail with a strange Exception: MysqlDataSource mds = new MysqlDataSource(); mds.setDatabaseName("test"); mds.getConnection(); java.sql.SQLException: Unable to connect to any hosts due to exception: java.net.UnknownHostException: 3306 How to repeat: // Add to your Regression Tests please: package testsuite.regression; import java.sql.SQLException; import com.mysql.jdbc.jdbc2.optional.MysqlDataSource; import testsuite.BaseTestCase; /** * Regression tests for DatabaseMetaData * * @author Mark Matthews * @author <a href="mailto:klaus.halfmann@top-logic.com">Klaus Halfmann</a> * @version $Id: $ */ public class DataSourceRegressionTest extends BaseTestCase { /** * Creates a new DataSourceRegressionTest. * * @param name the name of the test */ public DataSourceRegressionTest(String name) { super(name); } /** * Runs all test cases in this test suite * * @param args */ public static void main(String[] args) { junit.textui.TestRunner.run(DataSourceRegressionTest.class); } /** * No Need to setup a Connection here. * * @throws Exception if an error occurs. */ public void setUp() throws Exception { } /** * Tests using a Default CTor. * * Should not work, need at lest database name. * * @throws Exception if the test fails. */ public void testDefaultCTor() throws Exception { MysqlDataSource mds = new MysqlDataSource(); try { this.conn = mds.getConnection(); fail("Need at least name of database to use"); } catch (SQLException expected) { } } /** * Tests for BUG when no Host is given. * * Should use localhost in this case. * * @throws Exception if the test fails. */ public void testMinimumSet() throws Exception { MysqlDataSource mds = new MysqlDataSource(); mds.setDatabaseName("test"); this.conn = mds.getConnection(); } /** * Tests for Host and dbname given. * * @throws Exception if the test fails. */ public void testCTorWithHost() throws Exception { MysqlDataSource mds = new MysqlDataSource(); mds.setServerName ("localhost"); mds.setDatabaseName("test"); this.conn = mds.getConnection(); } } Suggested fix: 1) Throw some better Exception like java.sql.SQLException: Unable to connect, no host given 2) Use localhost a default (as was in 3.0.11) This can be accomplished by. a) Use "localhost" a default server name for DataSources b) Fix the code to parse an URL to not confuse the host with the port 3306 c) Change the code for MySQLDataSource to create a Connection in some other way than first creating an URL and than parsing it. Thank you for the sources and Testcases