Skip to content

Commit 3205931

Browse files
committed
Make sure session does not have bookmark after unsuccessful tx
Session's last bookmark should not be defined after explicit transaction fails or is rolled back.
1 parent e4289d7 commit 3205931

File tree

2 files changed

+70
-8
lines changed

2 files changed

+70
-8
lines changed

src/v1/transaction.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ class Transaction {
3131
* @param {function()} onClose - Function to be called when transaction is committed or rolled back.
3232
* @param errorTransformer callback use to transform error
3333
* @param bookmark optional bookmark
34+
* @param onBookmark callback invoked when new bookmark is produced
3435
*/
3536
constructor(connectionPromise, onClose, errorTransformer, bookmark, onBookmark) {
3637
this._connectionPromise = connectionPromise;
@@ -129,10 +130,8 @@ class _TransactionStreamObserver extends StreamObserver {
129130

130131
onCompleted(meta) {
131132
super.onCompleted(meta);
132-
let bookmark = meta.bookmark;
133-
if (bookmark) {
134-
this._tx._onBookmark(bookmark);
135-
}
133+
const bookmark = meta.bookmark;
134+
this._tx._onBookmark(bookmark);
136135
}
137136

138137
serverMeta() {

test/v1/transaction.test.js

Lines changed: 67 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -219,9 +219,7 @@ describe('transaction', function() {
219219
});
220220

221221
it('should provide bookmark on commit', function (done) {
222-
//bookmarking is not in 3.0
223-
if (!server) {
224-
done();
222+
if (neo4jVersionOlderThan31(done)) {
225223
return;
226224
}
227225

@@ -232,11 +230,71 @@ describe('transaction', function() {
232230
tx.run("CREATE (:TXNode2)");
233231
tx.commit()
234232
.then(function () {
235-
expect(session.lastBookmark()).toBeDefined();
233+
expectValidLastBookmark(session);
236234
done();
237235
});
238236
});
239237

238+
it('should have no bookmark when tx is rolled back', function (done) {
239+
if (neo4jVersionOlderThan31(done)) {
240+
return;
241+
}
242+
243+
expect(session.lastBookmark()).not.toBeDefined();
244+
const tx1 = session.beginTransaction();
245+
246+
tx1.run('CREATE ()').then(() => {
247+
tx1.commit().then(() => {
248+
expectValidLastBookmark(session);
249+
250+
const tx2 = session.beginTransaction();
251+
tx2.run('CREATE ()').then(() => {
252+
tx2.rollback().then(() => {
253+
expect(session.lastBookmark()).not.toBeDefined();
254+
255+
const tx3 = session.beginTransaction();
256+
tx3.run('CREATE ()').then(() => {
257+
tx3.commit().then(() => {
258+
expectValidLastBookmark(session);
259+
done();
260+
});
261+
});
262+
});
263+
});
264+
});
265+
});
266+
});
267+
268+
it('should have no bookmark when tx fails', function (done) {
269+
if (neo4jVersionOlderThan31(done)) {
270+
return;
271+
}
272+
273+
expect(session.lastBookmark()).not.toBeDefined();
274+
const tx1 = session.beginTransaction();
275+
276+
tx1.run('CREATE ()').then(() => {
277+
tx1.commit().then(() => {
278+
expectValidLastBookmark(session);
279+
280+
const tx2 = session.beginTransaction();
281+
282+
tx2.run('RETURN').catch(error => {
283+
expectSyntaxError(error);
284+
expect(session.lastBookmark()).not.toBeDefined();
285+
286+
const tx3 = session.beginTransaction();
287+
tx3.run('CREATE ()').then(() => {
288+
tx3.commit().then(() => {
289+
expectValidLastBookmark(session);
290+
done();
291+
});
292+
});
293+
});
294+
});
295+
});
296+
});
297+
240298
it('should rollback when very first run fails', done => {
241299
const tx1 = session.beginTransaction();
242300
tx1.run('RETURN foo')
@@ -349,6 +407,11 @@ describe('transaction', function() {
349407
expect(code).toBe('Neo.ClientError.Statement.SyntaxError');
350408
}
351409

410+
function expectValidLastBookmark(session) {
411+
expect(session.lastBookmark()).toBeDefined();
412+
expect(session.lastBookmark()).not.toBeNull();
413+
}
414+
352415
function neo4jVersionOlderThan31(done) {
353416
//lazy way of checking the version number
354417
//if server has been set we know it is at least

0 commit comments

Comments
 (0)