Jan 9, 2013

Sonar with mysql


I wanted to share few details on configuring Sonar to use mysql. It is quite easy, just follow documentation available at  http://docs.codehaus.org/display/SONAR/Installing+Sonar

On sonar startup, if you get this error 
Cause: org.apache.commons.dbcp.SQLNestedException: Cannot create PoolableConnectionFactory (Unknown database 'sonar')

It might mean that sonar database is not configured properly. Try creating the database by running the scripts  available @ https://github.com/SonarSource/sonar/tree/master/sonar-application/src/main/assembly/extras/database/mysql.

Now on maven side , you need to add this to the setting.xml
<profile>
            <id>sonar</id>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
            <properties>
                <!-- SERVER ON A REMOTE HOST -->
               <sonar.jdbc.url>jdbc:mysql://localhost:3306/sonar?useUnicode=true&amp;characterEncoding=utf8</sonar.jdbc.url>
                <sonar.jdbc.driverClassName>com.mysql.jdbc.Driver</sonar.jdbc.driverClassName>
                <sonar.jdbc.username>sonar</sonar.jdbc.username>
                <sonar.jdbc.password>sonar</sonar.jdbc.password>
                <sonar.host.url>http://localhost:9000</sonar.host.url>
            </properties>
 </profile>


 If you get the error ‘Cannot load JDBC driver class’ on running mvn sonar:sonar  
 check for the driver class name. 
If driver class is derby or H2, it means the mysql configuration is not picked by from setting.xml. Try giving profile for sonar explicitly and including the path to setting.xml, this would ensure that the mysql settings is pulled up by maven.

If however the driver class name in the error is 'com.mysql.jdbc.Driver', this means that the profile is read properly. This error is mostly because of incorrect JDBC url; eventhough the eror messages makes you think that you need to copy the driver file somewhere for maven to pickup. 

In my case I had my jdbc url as jdbc:mysql://localhost:3306/sonar, when I changed to jdbc:mysql://localhost:3306/sonar?useUnicode=true&amp;characterEncoding=utf8 everything worked fine.