Created
January 13, 2014 03:54
-
-
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…
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
/** | |
* @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