huber.duber
huber.duber

Reputation: 796

Neo4jClient Transaction Error

When I try to update an existing Node in Neo4JClient without using transaction, which has a Unique Constraint on Id and Name property, I receive this Exception:

CypherExecutionException: Node 6 already exists with label User and property "Name"=[Mike]

But when I try to update a Node in transaction, I receive this Exception:

Received an unexpected HTTP status when executing the request.

The response status was: 404 Not Found

The response from Neo4j (which might include useful detail!) was: {"results":[],"errors":[{"code":"Neo.ClientError.Transaction.UnknownId","message":"Unrecognized transaction id. Transaction may have timed out and been rolled back."}]}

My Code is like this:

using (var transaction = client.BeginTransaction())
{
    client.Cypher
        .Merge("(user:User { Id: {id}})")
        .OnCreate().Set("user = {user}")
        .OnMatch().Set("user = {user}")
        .WithParams(new { id = user.Id, user = user })
        .ExecuteWithoutResults();

    transaction.Commit();
}

My question is: Is there a way to get the actual error when using transaction, like I get one when I don't use transaction?

Thanks

EDIT 1:

It looks like the NULL Id in not being handled correctly. After drilling down, I managed to pull the actual error. The first exception in is:

errors = {[
  {
    "code": "Neo.ClientError.Statement.InvalidSemantics",
    "message": "Cannot merge node using null property value for Id"
  }
]}

The stack trace for this exception is:

at Neo4jClient.Serialization.CypherJsonDeserializer1.GetRootResultInTransaction(JObject root) in [PATH_HIIDEN]\Neo4jClient\Serialization\CypherJsonDeserializer.cs:line 316 at Neo4jClient.Serialization.CypherJsonDeserializer1.CheckForErrorsInTransactionResponse(String content) in [PATH_HIIDEN]\Neo4jClient\Serialization\CypherJsonDeserializer.cs:line 291 at Neo4jClient.GraphClient.<>c__DisplayClass84_01.<PrepareCypherRequest>b__0(Task1 responseTask) in [PATH_HIIDEN]\Neo4jClient\GraphClient.cs:line 933 at System.Threading.Tasks.ContinuationResultTaskFromResultTask`2.InnerInvoke() at System.Threading.Tasks.Task.Execute()

And after that there is the exception with 404 NOT Found error. For that the stack trace is:

at Neo4jClient.Execution.HttpResponseMessageExtensions.EnsureExpectedStatusCode(HttpResponseMessage response, String commandDescription, HttpStatusCode[] expectedStatusCodes) in [PATH_HIIDEN]\Neo4jClient\Execution\HttpResponseMessageExtensions.cs:line 41 at Neo4jClient.Execution.HttpResponseMessageExtensions.EnsureExpectedStatusCode(HttpResponseMessage response, HttpStatusCode[] expectedStatusCodes) in [PATH_HIIDEN]\Neo4jClient\Execution\HttpResponseMessageExtensions.cs:line 14 at Neo4jClient.Execution.ResponseBuilder.<>c__DisplayClass21_0.b__0(Task1 requestTask) in [PATH_HIIDEN]\Neo4jClient\Execution\ResponseBuilder.cs:line 127 at System.Threading.Tasks.ContinuationResultTaskFromResultTask2.InnerInvoke() at System.Threading.Tasks.Task.Execute()

Can this error be fixed in the next release please?

EDIT 2

using (var transaction = client.BeginTransaction())
{
    manifest.Users = client.Cypher
        .Merge("(n:User { Id: '8be0b8fd-c433-44d3-a7e2-3f0d1a03fefa'}) " +
            "ON CREATE " +
            "SET n = { " +
                "Id: '8be0b8fd-c433-44d3-a7e2-3f0d1a03fefa', " +
                "UserName: 'test.user', " +
                "Name: 'Test User', " +
                "Active: true " +
            "} " +
            "ON MATCH " +
            "SET n = { " +
                "Id: '8be0b8fd-c433-44d3-a7e2-3f0d1a03fefa', " +
                "UserName: 'test.user', " +
                "Name: 'Test User', " +
                "Active: true " + 
            "}")
        .Return(n => n.As<User>())
        .Results
        .ToList();
    transaction.Commit();
}

Meaning fluent Api is causing the error.

Upvotes: 1

Views: 1977

Answers (1)

Charlotte Skardon
Charlotte Skardon

Reputation: 6270

So the problem is that in Neo4j 2.2.6 a change was introduced whereby any ClientError (Constraint Violation etc) resulted in an automatic Rollback of a transaction on the server.

Neo4jClient already did an auto-rollback on error, and continued to do so. With 2.2.6 this resulted in a 404 error as the transaction was no longer there (previous versions were fine).

So, I've made a fix and published an updated Nuget package, so version 1.1.0.12 of Neo4jClient will now give you the correct response.

Upvotes: 0

Related Questions