Wednesday, February 22, 2017

MySQL driver and Fuse

The internet caused me a headache this past week.
All the guides on using MySQL with Fuse in a project utilizing Blueprint DSL will demonstrate something like this:
1. In your POM declare <Import-Package>com.mysql.jdbc</Import-Package>
2. Install mysql-connector to your OSGi container using "osgi:install"
3. Install your app.

Here's some links: Fuse examples on Git, http://stackoverflow.com/questions/30307288/mysql-connector-in-osgi-environment-gradle-noclassdeffounderror, http://freemanfang.blogspot.sg/2012/03/how-to-use-jdbc-driver-in-osgi.html, http://www.liquid-reality.de/display/liquid/2012/01/13/Apache+Karaf+Tutorial+Part+6+-+Database+Access

If you were getting error "java.lang.ClassNotFoundException: com.mysql.jdbc.Driver not found", you would probably come across the above guides. However, you would be severely misled, because they all do not address the fundamental issue: MySQL changed their package name of the Driver.class.
I guess this is one of the downfalls of proprietary libraries, they change package names at will, and there are 0 OSGi articles that mention this. So the easy fix to your solution would be:
1. Change this in your POM:

      <plugin>
        <groupId>org.apache.felix</groupId>
        <artifactId>maven-bundle-plugin</artifactId>
        <version>${version.maven-bundle-plugin}</version>
        <extensions>true</extensions>
        <configuration>
          <instructions>
             <Bundle-SymbolicName>${project.artifactId}</Bundle-SymbolicName>
             <Bundle-Description>${project.description}</Bundle-Description>
             <Import-Package>com.mysql.cj.jdbc, com.ibm.mq.jms, com.ibm.mq, com.ibm.mq.constants ,org.springframework.jdbc.*, org.apache.commons.dbcp,*;resolution:=optional</Import-Package>
             <DynamicImport-Package>*</DynamicImport-Package>
          </instructions>
        </configuration>
      </plugin>


2. Change this in your blueprint:

    <bean class="org.apache.commons.dbcp.BasicDataSource" id="dataSource">
        <property name="driverClassName" value="com.mysql.cj.jdbc.Driver"/>
        <property name="url" value="jdbc:mysql://localhost:3306/yourdb"/>
        <property name="username" value="user"/>
        <property name="password" value="pass"/>
    </bean>



I really hope this saves some people a heap of time!!