Skip to content

Instantly share code, notes, and snippets.

@gamlerhart
Created May 17, 2012 15:20
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gamlerhart/2719603 to your computer and use it in GitHub Desktop.
Save gamlerhart/2719603 to your computer and use it in GitHub Desktop.
AsyncJDBC
myapp{
async-jdbc{
url = "adbcj:mysql://localhost/employees"
username = "root"
password = ""
}
}
resolvers += "Gamlor-Repo" at "https://github.com/gamlerhart/gamlor-mvn/raw/master/snapshots"
libraryDependencies += "com.typesafe.akka" % "akka-actor" % "2.0"
libraryDependencies += "info.gamlor.akkaasync" %% "akka-dbclient" % "1.0-SNAPSHOT"
libraryDependencies += "org.adbcj" % "adbcj-api" % "0.3-SNAPSHOT"
libraryDependencies += "org.adbcj" % "mysql-async-driver" % "0.3-SNAPSHOT"
// grab the extension
val dbSupport = Database(actorSystem)
val result = for{
// open the connection asynchronously
connection <- dbSupport.connect()
// Execute a query quest
newestEmployee <- connection.executeQuery(
"SELECT first_name,last_name,hire_date FROM employees " +
"ORDER BY hire_date DESC " +
"LIMIT 0,5")
// When the results arrived, close the connection
_ <- connection.close()
} yield newestEmployee
// Process the results
result.onSuccess{
case resultSet =>{
resultSet.foreach{
row => println(row("first_name").getString
+ " "+row("first_name").getString
+ " since " + row("hire_date").getString )
}
}
}.onFailure{
case e:Exception =>{
e.printStackTrace()
}
}
val resultAsString = for {
// open the connection asynchronously
connection <- dbSupport.connect()
// Execute a query quest
dataAsString <- connection.executeQuery(
"SELECT first_name,last_name,hire_date FROM employees " +
"ORDER BY hire_date DESC " +
"LIMIT 0,5", "") {
// Instead of creating a result set, we also can directly react
// as the data streams in
case StartRow(jsonToBuild) => jsonToBuild + "-"
case AValue(value, jsonToBuild) => jsonToBuild + "," + value.getString
case EndRow(jsonToBuild) => jsonToBuild + "\n"
}
// When the results arrived, close the connection
_ <- connection.close()
} yield dataAsString
// Process the results
resultAsString.onSuccess {
case stringData => print(stringData)
}.onFailure {
case e: Exception => {
e.printStackTrace()
}
}
val result = for {
// open the connection asynchronously
connection <- dbSupport.connect()
_ <- connection.beginTransaction()
insertedInfo <- connection.executeUpdate(
"INSERT INTO employees (emp_no, birth_date, first_name, last_name, gender, hire_date) " +
"VALUES (42, '1986-05-07', 'Roman', 'Stoffel', 'M', '2012-05-17')")
statement <- connection.prepareQuery("SELECT first_name FROM employees WHERE emp_no = ?")
queryResult <- statement.execute(42)
_ <- connection.rollback()
_ <- connection.close()
} yield queryResult
val result = Database(actorSystem)
.withConnection{
connection =>connection.executeQuery(
"SELECT first_name,last_name,hire_date FROM employees " +
"ORDER BY hire_date DESC " +
"LIMIT 0,5")
}
connection.withTransaction{
txConn =>
txConn.executeQuery(
"SELECT first_name,last_name,hire_date FROM employees " +
"ORDER BY hire_date DESC " +
"LIMIT 0,5")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment