Slick defines here how to connect to a database using JNDI:
val db = Database.forName(jndiName: String)
I use the above to connect to a database in Play for Scala, defining the JNDI connection in application.conf
:
def read (jndi: String, code: Int) = {
val db = Database.forName(jndi)
val records = TableQuery[TableDB]
val action = records.filter(_.code === code).result
val future = db.run(action.asTry)
future.map{
case Success(s) =>
if (s.length>0)
Some(s(0))
else
None
case Failure(e) => throw new Exception ("Failure in read: " + e.getMessage)
}
}
Question is: how to disconnect from the JNDI resource? Simply db.close()
? Is there a way to implicitly close the connection when the read
method ends?