Skip to content
This repository has been archived by the owner on Jan 17, 2024. It is now read-only.

Getting Started

Roman Stoffel edited this page Mar 25, 2014 · 8 revisions

The example code can be found here: https://github.com/gamlerhart/scala-adbcj/tree/master/demo

#Adding Dependency Using sbt add first the snapshot repository. Currently only snapshots are available:

resolvers += "Gamlor-Repo" at "https://github.com/gamlerhart/gamlor-mvn/raw/master/snapshots"

Then add the Scala ADBCJ dependency, the ADBCJ connection pool and the needed driver:

libraryDependencies += "info.gamlor.adbcj" %% "scala-adbcj" % "0.7.2-SNAPSHOT"

libraryDependencies += "org.adbcj" % "adbcj-connection-pool" % "0.7.2-SNAPSHOT"

libraryDependencies += "org.adbcj" % "mysql-async-driver" % "0.7.2-SNAPSHOT"

#Getting Started First we create a database connection object, which is our entry. Usually you should keep one of such a instance per database in your application. After that we can create a connection:

// connect() will return a future, which contains the conneciton
// 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"

Or we can use the withConnection / withTransaction utitlity, which close the connections for us. Note that those functions expect a closure which returns a future. Since everything runs asynchronously, they only can close the connection when everything is done:

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")
	}

	database.withTransaction{
		connection =>
		// Same goes for transactions
		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;""")
}

#Pipeline You can 'pipeline' queries and inserts. Send queries and updates directly to the database. They will be sent immediately, avoiding waiting on the database side. Then await for the results:

database.withTransaction{
	connection =>
	// Try to send all queries, statements etc in one go.
	// Then collect the results at the end, or when needed for a intermediate step
	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')")

	val allDone = for {
		postOne <- firstPost
		postTwo <- secondPost
		postThree <- thirdPost
	} yield "All DONE"

	allDone
}

#Queries Queries work the same as updates. The result sets in the futures are immutable. So you can safely close the connection, once you have the result.

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
}
Clone this wiki locally