Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jenkins shared library fails to find SQL driver

This is a new class in a shared library for Jenkins.

The shared library is loaded via the standard method under Manage Jenkins > Configure System

package com.mycorp.core;

@Grab(group='com.microsoft.sqlserver', module='mssql-jdbc', version='6.4.0.jre8')
import com.microsoft.sqlserver.jdbc.SQLServerDriver
import groovy.sql.Sql

class MySQLClass implements Serializable {
    def dbconnection
    def dbURL
    def dbUsername
    def dbPassword
    def dbDriver

    MySQLClass(databaseConfig) {
        //Set any instance variables required.    
        this.dbURL = databaseConfig.sql.url
        this.dbUsername = databaseConfig.sql.username
        this.dbPassword = databaseConfig.sql.password
        this.dbDriver = databaseConfig.sql.driver
    }

    def getConnection() {
        return Sql.newInstance(dbURL,dbUsername,dbPassword,dbDriver)
    }
}

The databaseConfig object is configured as:

def databaseConfig = [
    url: 'jdbc:sqlserver://myhost:1433;databaseName=mydatabase',
    user: 'user',
    password: 'password',
    driver: 'com.microsoft.sqlserver.jdbc.SQLServerDriver'
]

When MyClass.getConnection() is called it fails with:

java.sql.SQLException: No suitable driver found for jdbc:sqlserver://myhost:1433;databaseName=mydatabase
    at java.sql.DriverManager.getConnection(DriverManager.java:689)
    at java.sql.DriverManager.getConnection(DriverManager.java:247)

Not sure why the '@Grab' and 'import' are not making the driver available.

I have tried adding:

@GrabConfig(systemClassLoader = true)

as suggest elsewhere but this cause the class to fail compilation - Jenkins starts to see it as a script rather than a class and so throws the duplicate class name error.

Any ideas Stackoverflow experts?

like image 720
gnuchu Avatar asked Apr 20 '18 13:04

gnuchu


1 Answers

You can register a driver manually

MySQLClass(databaseConfig) {
    //Set any instance variables required.    
    //...
    DriverManager.registerDriver(new SQLServerDriver())
}
like image 53
Vitalii Vitrenko Avatar answered Nov 01 '22 04:11

Vitalii Vitrenko