Skip to content

Instantly share code, notes, and snippets.

@adamretter
Created September 5, 2012 16:39
Show Gist options
  • Save adamretter/3639589 to your computer and use it in GitHub Desktop.
Save adamretter/3639589 to your computer and use it in GitHub Desktop.
Partial Function application for JDBC Connections in Scala
def withConnection[T](ds: ComboPooledDataSource, fn : Connection => T) : T = {
var optConn : Option[Connection] = Option.empty;
try {
val conn = ds.getConnection
optConn = Some(conn);
fn(conn)
} finally {
if (!optConn.isEmpty) {
optConn.get.close
}
}
}
withConnection(ds, myFunction("some param"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment