Bug #1246 MysqlDataSourceFactory port paramter not optional
Submitted: 11 Sep 2003 6:00 Modified: 13 Jan 2004 13:33
Reporter: Klaus Halfmann Email Updates:
Status: Closed Impact on me:
None 
Category:Connector / J Severity:S4 (Feature request)
Version:mysql-connector-java-3.0.8-stable OS:Windows (WinXP)
Assigned to: CPU Architecture:Any

[11 Sep 2003 6:00] Klaus Halfmann
Description:
I tried to register MYSQL as dataSource with Tomcat in our server.xml 
using the following entries:
...
<Resource name="jdbc/tl-datasource" auth="Container"
	type="com.mysql.jdbc.jdbc2.optional.MysqlDataSource" />
<ResourceParams name="jdbc/tl-datasource">
    <parameter>
	<name>factory</name>
	<value>com.mysql.jdbc.jdbc2.optional.MysqlDataSourceFactory</value>
    </parameter>
    <parameter>
	<name>user</name>
	<value>xxx</value>
    </parameter>
    <parameter>
	<name>password</name>
	<value>xxx</value>
    </parameter>
    <parameter>
	<name>serverName</name> 	
        <value>localhost</value>
    </parameter>
   <parameter>
	<name>databaseName</name>
 	<value>xxx</value>
    </parameter>
</ResourceParams>

How to repeat:
Take a Vanilla tomcat jakarta-tomcat-4.1.24
put mysql-connector-java-3.0.8-stable-bin.jar
into the 'common/lib' directory
add the above to some context.

in some jsp page (or servlet or whatever) incldue the following code

      Context ctx = new InitialContext();
        if (ctx == null)
            return;
              
        DataSource ds =
            (DataSource) ctx.lookup("java:comp/env/jdbc/tl-datasource");

Tomcat will refuse to start up with 
        at org.apache.naming.NamingContext.lookup(NamingContext.java:844)
        ....
        at org.apache.naming.SelectorContext.lookup(SelectorContext.java:183)
        at javax.naming.InitialContext.lookup(InitialContext.java:347)

(One problem here is that tomcat silently swallows the exception originating
 from MysqlDataSourceFactory)

Suggested fix:
a) Fix: add the (not optional !) port parameter 
        <parameter>
              <name>port</name> 
              <value>3306</value>
	</parameter>

b) Change MysqlDataSourceFactory to make most parameters optional

int portNumber = 3306;
String sPortNumber = (String)ref.get("port").getContent();
if (sPortNumber != null)
    portNumber = Integer.parseInt(sPortNumber);
String serverName = (String)ref.get("serverName").getContent());
if (serverName != null)
     dataSource.setUser(serverName);
String user = (String)ref.get("user").getContent());
if (user != null)
     dataSource.setUser(user);
...

c) Document this behaviour more detailed :-( 
   (It took me 4 hours to figure out where the problem was)
[13 Jan 2004 13:33] Mark Matthews
Thank you for your bug report. This issue has already been fixed
in the latest released version of that product, which you can download at 
http://www.mysql.com/downloads/

Additional info:

This was fixed in 3.0.9.