问题
I am using neo4j 2.1.7 with java.
try(Transaction transaction = this.graphDatabaseService.beginTx())
{
Node user = this.graphDatabaseService.createNode();
user.setProperty("userId", userId);
transaction.failure();
}
So I am getting the object of GraphDatabaseService and creating a new transaction and marking it to rollback. According to their javadocs:
void failure()
Marks this transaction as failed, which means that it will unconditionally be rolled back when close() is called. Once this method has been invoked, it doesn't matter if success() is invoked afterwards -- the transaction will still be rolled back.
But I see that the node gets created no matter what. I tried throwing an exception
. I also tried not calling transaction.success()
at all. Still I see that the changes get committed and not rolled back. I am not sure of this behaviour and would like an explanation. Thanks.
If you must know, I am trying to build a commit() function with nested transactions such that if any operation fail within the inner transactions, the parent transaction must fail too. However, in the process I found that no matter what I do, the transactions are getting committed.
Update 1:
The embedded version of neo4j works fine. The rest version is causing this trouble. I am using this package for rest:
<dependency>
<groupId>org.neo4j</groupId>
<artifactId>neo4j-rest-graphdb</artifactId>
<version>2.0.1</version>
</dependency>
回答1:
No transactions over REST, at least not for that old version.
There are only transactions over HTTP with the new Cypher Endpoint.
That library is discontinued, I recommend that you use e.g. the JDBC driver or the new implementation that comes with Spring Data REST.
来源:https://stackoverflow.com/questions/29319464/neo4j-transaction-not-rolling-back