Search code examples
javamysqldatasourceibm-cloud

Bluemix Liberty server.xml MySQL data source configuration


I have an EJB based application that connects to MySQL database and provides web UI for update operations. It works fine when deployed to local WAS Liberty.

Here is the server.xml configuration for data source.

<library id="MySQLDriverLib">
    <file name="${User-defined_JDBC_DRIVER_PATH}/mysql-connector-java-5.1.38-bin.jar"/>
</library>
<dataSource id="DefaultDataSource" type="javax.sql.ConnectionPoolDataSource" transactional="true">
    <jdbcDriver libraryRef="MySQLDriverLib"/>
    <properties URL="jdbc:mysql://localhost:3306/ic16_lab2434" connectionUrl="jdbc:mysql://localhost:3306/ic16_lab2434" driver="com.mysql.jdbc.Driver" driverClass="com.mysql.jdbc.Driver" metadata="mySQL" password="object00" user="root" userName="root" />
</dataSource>
<variable name="User-defined_JDBC_DRIVER_PATH" value="C:\Software\mysql-connector-java-5.1.38"/>

As you can see, it uses library for JDBC driver jar, which is specified with path to file. Obviously, this won't work if I try to deploy just EAR to Bluemix Liberty. That's why I am deploying whole server directory to make least number of changes. Yet even in this case I don't know how to properly configure JDBC driver library for data source so server picks it up. Please help.


Solution

  • If you want to connect to a mysql database and want to manually provide the credentials in the server.xml, you can do the following:

    server.xml:

    <dataSource jndiName="jdbc/TradeDataSource">
        <jdbcDriver id="mysqlDriver" libraryRef="mysql-connector" />
        <properties
            URL="jdbc:mysql://1.2.3.4:3306/db"
            password="mypassword" user="admin" />
    </dataSource>
    <library description="MySQL JDBC Driver" id="mysql-connector"
        name="MySQL Connector">
        <fileset dir="${server.config.dir}" id="mysql-connector-jar"
            includes="mysql-connector-java-*.jar" />
    </library>
    

    In this example, I would put the mysql jar file in the server config directory wlp/usr/servers/defaultServer/mysql-connector-java-5.1.34-bin.jar

    You can now cf push directly from the defaultServer dir

    However, the liberty buildpack can automatically generate the server.xml datasource config for databases that you bind from the Bluemix catalog. For example, if I create and bind a SQLDB or ClearDB service to my Liberty application and name the service it "TradeDataSource", the buildpack will generate the config and add the right driver jar to the classpath automatically.

    cf files yourappname app/wlp/usr/servers/defaultServer/server.xml

    <dataSource id='mysql-TradeDataSource' jdbcDriverRef='mysql-driver' jndiName='jdbc/TradeDataSource' transactional='true' type='javax.sql.ConnectionPoolDataSource'>
        <properties id='mysql-TradeDataSource-props' databaseName='${cloud.services.TradeDataSource.connection.name}' user='${cloud.services.TradeDataSource.connection.user}' password='${cloud.services.TradeDataSource.connection.password}' portNumber='${cloud.services.TradeDataSource.connection.port}' serverName='${cloud.services.TradeDataSource.connection.host}'/>
        <connectionManager id='mysql-TradeDataSource-conMgr' maxPoolSize='10'/>
    </dataSource>
    <jdbcDriver id='mysql-driver' javax.sql.XADataSource='org.mariadb.jdbc.MySQLDataSource' javax.sql.ConnectionPoolDataSource='org.mariadb.jdbc.MySQLDataSource' libraryRef='mysql-library'/>
    

    I can now look up the datasource using its jndi name: jdbc/TradeDataSource