Skip to content

Instantly share code, notes, and snippets.

@aktau
Created January 4, 2012 14:07
Show Gist options
  • Select an option

  • Save aktau/1560196 to your computer and use it in GitHub Desktop.

Select an option

Save aktau/1560196 to your computer and use it in GitHub Desktop.
Akka SQL first try
package sql.akka
import akka.actor.Actor
import akka.dispatch.Future
import java.sql._
import sql._
// messages
case class ExecQuery(query: String, onComplete: ResultSet => Unit)
// information carriers
case class Process(user: String, submitted: Date, result: Future[Any])
object Database {
def using[A, B <: {def close(): Unit}] (closeable: B) (f: B => A): A = {
try { f(closeable) }
finally { closeable.close() }
}
}
class Database extends Actor {
import Database._
var queries: List[Process] = Nil
def receive = {
case ExecQuery(sql, onComplete) =>
// TODO: use using {} blocks
val future = Future {
// get connection from pool
using (Config.getConnection) { connection =>
using (connection.createStatement) { statement =>
using (statement.executeQuery(sql)) { resultset =>
onComplete(resultset)
}
}
}
}
queries ::= Process("default", new Date(System.currentTimeMillis()), future)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment