19
19
20
20
import Session from './session'
21
21
import Pool from './internal/pool'
22
- import Connection from './internal/connection'
22
+ import ChannelConnection from './internal/connection-channel '
23
23
import { newError , SERVICE_UNAVAILABLE } from './error'
24
- import { DirectConnectionProvider } from './internal/connection-providers '
24
+ import DirectConnectionProvider from './internal/connection-provider-direct '
25
25
import Bookmark from './internal/bookmark'
26
26
import ConnectivityVerifier from './internal/connectivity-verifier'
27
27
import PoolConfig , {
@@ -76,19 +76,9 @@ class Driver {
76
76
this . _id = idGenerator ++
77
77
this . _address = address
78
78
this . _userAgent = userAgent
79
- this . _openConnections = { }
80
79
this . _authToken = authToken
81
80
this . _config = config
82
81
this . _log = Logger . create ( config )
83
- this . _pool = new Pool ( {
84
- create : this . _createConnection . bind ( this ) ,
85
- destroy : this . _destroyConnection . bind ( this ) ,
86
- validate : this . _validateConnection . bind ( this ) ,
87
- installIdleObserver : this . _installIdleObserverOnConnection . bind ( this ) ,
88
- removeIdleObserver : this . _removeIdleObserverOnConnection . bind ( this ) ,
89
- config : PoolConfig . fromDriverConfig ( config ) ,
90
- log : this . _log
91
- } )
92
82
93
83
/**
94
84
* Reference to the connection provider. Initialized lazily by {@link _getOrCreateConnectionProvider}.
@@ -111,73 +101,13 @@ class Driver {
111
101
112
102
/**
113
103
* Verifies connectivity of this driver by trying to open a connection with the provided driver options.
114
- * @param {string } [db =''] the target database to verify connectivity for.
104
+ * @param {string } [database =''] the target database to verify connectivity for.
115
105
* @returns {Promise<object> } promise resolved with server info or rejected with error.
116
106
*/
117
- verifyConnectivity ( { db = '' } = { } ) {
107
+ verifyConnectivity ( { database = '' } = { } ) {
118
108
const connectionProvider = this . _getOrCreateConnectionProvider ( )
119
109
const connectivityVerifier = new ConnectivityVerifier ( connectionProvider )
120
- return connectivityVerifier . verify ( { db } )
121
- }
122
-
123
- /**
124
- * Create a new connection and initialize it.
125
- * @return {Promise<Connection> } promise resolved with a new connection or rejected when failed to connect.
126
- * @access private
127
- */
128
- _createConnection ( address , release ) {
129
- const connection = Connection . create (
130
- address ,
131
- this . _config ,
132
- this . _createConnectionErrorHandler ( ) ,
133
- this . _log
134
- )
135
- connection . _release = ( ) => release ( address , connection )
136
- this . _openConnections [ connection . id ] = connection
137
-
138
- return connection . connect ( this . _userAgent , this . _authToken ) . catch ( error => {
139
- if ( this . onError ) {
140
- // notify Driver.onError callback about connection initialization errors
141
- this . onError ( error )
142
- }
143
- // let's destroy this connection
144
- this . _destroyConnection ( connection )
145
- // propagate the error because connection failed to connect / initialize
146
- throw error
147
- } )
148
- }
149
-
150
- /**
151
- * Check that a connection is usable
152
- * @return {boolean } true if the connection is open
153
- * @access private
154
- **/
155
- _validateConnection ( conn ) {
156
- if ( ! conn . isOpen ( ) ) {
157
- return false
158
- }
159
-
160
- const maxConnectionLifetime = this . _config . maxConnectionLifetime
161
- const lifetime = Date . now ( ) - conn . creationTimestamp
162
- return lifetime <= maxConnectionLifetime
163
- }
164
-
165
- _installIdleObserverOnConnection ( conn , observer ) {
166
- conn . _queueObserver ( observer )
167
- }
168
-
169
- _removeIdleObserverOnConnection ( conn ) {
170
- conn . _updateCurrentObserver ( )
171
- }
172
-
173
- /**
174
- * Dispose of a connection.
175
- * @return {Connection } the connection to dispose.
176
- * @access private
177
- */
178
- _destroyConnection ( conn ) {
179
- delete this . _openConnections [ conn . id ]
180
- conn . close ( )
110
+ return connectivityVerifier . verify ( { database } )
181
111
}
182
112
183
113
/**
@@ -194,13 +124,13 @@ class Driver {
194
124
* @param {string } [defaultAccessMode=WRITE] the access mode of this session, allowed values are {@link READ} and {@link WRITE}.
195
125
* @param {string|string[] } [bookmarks=null] the initial reference or references to some previous
196
126
* transactions. Value is optional and absence indicates that that the bookmarks do not exist or are unknown.
197
- * @param {string } [db =''] the database this session will connect to.
127
+ * @param {string } [database =''] the database this session will connect to.
198
128
* @return {Session } new session.
199
129
*/
200
130
session ( {
201
131
defaultAccessMode = WRITE ,
202
132
bookmarks : bookmarkOrBookmarks ,
203
- db = ''
133
+ database = ''
204
134
} = { } ) {
205
135
const sessionMode = Driver . _validateSessionMode ( defaultAccessMode )
206
136
const connectionProvider = this . _getOrCreateConnectionProvider ( )
@@ -209,7 +139,7 @@ class Driver {
209
139
: Bookmark . empty ( )
210
140
return new Session ( {
211
141
mode : sessionMode ,
212
- db ,
142
+ database ,
213
143
connectionProvider,
214
144
bookmark,
215
145
config : this . _config
@@ -225,38 +155,27 @@ class Driver {
225
155
}
226
156
227
157
// Extension point
228
- _createConnectionProvider ( address , connectionPool , driverOnErrorCallback ) {
229
- return new DirectConnectionProvider (
230
- address ,
231
- connectionPool ,
232
- driverOnErrorCallback
233
- )
234
- }
235
-
236
- // Extension point
237
- _createConnectionErrorHandler ( ) {
238
- return new ConnectionErrorHandler ( SERVICE_UNAVAILABLE )
158
+ _createConnectionProvider ( address , userAgent , authToken ) {
159
+ return new DirectConnectionProvider ( {
160
+ id : this . _id ,
161
+ config : this . _config ,
162
+ log : this . _log ,
163
+ address : address ,
164
+ userAgent : userAgent ,
165
+ authToken : authToken
166
+ } )
239
167
}
240
168
241
169
_getOrCreateConnectionProvider ( ) {
242
170
if ( ! this . _connectionProvider ) {
243
- const driverOnErrorCallback = this . _driverOnErrorCallback . bind ( this )
244
171
this . _connectionProvider = this . _createConnectionProvider (
245
172
this . _address ,
246
- this . _pool ,
247
- driverOnErrorCallback
173
+ this . _userAgent ,
174
+ this . _authToken
248
175
)
249
176
}
250
- return this . _connectionProvider
251
- }
252
177
253
- _driverOnErrorCallback ( error ) {
254
- const userDefinedOnErrorCallback = this . onError
255
- if ( userDefinedOnErrorCallback && error . code === SERVICE_UNAVAILABLE ) {
256
- userDefinedOnErrorCallback ( error )
257
- } else {
258
- // we don't need to tell the driver about this error
259
- }
178
+ return this . _connectionProvider
260
179
}
261
180
262
181
/**
@@ -266,18 +185,8 @@ class Driver {
266
185
*/
267
186
close ( ) {
268
187
this . _log . info ( `Driver ${ this . _id } closing` )
269
-
270
- try {
271
- // purge all idle connections in the connection pool
272
- this . _pool . purgeAll ( )
273
- } finally {
274
- // then close all connections driver has ever created
275
- // it is needed to close connections that are active right now and are acquired from the pool
276
- for ( let connectionId in this . _openConnections ) {
277
- if ( this . _openConnections . hasOwnProperty ( connectionId ) ) {
278
- this . _openConnections [ connectionId ] . close ( )
279
- }
280
- }
188
+ if ( this . _connectionProvider ) {
189
+ this . _connectionProvider . close ( )
281
190
}
282
191
}
283
192
}
0 commit comments