Skip to content

Commit 14ea911

Browse files
authored
Providing protocol version in Result get by Transaction.run
The result summary was not filled with the protocol version because the transactions were creating the Result object with a empty connection holder. Since the Result releases the connection from holder when it completes, the original holder could not be passed to it. The ReadOnlyConnectionHolder provides a safe way to pass the holder forward without let the consumer initialize, close or release the connection by simulating this methods without do the action and delegating part of the requests to the original ConnectionHolder. These changes also made shared-neo4j cluster friendly.
1 parent c719102 commit 14ea911

File tree

6 files changed

+479
-23
lines changed

6 files changed

+479
-23
lines changed
+77
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/**
2+
* Copyright (c) 2002-2020 "Neo4j,"
3+
* Neo4j Sweden AB [http://neo4j.com]
4+
*
5+
* This file is part of Neo4j.
6+
*
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
*/
19+
import ConnectionHolder from './connection-holder'
20+
21+
/**
22+
* Provides a interaction with a ConnectionHolder without change it state by
23+
* releasing or initilizing
24+
*/
25+
export default class ReadOnlyConnectionHolder extends ConnectionHolder {
26+
/**
27+
* Contructor
28+
* @param {ConnectionHolder} connectionHolder the connection holder which will treat the requests
29+
*/
30+
constructor (connectionHolder) {
31+
super({
32+
mode: connectionHolder._mode,
33+
database: connectionHolder._database,
34+
bookmark: connectionHolder._bookmark,
35+
connectionProvider: connectionHolder._connectionProvider
36+
})
37+
this._connectionHolder = connectionHolder
38+
}
39+
40+
/**
41+
* Return the true if the connection is suppose to be initilized with the command.
42+
*
43+
* @return {boolean}
44+
*/
45+
initializeConnection () {
46+
if (this._connectionHolder._referenceCount === 0) {
47+
return false
48+
}
49+
return true
50+
}
51+
52+
/**
53+
* Get the current connection promise.
54+
* @return {Promise<Connection>} promise resolved with the current connection.
55+
*/
56+
getConnection () {
57+
return this._connectionHolder.getConnection()
58+
}
59+
60+
/**
61+
* Get the current connection promise, doesn't performs the release
62+
* @return {Promise<Connection>} promise with the resolved current connection
63+
*/
64+
releaseConnection () {
65+
return this._connectionHolder.getConnection().catch(() => Promise.resolve())
66+
}
67+
68+
/**
69+
* Get the current connection promise, doesn't performs the connection close
70+
* @return {Promise<Connection>} promise with the resolved current connection
71+
*/
72+
close () {
73+
return this._connectionHolder
74+
.getConnection()
75+
.catch(() => () => Promise.resolve())
76+
}
77+
}

src/result.js

+17-9
Original file line numberDiff line numberDiff line change
@@ -166,26 +166,34 @@ class Result {
166166
const query = this._query
167167
const parameters = this._parameters
168168

169-
function release (protocolVersion) {
169+
function complete (protocolVersion) {
170+
onCompletedOriginal.call(
171+
observer,
172+
new ResultSummary(query, parameters, metadata, protocolVersion)
173+
)
174+
}
175+
176+
function release () {
170177
// notify connection holder that the used connection is not needed any more because result has
171178
// been fully consumed; call the original onCompleted callback after that
172-
connectionHolder.releaseConnection().then(() => {
173-
onCompletedOriginal.call(
174-
observer,
175-
new ResultSummary(query, parameters, metadata, protocolVersion)
176-
)
177-
})
179+
return connectionHolder.releaseConnection()
178180
}
179181

180182
connectionHolder.getConnection().then(
181183
// onFulfilled:
182184
connection => {
183-
release(connection ? connection.protocol().version : undefined)
185+
release().then(() =>
186+
complete(
187+
connection !== undefined
188+
? connection.protocol().version
189+
: undefined
190+
)
191+
)
184192
},
185193

186194
// onRejected:
187195
_ => {
188-
release()
196+
complete()
189197
}
190198
)
191199
}

src/transaction.js

+37-12
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import { validateQueryAndParameters } from './internal/util'
2121
import ConnectionHolder, {
2222
EMPTY_CONNECTION_HOLDER
2323
} from './internal/connection-holder'
24+
import ReadOnlyConnectionHolder from './internal/connection-holder-readonly'
2425
import Bookmark from './internal/bookmark'
2526
import TxConfig from './internal/tx-config'
2627

@@ -257,7 +258,12 @@ const _states = {
257258
})
258259
.catch(error => new FailedObserver({ error, onError }))
259260

260-
return newCompletedResult(observerPromise, query, parameters)
261+
return newCompletedResult(
262+
observerPromise,
263+
query,
264+
parameters,
265+
connectionHolder
266+
)
261267
}
262268
},
263269

@@ -274,14 +280,20 @@ const _states = {
274280
onError
275281
}),
276282
'COMMIT',
277-
{}
283+
{},
284+
connectionHolder
278285
),
279286
state: _states.FAILED
280287
}
281288
},
282289
rollback: ({ connectionHolder, onError, onComplete }) => {
283290
return {
284-
result: newCompletedResult(new CompletedObserver(), 'ROLLBACK', {}),
291+
result: newCompletedResult(
292+
new CompletedObserver(),
293+
'ROLLBACK',
294+
{},
295+
connectionHolder
296+
),
285297
state: _states.FAILED
286298
}
287299
},
@@ -294,7 +306,8 @@ const _states = {
294306
onError
295307
}),
296308
query,
297-
parameters
309+
parameters,
310+
connectionHolder
298311
)
299312
}
300313
},
@@ -313,7 +326,8 @@ const _states = {
313326
'COMMIT',
314327
{}
315328
),
316-
state: _states.SUCCEEDED
329+
state: _states.SUCCEEDED,
330+
connectionHolder
317331
}
318332
},
319333
rollback: ({ connectionHolder, onError, onComplete }) => {
@@ -328,7 +342,8 @@ const _states = {
328342
'ROLLBACK',
329343
{}
330344
),
331-
state: _states.SUCCEEDED
345+
state: _states.SUCCEEDED,
346+
connectionHolder
332347
}
333348
},
334349
run: (query, parameters, { connectionHolder, onError, onComplete }) => {
@@ -340,7 +355,8 @@ const _states = {
340355
onError
341356
}),
342357
query,
343-
parameters
358+
parameters,
359+
connectionHolder
344360
)
345361
}
346362
},
@@ -357,7 +373,8 @@ const _states = {
357373
onError
358374
}),
359375
'COMMIT',
360-
{}
376+
{},
377+
connectionHolder
361378
),
362379
state: _states.ROLLED_BACK
363380
}
@@ -371,7 +388,8 @@ const _states = {
371388
)
372389
}),
373390
'ROLLBACK',
374-
{}
391+
{},
392+
connectionHolder
375393
),
376394
state: _states.ROLLED_BACK
377395
}
@@ -385,7 +403,8 @@ const _states = {
385403
onError
386404
}),
387405
query,
388-
parameters
406+
parameters,
407+
connectionHolder
389408
)
390409
}
391410
}
@@ -446,15 +465,21 @@ function finishTransaction (
446465
* @param {ResultStreamObserver} observer - an observer for the created result.
447466
* @param {string} query - the cypher query that produced the result.
448467
* @param {Object} parameters - the parameters for cypher query that produced the result.
468+
* @param {ConnectionHolder} connectionHolder - the connection holder used to get the result
449469
* @return {Result} new result.
450470
* @private
451471
*/
452-
function newCompletedResult (observerPromise, query, parameters) {
472+
function newCompletedResult (
473+
observerPromise,
474+
query,
475+
parameters,
476+
connectionHolder = EMPTY_CONNECTION_HOLDER
477+
) {
453478
return new Result(
454479
Promise.resolve(observerPromise),
455480
query,
456481
parameters,
457-
EMPTY_CONNECTION_HOLDER
482+
new ReadOnlyConnectionHolder(connectionHolder || EMPTY_CONNECTION_HOLDER)
458483
)
459484
}
460485

0 commit comments

Comments
 (0)