Skip to content

Providing protocol version in Result get by Transaction.run #658

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 77 additions & 0 deletions src/internal/connection-holder-readonly.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/**
* Copyright (c) 2002-2020 "Neo4j,"
* Neo4j Sweden AB [http://neo4j.com]
*
* This file is part of Neo4j.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import ConnectionHolder from './connection-holder'

/**
* Provides a interaction with a ConnectionHolder without change it state by
* releasing or initilizing
*/
export default class ReadOnlyConnectionHolder extends ConnectionHolder {
/**
* Contructor
* @param {ConnectionHolder} connectionHolder the connection holder which will treat the requests
*/
constructor (connectionHolder) {
super({
mode: connectionHolder._mode,
database: connectionHolder._database,
bookmark: connectionHolder._bookmark,
connectionProvider: connectionHolder._connectionProvider
})
this._connectionHolder = connectionHolder
}

/**
* Return the true if the connection is suppose to be initilized with the command.
*
* @return {boolean}
*/
initializeConnection () {
if (this._connectionHolder._referenceCount === 0) {
return false
}
return true
}

/**
* Get the current connection promise.
* @return {Promise<Connection>} promise resolved with the current connection.
*/
getConnection () {
return this._connectionHolder.getConnection()
}

/**
* Get the current connection promise, doesn't performs the release
* @return {Promise<Connection>} promise with the resolved current connection
*/
releaseConnection () {
return this._connectionHolder.getConnection().catch(() => Promise.resolve())
}

/**
* Get the current connection promise, doesn't performs the connection close
* @return {Promise<Connection>} promise with the resolved current connection
*/
close () {
return this._connectionHolder
.getConnection()
.catch(() => () => Promise.resolve())
}
}
26 changes: 17 additions & 9 deletions src/result.js
Original file line number Diff line number Diff line change
Expand Up @@ -166,26 +166,34 @@ class Result {
const query = this._query
const parameters = this._parameters

function release (protocolVersion) {
function complete (protocolVersion) {
onCompletedOriginal.call(
observer,
new ResultSummary(query, parameters, metadata, protocolVersion)
)
}

function release () {
// notify connection holder that the used connection is not needed any more because result has
// been fully consumed; call the original onCompleted callback after that
connectionHolder.releaseConnection().then(() => {
onCompletedOriginal.call(
observer,
new ResultSummary(query, parameters, metadata, protocolVersion)
)
})
return connectionHolder.releaseConnection()
}

connectionHolder.getConnection().then(
// onFulfilled:
connection => {
release(connection ? connection.protocol().version : undefined)
release().then(() =>
complete(
connection !== undefined
? connection.protocol().version
: undefined
)
)
},

// onRejected:
_ => {
release()
complete()
}
)
}
Expand Down
49 changes: 37 additions & 12 deletions src/transaction.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import { validateQueryAndParameters } from './internal/util'
import ConnectionHolder, {
EMPTY_CONNECTION_HOLDER
} from './internal/connection-holder'
import ReadOnlyConnectionHolder from './internal/connection-holder-readonly'
import Bookmark from './internal/bookmark'
import TxConfig from './internal/tx-config'

Expand Down Expand Up @@ -257,7 +258,12 @@ const _states = {
})
.catch(error => new FailedObserver({ error, onError }))

return newCompletedResult(observerPromise, query, parameters)
return newCompletedResult(
observerPromise,
query,
parameters,
connectionHolder
)
}
},

Expand All @@ -274,14 +280,20 @@ const _states = {
onError
}),
'COMMIT',
{}
{},
connectionHolder
),
state: _states.FAILED
}
},
rollback: ({ connectionHolder, onError, onComplete }) => {
return {
result: newCompletedResult(new CompletedObserver(), 'ROLLBACK', {}),
result: newCompletedResult(
new CompletedObserver(),
'ROLLBACK',
{},
connectionHolder
),
state: _states.FAILED
}
},
Expand All @@ -294,7 +306,8 @@ const _states = {
onError
}),
query,
parameters
parameters,
connectionHolder
)
}
},
Expand All @@ -313,7 +326,8 @@ const _states = {
'COMMIT',
{}
),
state: _states.SUCCEEDED
state: _states.SUCCEEDED,
connectionHolder
}
},
rollback: ({ connectionHolder, onError, onComplete }) => {
Expand All @@ -328,7 +342,8 @@ const _states = {
'ROLLBACK',
{}
),
state: _states.SUCCEEDED
state: _states.SUCCEEDED,
connectionHolder
}
},
run: (query, parameters, { connectionHolder, onError, onComplete }) => {
Expand All @@ -340,7 +355,8 @@ const _states = {
onError
}),
query,
parameters
parameters,
connectionHolder
)
}
},
Expand All @@ -357,7 +373,8 @@ const _states = {
onError
}),
'COMMIT',
{}
{},
connectionHolder
),
state: _states.ROLLED_BACK
}
Expand All @@ -371,7 +388,8 @@ const _states = {
)
}),
'ROLLBACK',
{}
{},
connectionHolder
),
state: _states.ROLLED_BACK
}
Expand All @@ -385,7 +403,8 @@ const _states = {
onError
}),
query,
parameters
parameters,
connectionHolder
)
}
}
Expand Down Expand Up @@ -446,15 +465,21 @@ function finishTransaction (
* @param {ResultStreamObserver} observer - an observer for the created result.
* @param {string} query - the cypher query that produced the result.
* @param {Object} parameters - the parameters for cypher query that produced the result.
* @param {ConnectionHolder} connectionHolder - the connection holder used to get the result
* @return {Result} new result.
* @private
*/
function newCompletedResult (observerPromise, query, parameters) {
function newCompletedResult (
observerPromise,
query,
parameters,
connectionHolder = EMPTY_CONNECTION_HOLDER
) {
return new Result(
Promise.resolve(observerPromise),
query,
parameters,
EMPTY_CONNECTION_HOLDER
new ReadOnlyConnectionHolder(connectionHolder || EMPTY_CONNECTION_HOLDER)
)
}

Expand Down
Loading