Bug #3848 Using a MySQLDatasource without server name fails
Submitted: 21 May 2004 13:02 Modified: 27 May 2004 19:52
Reporter: Klaus Halfmann Email Updates:
Status: Closed Impact on me:
None 
Category:Connector / J Severity:S3 (Non-critical)
Version:mysql-connector-java-3.0.12-production-b OS:- any -
Assigned to: Mark Matthews CPU Architecture:Any

[21 May 2004 13:02] Klaus Halfmann
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
[27 May 2004 19:52] Mark Matthews
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