Skip to content

Commit 505b243

Browse files
committed
ensure the correct db is targeted by subsequent sessions
1 parent 25f6f3c commit 505b243

File tree

7 files changed

+24
-12
lines changed

7 files changed

+24
-12
lines changed

packages/core/src/driver.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -880,7 +880,10 @@ class Driver {
880880
database: database ?? '',
881881
connectionProvider,
882882
bookmarks,
883-
config: this._config,
883+
config: {
884+
...this._config,
885+
homeDatabase
886+
},
884887
reactive,
885888
impersonatedUser,
886889
fetchSize,

packages/core/src/internal/connection-holder.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -161,9 +161,9 @@ class ConnectionHolder implements ConnectionHolderInterface {
161161
return this._referenceCount
162162
}
163163

164-
initializeConnection (): boolean {
164+
initializeConnection (homeDatabase?: string): boolean {
165165
if (this._referenceCount === 0 && (this._connectionProvider != null)) {
166-
this._connectionPromise = this._createConnectionPromise(this._connectionProvider)
166+
this._connectionPromise = this._createConnectionPromise(this._connectionProvider, homeDatabase)
167167
} else {
168168
this._referenceCount++
169169
return false
@@ -172,10 +172,10 @@ class ConnectionHolder implements ConnectionHolderInterface {
172172
return true
173173
}
174174

175-
private async _createConnectionPromise (connectionProvider: ConnectionProvider): Promise<Connection & Releasable | null> {
175+
private async _createConnectionPromise (connectionProvider: ConnectionProvider, homeDatabase?: string): Promise<Connection & Releasable | null> {
176176
return await connectionProvider.acquireConnection({
177177
accessMode: this._mode,
178-
database: this._database,
178+
database: (this._database === '' && homeDatabase !== undefined) ? homeDatabase : this._database,
179179
bookmarks: await this._getBookmarks(),
180180
impersonatedUser: this._impersonatedUser,
181181
onDatabaseNameResolved: this._onDatabaseNameResolved,

packages/core/src/session.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ class Session {
7676
private readonly _log: Logger
7777
private readonly _homeDatabaseCallback: Function | undefined
7878
private readonly _auth: AuthToken | undefined
79+
private readonly _homeDatabaseBestGuess
7980
/**
8081
* @constructor
8182
* @protected
@@ -126,6 +127,7 @@ class Session {
126127
this._fetchSize = fetchSize
127128
this._onDatabaseNameResolved = this._onDatabaseNameResolved.bind(this)
128129
this._homeDatabaseCallback = homeDatabaseCallback
130+
this._homeDatabaseBestGuess = config?.homeDatabase
129131
this._auth = auth
130132
this._getConnectionAcquistionBookmarks = this._getConnectionAcquistionBookmarks.bind(this)
131133
this._readConnectionHolder = new ConnectionHolder({
@@ -260,7 +262,7 @@ class Session {
260262
resultPromise = Promise.reject(
261263
newError('Cannot run query in a closed session.')
262264
)
263-
} else if (!this._hasTx && connectionHolder.initializeConnection()) {
265+
} else if (!this._hasTx && connectionHolder.initializeConnection(this._homeDatabaseBestGuess)) {
264266
resultPromise = connectionHolder
265267
.getConnection()
266268
// Connection won't be null at this point since the initialize method

packages/neo4j-driver-deno/lib/core/driver.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -880,7 +880,9 @@ class Driver {
880880
database: database ?? '',
881881
connectionProvider,
882882
bookmarks,
883-
config: this._config,
883+
config: {...this._config,
884+
homeDatabase
885+
},
884886
reactive,
885887
impersonatedUser,
886888
fetchSize,

packages/neo4j-driver-deno/lib/core/internal/connection-holder.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -161,9 +161,9 @@ class ConnectionHolder implements ConnectionHolderInterface {
161161
return this._referenceCount
162162
}
163163

164-
initializeConnection (): boolean {
164+
initializeConnection (homeDatabase?: string): boolean {
165165
if (this._referenceCount === 0 && (this._connectionProvider != null)) {
166-
this._connectionPromise = this._createConnectionPromise(this._connectionProvider)
166+
this._connectionPromise = this._createConnectionPromise(this._connectionProvider, homeDatabase)
167167
} else {
168168
this._referenceCount++
169169
return false
@@ -172,10 +172,10 @@ class ConnectionHolder implements ConnectionHolderInterface {
172172
return true
173173
}
174174

175-
private async _createConnectionPromise (connectionProvider: ConnectionProvider): Promise<Connection & Releasable | null> {
175+
private async _createConnectionPromise (connectionProvider: ConnectionProvider, homeDatabase?: string): Promise<Connection & Releasable | null> {
176176
return await connectionProvider.acquireConnection({
177177
accessMode: this._mode,
178-
database: this._database,
178+
database: (this._database === "" && homeDatabase !== undefined) ? homeDatabase : this._database,
179179
bookmarks: await this._getBookmarks(),
180180
impersonatedUser: this._impersonatedUser,
181181
onDatabaseNameResolved: this._onDatabaseNameResolved,

packages/neo4j-driver-deno/lib/core/session.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ class Session {
7676
private readonly _log: Logger
7777
private readonly _homeDatabaseCallback: Function | undefined
7878
private readonly _auth: AuthToken | undefined
79+
private readonly _homeDatabaseBestGuess
7980
/**
8081
* @constructor
8182
* @protected
@@ -126,6 +127,7 @@ class Session {
126127
this._fetchSize = fetchSize
127128
this._onDatabaseNameResolved = this._onDatabaseNameResolved.bind(this)
128129
this._homeDatabaseCallback = homeDatabaseCallback
130+
this._homeDatabaseBestGuess = config?.homeDatabase
129131
this._auth = auth
130132
this._getConnectionAcquistionBookmarks = this._getConnectionAcquistionBookmarks.bind(this)
131133
this._readConnectionHolder = new ConnectionHolder({
@@ -260,7 +262,7 @@ class Session {
260262
resultPromise = Promise.reject(
261263
newError('Cannot run query in a closed session.')
262264
)
263-
} else if (!this._hasTx && connectionHolder.initializeConnection()) {
265+
} else if (!this._hasTx && connectionHolder.initializeConnection(this._homeDatabaseBestGuess)) {
264266
resultPromise = connectionHolder
265267
.getConnection()
266268
// Connection won't be null at this point since the initialize method

packages/neo4j-driver/test/driver.test.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -551,6 +551,9 @@ describe('#integration driver', () => {
551551

552552
expect(driver.homeDatabaseCache.get(sharedNeo4j.authToken.principal)).toBe('neo4j')
553553
expect(session1._database).toBe('neo4j')
554+
const session2 = driver.session({ auth: sharedNeo4j.authToken })
555+
expect(session2._homeDatabaseBestGuess).toBe('neo4j')
556+
await session2.run('CREATE () RETURN 43')
554557
})
555558

556559
it('should discard old connections', async () => {

0 commit comments

Comments
 (0)