Skip to content

Commit dd1b40f

Browse files
committed
Ignore null bookmarks in transaction
This commit makes `Session#beginTransaction(bookmark)` ignore `null` bookmark argument. Previously only `undefined` value was ignored and `null` was allowed to give ability to "break" the causal consistency chain. This makes it easier to reason about non-defined parameters. If "breaking" of the consistency chain is required then separate session should be used. Values `null` & `undefined` will result in session using it's known last bookmark for the next transaction.
1 parent f222d80 commit dd1b40f

File tree

3 files changed

+47
-17
lines changed

3 files changed

+47
-17
lines changed

src/v1/session.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -86,17 +86,15 @@ class Session {
8686
*
8787
* While a transaction is open the session cannot be used to run statements outside the transaction.
8888
*
89-
* @param {string} bookmark - a reference to a previous transaction. DEPRECATED: This function is deprecated in
89+
* @param {string} bookmark - a reference to a previous transaction. DEPRECATED: This parameter is deprecated in
9090
* favour of {@link Driver#session(string)} that accepts an initial bookmark. Session will ensure that all nested
9191
* transactions are chained with bookmarks to guarantee causal consistency.
9292
* @returns {Transaction} - New Transaction
9393
*/
9494
beginTransaction(bookmark) {
9595
if (bookmark) {
9696
assertString(bookmark, 'Bookmark');
97-
}
98-
if (typeof bookmark !== 'undefined') {
99-
this._lastBookmark = bookmark;
97+
this._updateBookmark(bookmark);
10098
}
10199

102100
if (this._hasTx) {

test/resources/boltkit/write_read_tx_with_bookmark_override.script.mst renamed to test/resources/boltkit/write_read_tx_with_bookmark_override.script

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ C: RUN "COMMIT" {}
1414
PULL_ALL
1515
S: SUCCESS {}
1616
SUCCESS {}
17-
C: RUN "BEGIN" {{{bookmarkOverride}}}
17+
C: RUN "BEGIN" {"bookmark": "BookmarkOverride"}
1818
PULL_ALL
1919
S: SUCCESS {}
2020
SUCCESS {}

test/v1/direct.driver.boltkit.it.js

Lines changed: 44 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -160,24 +160,57 @@ describe('direct driver', () => {
160160
});
161161

162162
it('should be possible to override bookmark', done => {
163-
testBookmarkOverride('BookmarkOverride', done);
164-
});
163+
if (!boltkit.BoltKitSupport) {
164+
done();
165+
return;
166+
}
167+
168+
const kit = new boltkit.BoltKit();
169+
const server = kit.start('./test/resources/boltkit/write_read_tx_with_bookmark_override.script', 9001);
170+
171+
kit.run(() => {
172+
const driver = createDriver();
173+
const session = driver.session(WRITE, 'BookmarkA');
174+
const writeTx = session.beginTransaction();
175+
writeTx.run('CREATE (n {name:\'Bob\'})').then(result => {
176+
const records = result.records;
177+
expect(records.length).toEqual(0);
178+
179+
writeTx.commit().then(() => {
180+
expect(session.lastBookmark()).toEqual('BookmarkB');
181+
182+
const readTx = session.beginTransaction('BookmarkOverride');
183+
readTx.run('MATCH (n) RETURN n.name AS name').then(result => {
184+
const records = result.records;
185+
expect(records.length).toEqual(1);
186+
expect(records[0].get('name')).toEqual('Bob');
187+
188+
readTx.commit().then(() => {
189+
expect(session.lastBookmark()).toEqual('BookmarkC');
190+
191+
session.close(() => {
192+
driver.close();
193+
server.exit(code => {
194+
expect(code).toEqual(0);
195+
done();
196+
});
197+
});
198+
});
199+
});
200+
});
201+
});
202+
});
165203

166-
it('should be possible to override bookmark with null', done => {
167-
testBookmarkOverride(null, done);
168204
});
169205

170-
function testBookmarkOverride(bookmarkOverride, done) {
206+
it('should not be possible to override bookmark with null', done => {
171207
if (!boltkit.BoltKitSupport) {
172208
done();
173209
return;
174210
}
175211

176212
const kit = new boltkit.BoltKit();
177-
const bookmarkScriptValue = bookmarkOverride ? JSON.stringify({bookmark: bookmarkOverride}) : '{}';
178-
const params = {bookmarkOverride: bookmarkScriptValue};
179-
const server = kit.startWithTemplate(
180-
'./test/resources/boltkit/write_read_tx_with_bookmark_override.script.mst', params, 9001);
213+
const server = kit.start('./test/resources/boltkit/write_read_tx_with_bookmarks.script', 9001);
181214

182215
kit.run(() => {
183216
const driver = createDriver();
@@ -190,7 +223,7 @@ describe('direct driver', () => {
190223
writeTx.commit().then(() => {
191224
expect(session.lastBookmark()).toEqual('BookmarkB');
192225

193-
const readTx = session.beginTransaction(bookmarkOverride);
226+
const readTx = session.beginTransaction(null);
194227
readTx.run('MATCH (n) RETURN n.name AS name').then(result => {
195228
const records = result.records;
196229
expect(records.length).toEqual(1);
@@ -211,8 +244,7 @@ describe('direct driver', () => {
211244
});
212245
});
213246
});
214-
}
215-
247+
});
216248
});
217249

218250
function createDriver() {

0 commit comments

Comments
 (0)