Skip to content

Commit c6a8cc7

Browse files
author
Zhen Li
committed
Fixed a bug where connection pool size can go beyond max connection pool size.
Added more logging around connection pool release.
1 parent 2dd64d9 commit c6a8cc7

File tree

2 files changed

+49
-8
lines changed

2 files changed

+49
-8
lines changed

src/v1/internal/pool.js

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -69,11 +69,18 @@ class Pool {
6969
const key = address.asKey()
7070

7171
if (resource) {
72-
resourceAcquired(key, this._activeResourceCounts)
73-
if (this._log.isDebugEnabled()) {
74-
this._log.debug(`${resource} acquired from the pool ${key}`)
72+
if (
73+
this._maxSize &&
74+
this.activeResourceCount(address) >= this._maxSize
75+
) {
76+
this._destroy(resource)
77+
} else {
78+
resourceAcquired(key, this._activeResourceCounts)
79+
if (this._log.isDebugEnabled()) {
80+
this._log.debug(`${resource} acquired from the pool ${key}`)
81+
}
82+
return resource
7583
}
76-
return resource
7784
}
7885

7986
// We're out of resources and will try to acquire later on when an existing resource is released.
@@ -100,11 +107,11 @@ class Pool {
100107
// request already resolved/rejected by the release operation; nothing to do
101108
} else {
102109
// request is still pending and needs to be failed
110+
const activeCount = this.activeResourceCount(address)
111+
const idleCount = this.has(address) ? this._pools[key].length : 0
103112
request.reject(
104113
newError(
105-
`Connection acquisition timed out in ${
106-
this._acquisitionTimeout
107-
} ms.`
114+
`Connection acquisition timed out in ${this._acquisitionTimeout} ms. Poos status: Active conn count = ${activeCount}, Idle conn count = ${idleCount}.`
108115
)
109116
)
110117
}
@@ -209,7 +216,10 @@ class Pool {
209216
}
210217
if (this._installIdleObserver) {
211218
this._installIdleObserver(resource, {
212-
onError: () => {
219+
onError: error => {
220+
this._log.debug(
221+
`Idle connection ${resource} destroyed because of error: ${error}`
222+
)
213223
const pool = this._pools[key]
214224
if (pool) {
215225
this._pools[key] = pool.filter(r => r !== resource)

test/v1/driver.test.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,37 @@ describe('driver', () => {
5757
}
5858
})
5959

60+
it('should not decrease active connection count after driver close', done => {
61+
// Given
62+
const config = {
63+
maxConnectionPoolSize: 2,
64+
connectionAcquisitionTimeout: 0,
65+
encrypted: false
66+
}
67+
driver = neo4j.driver('bolt://localhost', sharedNeo4j.authToken, config)
68+
69+
function beginTxWithoutCommit (driver) {
70+
const session = driver.session()
71+
const tx = session.beginTransaction()
72+
return tx.run('RETURN 1')
73+
}
74+
// When
75+
76+
const result1 = beginTxWithoutCommit(driver)
77+
const result2 = beginTxWithoutCommit(driver)
78+
79+
Promise.all([result1, result2]).then(results => {
80+
driver.close()
81+
beginTxWithoutCommit(driver).catch(() => {
82+
var serverKey = Object.keys(driver._pool._activeResourceCounts)[0]
83+
expect(driver._pool._activeResourceCounts[serverKey]).toEqual(2)
84+
expect(driver._pool._pools[serverKey].length).toEqual(0)
85+
expect(Object.keys(driver._openConnections).length).toEqual(2)
86+
done()
87+
})
88+
})
89+
}, 10000)
90+
6091
it('should expose sessions', () => {
6192
// Given
6293
driver = neo4j.driver('bolt://localhost', sharedNeo4j.authToken)

0 commit comments

Comments
 (0)