Skip to content

Instantly share code, notes, and snippets.

@johntbush
Created August 29, 2017 16:50
Show Gist options
  • Select an option

  • Save johntbush/334c505944046b5e265256bea7b1d61e to your computer and use it in GitHub Desktop.

Select an option

Save johntbush/334c505944046b5e265256bea7b1d61e to your computer and use it in GitHub Desktop.
getDataSource
def getDataSource(db: ConnectionName, write: Boolean, userName: String = user, pwd: String = password, isDomainLogon: Boolean = true, sendStringParametersAsUnicode: Option[Boolean] = None): HikariDataSource = {
val hconfig = new HikariConfig()
val url = baseUrl + db.sqlDns + "/" + db.databaseName
hconfig.setPoolName(db.connectionName + "_" + Utils.newUUID)
hconfig.setMaximumPoolSize(poolSize)
hconfig.setMinimumIdle(1)
hconfig.setJdbcUrl(url)
hconfig.setDriverClassName("net.sourceforge.jtds.jdbc.Driver")
hconfig.addDataSourceProperty("serverName", db.sqlDns)
hconfig.setReadOnly(write)
hconfig.setConnectionTestQuery("select 1")
val props = new Properties()
props.setProperty("cacheMetaData", "true")
props.setProperty("prepareSQL", "3")
props.setProperty("TDS", "8.0")
props.setProperty("appName", "fabric")
props.setProperty("sendStringParametersAsUnicode", "false")
props.setProperty("useLOBs", "false")
props.setProperty("useCursors", "true")
extraProps.split(";").map { prop =>
val pair = prop.split("=")
if (pair.length == 2) {
val name = pair(0)
val value = pair(1)
props.setProperty(name, value)
}
}
if (sendStringParametersAsUnicode.isDefined)
props.setProperty("sendStringParametersAsUnicode", sendStringParametersAsUnicode.get.toString)
hconfig.setConnectionTestQuery("select 1")
hconfig.addDataSourceProperty("port", "1433")
hconfig.addDataSourceProperty("databaseName", db.databaseName)
hconfig.addDataSourceProperty("user", userName)
hconfig.addDataSourceProperty("password", pwd)
if (isDomainLogon) props.setProperty("domain", "filex")
hconfig.setDataSourceProperties(props)
hconfig.setMetricRegistry(metricRegistry)
val propStr = props.map { case (name, value) => s"$name=$value" }.mkString(";")
logger.debug("attempting to connect to: " + url + s" with properties: $propStr")
new HikariDataSource(hconfig)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment