Bug #24004 Cannot running MXJ as part of the JDBC driver on non-3306 ports when ...
Submitted: 6 Nov 2006 10:42 Modified: 22 Nov 2006 22:25
Reporter: Guangyu Liu Email Updates:
Status: Patch pending Impact on me:
None 
Category:Connector / MXJ Severity:S2 (Serious)
Version:5.0 OS:
Assigned to:

[6 Nov 2006 10:42] Guangyu Liu
Description:
I Cannot running MXJ as part of the JDBC driver on non-3306 ports when the port is running an mysql-nt Thread.

//*********test code begin

public void testJDBC() throws Exception{
        String mysqldir = "d:/mysqltest";
        String url = "jdbc:mysql://localhost:3336/mysql?" + 
            "socketFactory=com.mysql.management.driverlaunched.ServerLauncherSocketFactory" +
            "&server.basedir="+mysqldir+"&server.datadir="+mysqldir+"/data";
        Class.forName("com.mysql.jdbc.Driver");
        
        Connection conn = DriverManager.getConnection(url, "root", "");
        Statement stmt = conn.createStatement();
        ResultSet rs = stmt.executeQuery("SELECT VERSION()");
        rs.next();
        String version = rs.getString(1);
        rs.close();
        conn.close();
        System.out.println(version);
}
//*********test code end

This method in the test case cannot run twice! The errors trace is :
com.mysql.jdbc.CommunicationsException: Communications link failure due to underlying exception: 

** BEGIN NESTED EXCEPTION ** 

java.lang.RuntimeException
MESSAGE: Mysqld at D:\mysqltest is running on port 3306 not 3336

STACKTRACE:

java.lang.RuntimeException: Mysqld at D:\mysqltest is running on port 3306 not 3336
	at com.mysql.management.driverlaunched.ServerLauncherSocketFactory.ensureMysqlStarted(ServerLauncherSocketFactory.java:94)
	at com.mysql.management.driverlaunched.ServerLauncherSocketFactory.connect(ServerLauncherSocketFactory.java:58)
	at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:276)
	at com.mysql.jdbc.Connection.createNewIO(Connection.java:2666)
	at com.mysql.jdbc.Connection.<init>(Connection.java:1531)
	at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:266)
	at java.sql.DriverManager.getConnection(Unknown Source)
	at java.sql.DriverManager.getConnection(Unknown Source)
	at com.mysql.management.driverlaunched.ServerLauncherSocketFactoryTest.testJDBC(ServerLauncherSocketFactoryTest.java:213)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
	at java.lang.reflect.Method.invoke(Unknown Source)
	at junit.framework.TestCase.runTest(TestCase.java:154)
	at junit.framework.TestCase.runBare(TestCase.java:127)
	at junit.framework.TestResult$1.protect(TestResult.java:106)
	at junit.framework.TestResult.runProtected(TestResult.java:124)
	at junit.framework.TestResult.run(TestResult.java:109)
	at junit.framework.TestCase.run(TestCase.java:118)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:478)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:344)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)

** END NESTED EXCEPTION **

Last packet sent to the server was 31 ms ago.
	at com.mysql.jdbc.Connection.createNewIO(Connection.java:2741)
	at com.mysql.jdbc.Connection.<init>(Connection.java:1531)
	at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:266)
	at java.sql.DriverManager.getConnection(Unknown Source)
	at java.sql.DriverManager.getConnection(Unknown Source)
	at com.mysql.management.driverlaunched.ServerLauncherSocketFactoryTest.testJDBC(ServerLauncherSocketFactoryTest.java:213)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
	at java.lang.reflect.Method.invoke(Unknown Source)
	at junit.framework.TestCase.runTest(TestCase.java:154)
	at junit.framework.TestCase.runBare(TestCase.java:127)
	at junit.framework.TestResult$1.protect(TestResult.java:106)
	at junit.framework.TestResult.runProtected(TestResult.java:124)
	at junit.framework.TestResult.run(TestResult.java:109)
	at junit.framework.TestCase.run(TestCase.java:118)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:478)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:344)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)

Because the mysql-nt.exe is running on port 3336, the message "Mysqld at D:\mysqltest is running on port 3306 not 3336" is obviously wrong.

The reason is in class "com.mysql.management.driverlaunched.ServerLauncherSocketFactory", the method is "ensureMysqlStarted":
//************
if (mysqld.isRunning()) {
            Object runningPort = mysqld.getServerOptions().get(
                    MysqldResourceI.PORT);
            if (!runningPort.equals(Integer.toString(port))) {
                String msg = "Mysqld at " + mysqld.getBaseDir()
                        + " is running on port " + runningPort + " not " + port;
                throw new RuntimeException(msg);
            }
            return;
}
//************
In the code before, if mysqld.isRunning() is true, the runningPort always is "3306". so always " throw new RuntimeException(msg)".

How to repeat:
Please use the test code in the description.

Suggested fix:
//************
if (mysqld.isRunning()) {
            Object runningPort = mysqld.getServerOptions().get(
                    MysqldResourceI.PORT);
            if (!runningPort.equals(Integer.toString(port))) {
                String msg = "Mysqld at " + mysqld.getBaseDir()
                        + " is running on port " + runningPort + " not " + port;
                throw new RuntimeException(msg);
            }
            return;
}
//************
Change to:
//************
if (mysqld.isRunning()) {
            return;
}
//************
Then I can run my test code. but I am not sure whether it's suitable.
[22 Nov 2006 19:12] Eric Herman
Verified. Bug is certainly present: first connection always works regardless of port. Additional connections are on non 3306 ports fail.
[22 Nov 2006 22:25] Bugs System
A patch for this bug has been committed. After review, it may
be pushed to the relevant source trees for release in the next
version. You can access the patch from:

  http://lists.mysql.com/commits/15739