Connecting to External Databases

You may want to connect to a database in your workflow function scripts, for instance read data from an external source in a validator.

The easiest method is to use groovy sql. But, there is a gotcha or two.

JDBC drivers must be loaded by the system classloader, and furthermore the DriverManager will make checks that the driver class is accessible from the classloader of the calling class. In an OSGi environment this causes problems.

So, the following code will not work:

import groovy.sql.Sql

Sql.newInstance("jdbc:postgresql://localhost:5432/jira_62", "jiradb", "")

you will get an error: No suitable driver found for jdbc:postgresql://localhost:5432/jira_62

Instead, manually load the driver class and create the connection:

import groovy.sql.Sql
import java.sql.Driver

def driver = Class.forName('org.postgresql.Driver').newInstance() as Driver (1)

def props = new Properties()
props.setProperty("user", "devtools") (2)
props.setProperty("password", "devtools")

def conn = driver.connect("jdbc:postgresql://localhost:5432/jira_6.4.6", props) (3)
def sql = new Sql(conn)

try {
    sql.eachRow("select count(*) from jiraissue") {
        log.debug(it)
    }
} finally {
    sql.close()
    conn.close()
}
1 check the database driver class
2 and your database connection credentials
3 and finally the JDBC connection string

Driver jar files should be placed in your tomcat/lib directory, eg <jira.install>/lib, but JIRA already ships with the major drivers.

Querying the Current JIRA Database

You can execute a query against the current JIRA database, for instance in reports. Here’s how:

import com.atlassian.jira.component.ComponentAccessor
import groovy.sql.Sql
import org.ofbiz.core.entity.ConnectionFactory
import org.ofbiz.core.entity.DelegatorInterface

import java.sql.Connection

def delegator = (DelegatorInterface) ComponentAccessor.getComponent(DelegatorInterface)
String helperName = delegator.getGroupHelperName("default")

def sqlStmt = """
    SELECT     project.pname, COUNT(*) AS kount
    FROM       project
               INNER JOIN jiraissue ON project.ID = jiraissue.PROJECT
    GROUP BY project.pname
    ORDER BY kount DESC
"""

Connection conn = ConnectionFactory.getConnection(helperName)
Sql sql = new Sql(conn)

try {
    StringBuffer sb = new StringBuffer()
    sql.eachRow(sqlStmt) {
        sb << "${it.pname}\t${it.kount}\n"
    }
    log.debug sb.toString()
}
finally {
    sql.close()
}
Direct database update queries are not recommended in JIRA. Instead, we recommend adding or modifying data using JIRA’s APIs (via ScriptRunner). If you absolutely must modify data in your database via direct database queries, always back up your data before performing any modification to the database.

For how-to questions please ask on Atlassian Answers where there is a very active community. Adaptavist staff are also likely to respond there.

Ask a question about ScriptRunner for JIRA, for for Bitbucket Server, or for Confluence.