Skip to content

Rollback transaction after run failure #173

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 16, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion src/v1/transaction.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,13 @@ class Transaction {
}

_onError() {
this._onClose();
// rollback explicitly if tx.run failed, rollback
if (this._state == _states.ACTIVE) {
this.rollback();
} else {
// else just do the cleanup
this._onClose();
}
this._state = _states.FAILED;
}
}
Expand Down
64 changes: 64 additions & 0 deletions test/v1/transaction.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -236,4 +236,68 @@ describe('transaction', function() {
done();
});
});

it('should rollback when very first run fails', done => {
const tx1 = session.beginTransaction();
tx1.run('RETURN foo')
.catch(error => {
expectSyntaxError(error);

const tx2 = session.beginTransaction();
tx2.run('RETURN 1')
.then(result => {
expect(result.records[0].get(0).toNumber()).toEqual(1);
tx2.commit()
.then(() => done());
});
});
});

it('should rollback when some run fails', done => {
const tx1 = session.beginTransaction();
tx1.run('CREATE (:Person)')
.then(() => {
tx1.run('RETURN foo')
.catch(error => {
expectSyntaxError(error);

const tx2 = session.beginTransaction();
tx2.run('MATCH (n:Person) RETURN count(n)')
.then(result => {
expect(result.records[0].get(0).toNumber()).toEqual(0);
tx2.commit()
.then(() => done());
});
});
});
});

it('should fail to commit transaction that had run failures', done => {
const tx1 = session.beginTransaction();
tx1.run('CREATE (:Person)')
.then(() => {
tx1.run('RETURN foo')
.catch(error => {
expectSyntaxError(error);
tx1.commit()
.catch(error => {
const errorMessage = error.error;
const index = errorMessage.indexOf('Cannot commit statements in this transaction');
expect(index).not.toBeLessThan(0);

const tx2 = session.beginTransaction();
tx2.run('MATCH (n:Person) RETURN count(n)')
.then(result => {
expect(result.records[0].get(0).toNumber()).toEqual(0);
done();
});
});
});
});
});

function expectSyntaxError(error) {
const code = error.fields[0].code;
expect(code).toBe('Neo.ClientError.Statement.SyntaxError');
}
});