Created
January 4, 2012 14:07
-
-
Save aktau/1560196 to your computer and use it in GitHub Desktop.
Akka SQL first try
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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