Skip to content

Commit c7c7123

Browse files
authored
Merge pull request #407 from lutovich/1.7-bookmark-after-auto-commit-tx
Keep bookmark returned after auto-commit tx
2 parents 4513d12 + 2600668 commit c7c7123

File tree

4 files changed

+150
-20
lines changed

4 files changed

+150
-20
lines changed

src/v1/session.js

+18-2
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ class Session {
8989
}
9090

9191
_run(statement, parameters, statementRunner) {
92-
const streamObserver = new StreamObserver();
92+
const streamObserver = new SessionStreamObserver(this);
9393
const connectionHolder = this._connectionHolderWithMode(this._mode);
9494
if (!this._hasTx) {
9595
connectionHolder.initializeConnection();
@@ -209,7 +209,6 @@ class Session {
209209
/**
210210
* Update value of the last bookmark.
211211
* @param {Bookmark} newBookmark the new bookmark.
212-
* @private
213212
*/
214213
_updateBookmark(newBookmark) {
215214
if (newBookmark && !newBookmark.isEmpty()) {
@@ -247,6 +246,23 @@ class Session {
247246
}
248247
}
249248

249+
/**
250+
* @private
251+
*/
252+
class SessionStreamObserver extends StreamObserver {
253+
254+
constructor(session) {
255+
super();
256+
this._session = session;
257+
}
258+
259+
onCompleted(meta) {
260+
super.onCompleted(meta);
261+
const bookmark = new Bookmark(meta.bookmark);
262+
this._session._updateBookmark(bookmark);
263+
}
264+
}
265+
250266
function _createTransactionExecutor(config) {
251267
const maxRetryTimeMs = (config && config.maxTransactionRetryTime) ? config.maxTransactionRetryTime : null;
252268
return new TransactionExecutor(maxRetryTimeMs);

test/v1/bolt-v3.test.js

+111
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,117 @@ describe('Bolt V3 API', () => {
256256
expect(() => session.writeTransaction(tx => tx.run('RETURN 1'), {metadata: invalidValue})).toThrow());
257257
});
258258

259+
it('should use bookmarks for auto commit transactions', done => {
260+
if (!databaseSupportsBoltV3()) {
261+
done();
262+
return;
263+
}
264+
265+
const initialBookmark = session.lastBookmark();
266+
267+
session.run('CREATE ()').then(() => {
268+
const bookmark1 = session.lastBookmark();
269+
expect(bookmark1).not.toBeNull();
270+
expect(bookmark1).toBeDefined();
271+
expect(bookmark1).not.toEqual(initialBookmark);
272+
273+
session.run('CREATE ()').then(() => {
274+
const bookmark2 = session.lastBookmark();
275+
expect(bookmark2).not.toBeNull();
276+
expect(bookmark2).toBeDefined();
277+
expect(bookmark2).not.toEqual(initialBookmark);
278+
expect(bookmark2).not.toEqual(bookmark1);
279+
280+
session.run('CREATE ()').then(() => {
281+
const bookmark3 = session.lastBookmark();
282+
expect(bookmark3).not.toBeNull();
283+
expect(bookmark3).toBeDefined();
284+
expect(bookmark3).not.toEqual(initialBookmark);
285+
expect(bookmark3).not.toEqual(bookmark1);
286+
expect(bookmark3).not.toEqual(bookmark2);
287+
288+
done();
289+
});
290+
});
291+
});
292+
});
293+
294+
it('should use bookmarks for auto commit and explicit transactions', done => {
295+
if (!databaseSupportsBoltV3()) {
296+
done();
297+
return;
298+
}
299+
300+
const initialBookmark = session.lastBookmark();
301+
302+
const tx1 = session.beginTransaction();
303+
tx1.run('CREATE ()').then(() => {
304+
tx1.commit().then(() => {
305+
const bookmark1 = session.lastBookmark();
306+
expect(bookmark1).not.toBeNull();
307+
expect(bookmark1).toBeDefined();
308+
expect(bookmark1).not.toEqual(initialBookmark);
309+
310+
session.run('CREATE ()').then(() => {
311+
const bookmark2 = session.lastBookmark();
312+
expect(bookmark2).not.toBeNull();
313+
expect(bookmark2).toBeDefined();
314+
expect(bookmark2).not.toEqual(initialBookmark);
315+
expect(bookmark2).not.toEqual(bookmark1);
316+
317+
const tx2 = session.beginTransaction();
318+
tx2.run('CREATE ()').then(() => {
319+
tx2.commit().then(() => {
320+
const bookmark3 = session.lastBookmark();
321+
expect(bookmark3).not.toBeNull();
322+
expect(bookmark3).toBeDefined();
323+
expect(bookmark3).not.toEqual(initialBookmark);
324+
expect(bookmark3).not.toEqual(bookmark1);
325+
expect(bookmark3).not.toEqual(bookmark2);
326+
327+
done();
328+
});
329+
});
330+
});
331+
});
332+
});
333+
});
334+
335+
it('should use bookmarks for auto commit transactions and transaction functions', done => {
336+
if (!databaseSupportsBoltV3()) {
337+
done();
338+
return;
339+
}
340+
341+
const initialBookmark = session.lastBookmark();
342+
343+
session.writeTransaction(tx => tx.run('CREATE ()')).then(() => {
344+
const bookmark1 = session.lastBookmark();
345+
expect(bookmark1).not.toBeNull();
346+
expect(bookmark1).toBeDefined();
347+
expect(bookmark1).not.toEqual(initialBookmark);
348+
349+
session.run('CREATE ()').then(() => {
350+
const bookmark2 = session.lastBookmark();
351+
expect(bookmark2).not.toBeNull();
352+
expect(bookmark2).toBeDefined();
353+
expect(bookmark2).not.toEqual(initialBookmark);
354+
expect(bookmark2).not.toEqual(bookmark1);
355+
356+
session.writeTransaction(tx => tx.run('CREATE ()')).then(() => {
357+
const bookmark3 = session.lastBookmark();
358+
expect(bookmark3).not.toBeNull();
359+
expect(bookmark3).toBeDefined();
360+
expect(bookmark3).not.toEqual(initialBookmark);
361+
expect(bookmark3).not.toEqual(bookmark1);
362+
expect(bookmark3).not.toEqual(bookmark2);
363+
364+
done();
365+
});
366+
});
367+
});
368+
});
369+
259370
function testTransactionMetadataWithTransactionFunctions(read, done) {
260371
if (!databaseSupportsBoltV3()) {
261372
done();

test/v1/session.test.js

+11-16
Original file line numberDiff line numberDiff line change
@@ -583,7 +583,9 @@ describe('session', () => {
583583
return;
584584
}
585585

586-
const bookmarkBefore = session.lastBookmark();
586+
// new session without initial bookmark
587+
session = driver.session();
588+
expect(session.lastBookmark()).toBeNull();
587589

588590
const tx = session.beginTransaction();
589591
tx.run('RETURN 42 as answer').then(result => {
@@ -592,11 +594,7 @@ describe('session', () => {
592594
expect(records[0].get('answer').toNumber()).toEqual(42);
593595

594596
tx.commit().then(() => {
595-
const bookmarkAfter = session.lastBookmark();
596-
expect(bookmarkAfter).toBeDefined();
597-
expect(bookmarkAfter).not.toBeNull();
598-
expect(bookmarkAfter).not.toEqual(bookmarkBefore);
599-
597+
verifyBookmark(session.lastBookmark());
600598
done();
601599
});
602600
});
@@ -631,12 +629,10 @@ describe('session', () => {
631629
tx.run('CREATE ()').then(() => {
632630
tx.commit().then(() => {
633631
const bookmarkBefore = session.lastBookmark();
634-
expect(bookmarkBefore).toBeDefined();
635-
expect(bookmarkBefore).not.toBeNull();
632+
verifyBookmark(bookmarkBefore);
636633

637634
session.run('CREATE ()').then(() => {
638-
const bookmarkAfter = session.lastBookmark();
639-
expect(bookmarkAfter).toEqual(bookmarkBefore);
635+
verifyBookmark(session.lastBookmark());
640636
done();
641637
});
642638
});
@@ -648,17 +644,16 @@ describe('session', () => {
648644
return;
649645
}
650646

651-
const bookmarkBefore = session.lastBookmark();
647+
// new session without initial bookmark
648+
session = driver.session();
649+
expect(session.lastBookmark()).toBeNull();
650+
652651
const resultPromise = session.readTransaction(tx => tx.run('RETURN 42 AS answer'));
653652

654653
resultPromise.then(result => {
655654
expect(result.records.length).toEqual(1);
656655
expect(result.records[0].get('answer').toNumber()).toEqual(42);
657-
658-
const bookmarkAfter = session.lastBookmark();
659-
verifyBookmark(bookmarkAfter);
660-
expect(bookmarkAfter).not.toEqual(bookmarkBefore);
661-
656+
verifyBookmark(session.lastBookmark());
662657
done();
663658
});
664659
});

test/v1/transaction.test.js

+10-2
Original file line numberDiff line numberDiff line change
@@ -233,8 +233,11 @@ describe('transaction', () => {
233233
return;
234234
}
235235

236-
const tx = session.beginTransaction();
236+
// new session without initial bookmark
237+
session = driver.session();
237238
expect(session.lastBookmark()).toBeNull();
239+
240+
const tx = session.beginTransaction();
238241
tx.run("CREATE (:TXNode1)").then(() => {
239242
tx.run("CREATE (:TXNode2)").then(() => {
240243
tx.commit().then(() => {
@@ -250,9 +253,11 @@ describe('transaction', () => {
250253
return;
251254
}
252255

256+
// new session without initial bookmark
257+
session = driver.session();
253258
expect(session.lastBookmark()).toBeNull();
254-
const tx1 = session.beginTransaction();
255259

260+
const tx1 = session.beginTransaction();
256261
tx1.run('CREATE ()').then(() => {
257262
tx1.commit().then(() => {
258263
expectValidLastBookmark(session);
@@ -283,7 +288,10 @@ describe('transaction', () => {
283288
return;
284289
}
285290

291+
// new session without initial bookmark
292+
session = driver.session();
286293
expect(session.lastBookmark()).toBeNull();
294+
287295
const tx1 = session.beginTransaction();
288296

289297
tx1.run('CREATE ()').then(() => {

0 commit comments

Comments
 (0)