Last active
December 14, 2015 08:28
-
-
Save gamlerhart/5057658 to your computer and use it in GitHub Desktop.
Scala ADBCJ Getting Started
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
val database: DatabaseAccess = Database("adbcj:pooled:mysql://localhost/adbcj-demo", | |
"adbcj", | |
"adbcj-pwd", | |
StandardProperties.CAPTURE_CALL_STACK->"true") // Enable debug stack traces |
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
name := "Scala ADBCJ Demo" | |
version := "1.0" | |
scalaVersion := "2.10.0" | |
libraryDependencies += "info.gamlor.adbcj" %% "scala-adbcj" % "0.6-SNAPSHOT" | |
libraryDependencies += "org.adbcj" % "adbcj-connection-pool" % "0.6-SNAPSHOT" | |
libraryDependencies += "org.adbcj" % "mysql-async-driver" % "0.6-SNAPSHOT" | |
resolvers += "Gamlor-Repo" at "https://raw.github.com/gamlerhart/gamlor-mvn/master/snapshots" |
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
// connect() will return a future, which contains the connection | |
// You need to close the connection yourself. | |
val simpleConnectionFuture = database.connect() | |
// We can use the Scala for construct to deal nicely with futures | |
val futureForWholeOperation = for { | |
connection <-simpleConnectionFuture | |
closeDone <- connection.close() | |
} yield "Done" |
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
// Note the "pooled" prefix, for using the connection pool | |
val database: DatabaseAccess = Database("adbcj:pooled:mysql://localhost/adbcj-demo", | |
"adbcj", | |
"adbcj-pwd") |
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
org.adbcj.DbException: 42S02Table 'adbcj-demo.thistabledoesnotexist' doesn't exis | |
at org.adbcj.support.stacktracing.StackTraceCapturing$CaputureByDefault.capture(StackTraceCapturing.java:20) | |
at org.adbcj.support.stacktracing.StackTraceCapturing.defaultCapture(StackTraceCapturing.java:11) | |
at org.adbcj.support.stacktracing.StackTracingOptions$1.captureStacktraceAtEntryPoint(StackTracingOptions.java:12) | |
at org.adbcj.support.DefaultDbFuture.<init>(DefaultDbFuture.java:45) | |
at org.adbcj.support.DefaultDbSessionFuture.<init>(DefaultDbSessionFuture.java:50) | |
at org.adbcj.mysql.codec.MySqlRequests.executeQuery(MySqlRequests.java:28) | |
at org.adbcj.mysql.codec.MySqlConnection.executeQuery(MySqlConnection.java:108) | |
at org.adbcj.mysql.codec.MySqlConnection.executeQuery(MySqlConnection.java:102) | |
at org.adbcj.connectionpool.PooledConnection.executeQuery(PooledConnection.java:78) | |
at info.gamlor.db.DBConnection$$anonfun$executeQuery$1.apply(DBConnection.scala:109) | |
at info.gamlor.db.DBConnection$$anonfun$executeQuery$1.apply(DBConnection.scala:109) | |
at info.gamlor.db.FutureConversions$class.completeWithAkkaFuture(FutureConversions.scala:20) | |
at info.gamlor.db.DBConnection.completeWithAkkaFuture(DBConnection.scala:31) | |
at info.gamlor.db.DBConnection.executeQuery(DBConnection.scala:109) | |
--->at info.gamlor.adbcj.scalademo.MainDemo$$anonfun$8.apply(MainDemo.scala:99) | |
... // RIGHT Location | |
Caused by: org.adbcj.mysql.codec.MysqlException: 42S02Table 'adbcj-demo.thistabledoesnotexist' doesn't exist | |
at org.adbcj.mysql.codec.packets.ErrorResponse.toException(ErrorResponse.java:49) | |
at org.adbcj.mysql.codec.decoding.ExpectQueryResult.handleError(ExpectQueryResult.java:31) | |
at org.adbcj.mysql.codec.decoding.ResponseStart.parse(ResponseStart.java:33) | |
at org.adbcj.mysql.codec.decoding.AcceptNextResponse.parse(AcceptNextResponse.java:26) | |
at org.adbcj.mysql.codec.MySqlClientDecoder.doDecode(MySqlClientDecoder.java:96) | |
at org.adbcj.mysql.codec.MySqlClientDecoder.decode(MySqlClientDecoder.java:67) | |
at org.adbcj.mysql.netty.Decoder.decode(MysqlConnectionManager.java:177) | |
at io.netty.handler.codec.ByteToMessageDecoder.callDecode(ByteToMessageDecoder.java:113) |
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
val txDoneFuture = database.withTransaction{ | |
connection => | |
// Same goes for transactions | |
val schemaCreation = connection.executeQuery("""CREATE TABLE IF NOT EXISTS posts(\n | |
id int NOT NULL AUTO_INCREMENT,\n | |
title varchar(255) NOT NULL,\n | |
ontent TEXT NOT NULL,\n | |
PRIMARY KEY (id)\n | |
) ENGINE = INNODB;""") | |
val firstPost =connection.executeUpdate("INSERT INTO posts(title,content) VALUES('The Title','TheContent')") | |
val secondPost =connection.executeUpdate("INSERT INTO posts(title,content) VALUES('Second Title','More Content')") | |
val thirdPost =connection.executeUpdate("INSERT INTO posts(title,content) VALUES('Third Title','Even More Content')") | |
// we want to await for all operations at the end | |
for { | |
schemaDone <-schemaCreation | |
postOneDone <-firstPost | |
postTwoDone <-secondPost | |
postThreeDone <-thirdPost | |
} yield "All Done" | |
} |
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
org.adbcj.mysql.codec.MysqlException: 42S02Table 'adbcj-demo.thistabledoesnotexist' doesn't exis | |
at org.adbcj.mysql.codec.packets.ErrorResponse.toException(ErrorResponse.java:49) | |
at org.adbcj.mysql.codec.decoding.ExpectQueryResult.handleError(ExpectQueryResult.java:31) | |
at org.adbcj.mysql.codec.decoding.ResponseStart.parse(ResponseStart.java:33) | |
at org.adbcj.mysql.codec.decoding.AcceptNextResponse.parse(AcceptNextResponse.java:26) | |
at org.adbcj.mysql.codec.MySqlClientDecoder.doDecode(MySqlClientDecoder.java:96) | |
at org.adbcj.mysql.codec.MySqlClientDecoder.decode(MySqlClientDecoder.java:67) | |
at org.adbcj.mysql.netty.Decoder.decode(MysqlConnectionManager.java:177) | |
at io.netty.handler.codec.ByteToMessageDecoder.callDecode(ByteToMessageDecoder.java:113) | |
... |
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
val postsDataFuture= database.withTransaction{ | |
connection => | |
val preparingStmt =connection.prepareQuery("SELECT * FROM posts WHERE title LIKE ?") | |
val postsFuture = preparingStmt.flatMap(stmt=>stmt.execute("Third Title")) | |
postsFuture onSuccess { | |
case rs:DBResultList => { | |
for (row <- rs){ | |
System.out.println("ID: "+row("ID").getLong()+" with title "+row("title").getString()); | |
} | |
} | |
} | |
postsFuture | |
} |
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
val postsFuture =connection.executeQuery("SELECT * FROM thisTableDoesNotExist") | |
postsFuture onFailure{ | |
case ex:DbException =>{ | |
ex.printStackTrace() | |
} | |
} |
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
val postsDataFuture = database.withTransaction{ | |
connection => | |
val postsFuture =connection.executeQuery("SELECT * FROM posts") | |
postsFuture onSuccess { | |
case rs:DBResultList => { | |
for (row <- rs){ | |
System.out.println("ID: "+row("ID").getLong()+" with title "+row("title").getString()); | |
} | |
} | |
} | |
postsFuture | |
} |
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
val doneWithConnection:Future[DBResultList] = database.withConnection{ | |
connection => | |
// do something with the connection | |
// you need to return a future, because everything is asynchrous | |
// so the connection can only be closed when everything is done | |
connection.executeQuery("SELECT 1") | |
} | |
val doneWithTransactionAndConnection:Future[DBResultList] = database.withTransaction{ | |
connection => | |
// Same goes for transactions | |
connection.executeQuery("SELECT 1") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment