Skip to content

'beginning implicit transaction' cannot be done when a session is in the 'IN_TRANSACTION' state #166

Closed
@cipacda

Description

@cipacda
  • Neo4j version: 3.0.6
  • Operating system: Windows 8 and 10
  • API/Driver: Neo4j Javascript driver - [email protected]

##Steps to reproduce##
Run two sessions updating the same node at the same time.

What I am doing is having an asynchronous neo4j endpoint that returns before it finishes adding a relation to a specific node and then immediately I have another synchronous endpoint called that adds another relation to that node, both actions being executed inside a transaction.

##Expected behavior##
To get a deadlock error on the second query and then when I am retrying the transaction for it to succeed.

##Actual behavior##
I am getting a deadlock on the 4th query inside my transaction and then when I am retrying the transaction (by creating a new one with the same queries) I get the following: {"code":"Neo.ClientError.Request.Invalid","message":"'beginning implicit transaction' cannot be done when a session is in the 'IN_TRANSACTION' state."}

##Additional comments##
This is my code with sensitive data taken out:

function transactionFunction(txn: any)  {

   return txn.run(query1)
     .then(_ => { return txn.run(query2); })
     .then(_ => { return txn.run(query3); })
     .then(_ => { return txn.run(query4); })
}

export function runInsideTransaction(session, runFunction: (txn) => Promise<any>): Promise<any> {
    function retry(times: number): Promise<any> {
        car result;
        var txn = createTransaction(session);
        return new Promise((resolve, reject) => {
            runFunction(txn)
                .then(response => {
                    result = response;
                    return commitTransaction(txn);
                })
                .then(() => {
                    resolve(result);
                })
                .catch((error) => {
                    // We should retry deadlocks
                    var code = (error.fields && error.fields[0]) ? error.fields[0].code : null;
                    if (times > 0 && code && code.indexOf('Neo.TransientError.Transaction.DeadlockDetected') > -1) {
                            retry(times - 1)
                                .then(response => {
                                    resolve(response);
                                })
                                .catch(e => {
                                    reject(e);
                                });
                        });
                    } else {
                        reject(error);
                    }
                });
        });
    }
    
    return retry(5);
}

runInsideTransaction(session, transactionFunction);

I am creating a session that outside that is only used here and I have logged everything and the _hasTx property of the session is false when we try the transaction the second time.

I have looked at the source code of the javascript driver and did not find this error which makes me believe this is actually a Neo4J bug?

I have reported this on the neo4j repo as well.

Anyone has any thoughts?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions