Skip to content

Instantly share code, notes, and snippets.

@tjweir
Created February 8, 2014 14:00
Show Gist options
  • Select an option

  • Save tjweir/8884185 to your computer and use it in GitHub Desktop.

Select an option

Save tjweir/8884185 to your computer and use it in GitHub Desktop.
private object DBVendor extends Logger {
def apply(prefix: String) = {
List("driver", "url", "user", "password").flatMap(str => Props.get(prefix + ".db." + str)) match {
case driver :: url :: user :: password :: Nil =>
new DBVendor(driver, url, user, password, Props.get(prefix + ".db.ssl"), Props.get(prefix + ".db.sslfactory"))
case _ =>
throw new Exception("The database connection properties could not be found.")
}
}
}
private class DBVendor(
driverName: String,
dbUrl: String,
username: String,
password: String,
ssl: Box[String],
sslfactory: Box[String]
) extends ProtoDBVendor {
LiftRules.unloadHooks.append(this.closeAllConnections_! _)
override protected def maxPoolSize = 10
override protected def doNotExpandBeyond = 20
protected def createOne: Box[Connection] = {
try {
Class.forName(driverName)
Full(DriverManager.getConnection(dbUrl, new java.util.Properties {
setProperty("user", username)
setProperty("password", password)
for (ssl <- ssl; sslfactory <- sslfactory) {
setProperty("ssl", ssl)
setProperty("sslfactory", sslfactory)
}
}))
} catch {
case e: Exception =>
Empty
}
}
override protected def testConnection(conn: Connection) {
//conn.setAutoCommit(false) // default - doesn't seem to check anything
var stmt: Box[Statement] = Empty
var rs: Box[ResultSet] = Empty
try {
stmt = Full(conn.createStatement())
stmt.foreach(s => { rs = Full(s.executeQuery("SELECT 1")) })
for (r <- rs if !r.next()) {
throw new Exception("Invalid result from connection test.")
}
} catch {
case e : Exception =>
throw new Exception("An error occurred while validating the connection.")
} finally {
stmt.foreach(_.close())
rs.foreach(_.close())
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment