ScalikeJDBC-Async provides non-blocking APIs to talk with PostgreSQL and MySQL in the JDBC way.
This library is built with postgrsql-async and mysql-async,incredible works by @mauricio.
ScalikeJDBC:
https://github.com/scalikejdbc/scalikejdbc
ScalikeJDBC is a tidy SQL-based DB access library for Scala developers. This library naturally wraps JDBC APIs and provides you easy-to-use APIs.
ScalikeJDBC-Async is still in the beta stage. If you don't have motivation to investigate or fix issues by yourself, we recommend you waiting until stable version release someday.
- PostgreSQL
- MySQL
Add scalikejdbc-async
to your dependencies.
libraryDependencies ++= Seq(
"org.scalikejdbc" %% "scalikejdbc-async" % "0.6.+",
"com.github.mauricio" %% "postgresql-async" % "0.2.+",
"com.github.mauricio" %% "mysql-async" % "0.2.+",
"org.slf4j" % "slf4j-simple" % "1.7.+" // slf4j implementation
)
If you're a Play2 user, use play-plugin too!
val appDependencies = Seq(
"org.scalikejdbc" %% "scalikejdbc-async" % "0.6.+",
"org.scalikejdbc" %% "scalikejdbc-async-play-plugin" % "0.6.+",
"com.github.mauricio" %% "postgresql-async" % "0.2.+"
)
import scalikejdbc._, async._
import scala.concurrent._, duration._, ExecutionContext.Implicits.global
// set up connection pool (that's all you need to do)
AsyncConnectionPool.singleton("jdbc:postgresql://localhost:5432/scalikejdbc", "sa", "sa")
// create a new record within a transaction
val created: Future[Company] = AsyncDB.localTx { implicit tx =>
for {
company <- Company.create("ScalikeJDBC, Inc.", Some("http://scalikejdbc.org/"))
seratch <- Programmer.create("seratch", Some(company.id))
gakuzzzz <- Programmer.create("gakuzzzz", Some(company.id))
xuwei_k <- Programmer.create("xuwei-k", Some(company.id))
} yield company
}
Await.result(created, 5.seconds)
created.foreach { newCompany: Company =>
// delete a record and rollback
val withinTx: Future[Unit] = AsyncDB.localTx { implicit tx =>
for {
restructuring <- Programmer.findAllBy(sqls.eq(p.companyId, newCompany.id)).map {
programmers => programmers.foreach(_.destroy())
}
dissolution <- newCompany.destroy()
failure <- sql"Just joking!".update.future
} yield ()
}
try Await.result(withinTx, 5.seconds)
catch { case e: Exception => log.debug(e.getMessage, e) }
// rollback expected
val company = AsyncDB.withPool { implicit s =>
Company.find(newCompany.id)
}
Await.result(company, 5.seconds)
val found: Option[Company]= company.value.get.get
found.isDefined should be(true)
}
Transactional queries should be executed in series. You cannot use Future.traverse
or Future.sequence
.
See the play-sample project:
https://github.com/scalikejdbc/scalikejdbc-async/blob/master/play-sample
ScalikeJDBC-Async and postgrsql-async and mysql-async basically works fine. However, to be honest, ScalikeJBDC-Async doesn't have much of a record of production applications.
Yes, it's possible. See this example.
This library is still in alpha stage. If this library becomes stable enough, it will be merged into the ScalikeJDBC project.
Before sending pull requests, please prepare the following DB settings and write tests to ensure that your fixes work as expected.
- PostgreSQL
url: jdbc:postgresql://localhost:5432/scalikejdbc
username: sa
password: sa
url: jdbc:postgresql://localhost:5432/scalikejdbc2
username: sa
password: sa
- MySQL
url: jdbc:mysql://localhost:3306/scalikejdbc
username: sa
password: sa
Published binary files have the following copyright:
Copyright 2013 - 2014 scalikejdbc.org
Apache License, Version 2.0
http://www.apache.org/licenses/LICENSE-2.0.html