Skip to content

Commit 92a946d

Browse files
authored
Merge pull request #412 from lutovich/1.7-tx-rollback-after-failure
Allow rollback transaction after failure
2 parents cf14dcc + 4e177a1 commit 92a946d

File tree

5 files changed

+35
-4
lines changed

5 files changed

+35
-4
lines changed

src/v1/internal/stream-observer.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,14 @@ class StreamObserver {
113113
this._fieldKeys = [];
114114
}
115115

116+
/**
117+
* Mark this observer as if it has completed with no metadata.
118+
*/
119+
markCompleted() {
120+
this._fieldKeys = [];
121+
this._tail = {};
122+
}
123+
116124
/**
117125
* Will be called on errors.
118126
* If user-provided observer is present, pass the error

src/v1/transaction.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -177,9 +177,7 @@ let _states = {
177177
return {result: _newDummyResult(observer, "COMMIT", {}), state: _states.FAILED};
178178
},
179179
rollback: (connectionHolder, observer) => {
180-
observer.onError({error:
181-
"Cannot rollback transaction, because previous statements in the " +
182-
"transaction has failed and the transaction has already been rolled back."});
180+
observer.markCompleted();
183181
return {result: _newDummyResult(observer, "ROLLBACK", {}), state: _states.FAILED};
184182
},
185183
run: (connectionHolder, observer, statement, parameters) => {

test/internal/stream-observer.test.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,18 @@ describe('StreamObserver', () => {
172172
streamObserver.onCompleted({key: 42});
173173
});
174174

175+
it('should mark as completed', done => {
176+
const streamObserver = new StreamObserver();
177+
streamObserver.markCompleted();
178+
179+
streamObserver.subscribe({
180+
onCompleted: metadata => {
181+
expect(metadata).toEqual({});
182+
done();
183+
}
184+
});
185+
});
186+
175187
});
176188

177189
function newStreamObserver() {

test/v1/session.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ describe('session', () => {
114114
const session = driver.session();
115115
const tx = session.beginTransaction();
116116
tx.run('INVALID QUERY').catch(() => {
117-
tx.rollback().catch(() => {
117+
tx.rollback().then(() => {
118118
session.close(() => {
119119
driver.close();
120120
done();

test/v1/transaction.test.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -527,6 +527,19 @@ describe('transaction', () => {
527527
tx.rollback().then(() => done());
528528
});
529529

530+
it('should allow rollback after failure', done => {
531+
const tx = session.beginTransaction();
532+
tx.run('WRONG QUERY')
533+
.then(() => done.fail('Expected to fail'))
534+
.catch(error => {
535+
expectSyntaxError(error);
536+
537+
tx.rollback()
538+
.catch(error => done.fail(error))
539+
.then(() => done());
540+
});
541+
});
542+
530543
function expectSyntaxError(error) {
531544
expect(error.code).toBe('Neo.ClientError.Statement.SyntaxError');
532545
}

0 commit comments

Comments
 (0)