10

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?

Dan Wilson
  • 3,937
  • 2
  • 17
  • 27
gnuchu
  • 1,496
  • 13
  • 21

1 Answers1

4

You can register a driver manually

MySQLClass(databaseConfig) {
    //Set any instance variables required.    
    //...
    DriverManager.registerDriver(new SQLServerDriver())
}
Vitalii Vitrenko
  • 9,763
  • 4
  • 43
  • 62