Skip to content

Instantly share code, notes, and snippets.

@KirillGrishin
Created January 13, 2014 03:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save KirillGrishin/8394450 to your computer and use it in GitHub Desktop.
Save KirillGrishin/8394450 to your computer and use it in GitHub Desktop.
An attempt to create a transaction wrapper for Neo4j in Scala. I think there is one problem with transactions though. Neo4j transactions are thread global, which means that there is only one transaction per thread. But in Scala, and especially in Play framework, the threads would be reused and multiple pieces of code may reuse the same thread. T…
/**
* @param operation a `by name` parameter which represents the db operation
* @tparam T
* @return
*/
def withTx[T](operation: => T): Try[T] = synchronized {
// Start transaction
val tx = db.beginTx()
// Try the operation
val result = Try { operation }
// If operation was a success, mark transaction as successful; if transaction is not marked as
// successful when it is closes, it is considered a failure
result match {
case Success(_) => tx.success()
case _ => ()
}
// Close transaction
tx.close()
// Return result
result
}
val res = withTx {
val res = engine.execute("... cypher query ...")
val paths = res.columnAs[org.neo4j.graphdb.Path]("p").toList
paths map { path =>
// Do something with the path here
...
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment