Created
May 17, 2012 15:20
-
-
Save gamlerhart/2719603 to your computer and use it in GitHub Desktop.
AsyncJDBC
This file contains 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
myapp{ | |
async-jdbc{ | |
url = "adbcj:mysql://localhost/employees" | |
username = "root" | |
password = "" | |
} | |
} |
This file contains 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
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" |
This file contains 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
// 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() | |
} | |
} |
This file contains 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
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() | |
} | |
} |
This file contains 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
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 |
This file contains 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
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") | |
} |
This file contains 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
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