@@ -27,6 +27,9 @@ var connection = require('./connection.js');
27
27
var nodbUtil = require ( './util.js' ) ;
28
28
var createPoolPromisified ;
29
29
var getConnectionPromisified ;
30
+ var poolCache = { } ;
31
+ var tempUsedPoolAliases = { } ;
32
+ var defaultPoolAlias = 'default' ;
30
33
31
34
try {
32
35
oracledbCLib = require ( '../build/Release/oracledb' ) ;
@@ -55,45 +58,163 @@ oracledbCLib.Oracledb.prototype.newLob = function(iLob) {
55
58
// things like extend out the pool instance prior to passing it to the caller.
56
59
function createPool ( poolAttrs , createPoolCb ) {
57
60
var self = this ;
61
+ var poolAlias ;
58
62
63
+ // Initial argument count and type checks are done first and throw in the same
64
+ // call stack.
59
65
nodbUtil . assert ( arguments . length === 2 , 'NJS-009' ) ;
60
66
nodbUtil . assert ( nodbUtil . isObject ( poolAttrs ) , 'NJS-006' , 1 ) ;
61
67
nodbUtil . assert ( typeof createPoolCb === 'function' , 'NJS-006' , 2 ) ;
62
68
69
+ // Additional validations should pass errors via the callback. Need to ensure
70
+ // that errors are raised prior to actually creating the pool via _createPool.
71
+ if ( poolAttrs . poolAlias !== undefined ) {
72
+ if ( typeof poolAttrs . poolAlias !== 'string' || poolAttrs . poolAlias . length === 0 ) {
73
+ createPoolCb ( new Error ( nodbUtil . getErrorMessage ( 'NJS-004' , 'poolAttrs.poolAlias' ) ) ) ;
74
+ return ;
75
+ }
76
+
77
+ poolAlias = poolAttrs . poolAlias ;
78
+ } else if ( poolAttrs . poolAlias === undefined
79
+ && ! poolCache [ defaultPoolAlias ]
80
+ && ! tempUsedPoolAliases [ defaultPoolAlias ]
81
+ ) {
82
+ poolAlias = defaultPoolAlias ;
83
+ }
84
+
85
+ if ( poolCache [ poolAlias ] || tempUsedPoolAliases [ poolAlias ] ) {
86
+ createPoolCb ( new Error ( nodbUtil . getErrorMessage ( 'NJS-046' , poolAlias ) ) ) ;
87
+ return ;
88
+ }
89
+
90
+ // Need to prevent another call in the same stack from succeeding, otherwise
91
+ // two pools could be created with the same poolAlias and the second one that
92
+ // comes back would overwrite the first in the cache.
93
+ if ( poolAlias ) {
94
+ tempUsedPoolAliases [ poolAlias ] = true ;
95
+ }
96
+
63
97
self . _createPool ( poolAttrs , function ( err , poolInst ) {
64
98
if ( err ) {
99
+ // We need to free this up since the creation of the pool failed.
100
+ if ( poolAlias ) {
101
+ delete tempUsedPoolAliases [ poolAlias ] ;
102
+ }
103
+
65
104
createPoolCb ( err ) ;
105
+
66
106
return ;
67
107
}
68
108
69
- pool . extend ( poolInst , poolAttrs , self ) ;
109
+ if ( poolAlias ) {
110
+ poolCache [ poolAlias ] = poolInst ;
111
+
112
+ // It's now safe to remove this alias from the tempUsedPoolAliases.
113
+ delete tempUsedPoolAliases [ poolAlias ] ;
114
+ }
115
+
116
+ pool . extend ( poolInst , poolAttrs , poolAlias , self ) ;
117
+
118
+ poolInst . on ( '_after_close' , function ( ) {
119
+ var pool = this ;
120
+
121
+ if ( pool . poolAlias ) {
122
+ delete poolCache [ pool . poolAlias ] ;
123
+ }
124
+ } ) ;
70
125
71
126
createPoolCb ( null , poolInst ) ;
72
127
} ) ;
73
128
}
74
129
75
130
createPoolPromisified = nodbUtil . promisify ( createPool ) ;
76
131
132
+ // The getPool function is a synchronous method used to retrieve pools from the
133
+ // pool cache.
134
+ function getPool ( poolAlias ) {
135
+ var pool ;
136
+
137
+ nodbUtil . assert ( arguments . length < 2 , 'NJS-009' ) ;
138
+
139
+ if ( poolAlias ) {
140
+ nodbUtil . assert ( typeof poolAlias === 'string' || typeof poolAlias === 'number' , 'NJS-006' , 1 ) ;
141
+ }
142
+
143
+ poolAlias = poolAlias || defaultPoolAlias ;
144
+
145
+ pool = poolCache [ poolAlias ] ;
146
+
147
+ if ( ! pool ) {
148
+ throw new Error ( nodbUtil . getErrorMessage ( 'NJS-047' , poolAlias ) ) ;
149
+ }
150
+
151
+ return pool ;
152
+ }
153
+
77
154
// This getConnection function is used the override the getConnection method of the
78
155
// Oracledb class, which is defined in the C layer. The override allows us to do
79
156
// things like extend out the connection instance prior to passing it to the caller.
80
- function getConnection ( connAttrs , createConnectionCb ) {
157
+ function getConnection ( a1 , a2 ) {
81
158
var self = this ;
159
+ var pool ;
160
+ var poolAlias ;
161
+ var connAttrs ;
162
+ var getConnectionCb ;
82
163
83
- nodbUtil . assert ( arguments . length === 2 , 'NJS-009' ) ;
84
- nodbUtil . assert ( nodbUtil . isObject ( connAttrs ) , 'NJS-006' , 1 ) ;
85
- nodbUtil . assert ( typeof createConnectionCb === 'function' , 'NJS-006' , 2 ) ;
164
+ nodbUtil . assert ( arguments . length < 3 , 'NJS-009' ) ;
86
165
87
- self . _getConnection ( connAttrs , function ( err , connInst ) {
88
- if ( err ) {
89
- createConnectionCb ( err ) ;
166
+ // Verify the number and types of arguments, then initialize the local poolAlias,
167
+ // connAttrs, and getConnectionCb variables based on the arguments.
168
+ switch ( arguments . length ) {
169
+ case 1 :
170
+ nodbUtil . assert ( typeof a1 === 'function' , 'NJS-006' , 1 ) ;
171
+
172
+ poolAlias = defaultPoolAlias ;
173
+ getConnectionCb = a1 ;
174
+
175
+ break ;
176
+ case 2 :
177
+ nodbUtil . assert ( typeof a1 === 'string' || nodbUtil . isObject ( a1 ) , 'NJS-006' , 1 ) ;
178
+ nodbUtil . assert ( typeof a2 === 'function' , 'NJS-006' , 2 ) ;
179
+
180
+ if ( typeof a1 === 'string' ) {
181
+ poolAlias = a1 ;
182
+ } else if ( nodbUtil . isObject ( a1 ) ) {
183
+ connAttrs = a1 ;
184
+
185
+ if ( connAttrs . poolAlias ) {
186
+ poolAlias = connAttrs . poolAlias ;
187
+ }
188
+ }
189
+
190
+ getConnectionCb = a2 ;
191
+
192
+ break ;
193
+ }
194
+
195
+ // Proceed to execution based on values in local variables. Look for the poolAlias
196
+ // first and only attempt to use connAttrs if the poolAlias isn't set.
197
+ if ( poolAlias ) {
198
+ pool = poolCache [ poolAlias ] ;
199
+
200
+ if ( ! pool ) {
201
+ getConnectionCb ( new Error ( nodbUtil . getErrorMessage ( 'NJS-047' , poolAlias ) ) ) ;
90
202
return ;
91
203
}
92
204
93
- connection . extend ( connInst , self ) ;
205
+ pool . getConnection ( getConnectionCb ) ;
206
+ } else {
207
+ self . _getConnection ( connAttrs , function ( err , connInst ) {
208
+ if ( err ) {
209
+ getConnectionCb ( err ) ;
210
+ return ;
211
+ }
212
+
213
+ connection . extend ( connInst , self ) ;
94
214
95
- createConnectionCb ( null , connInst ) ;
96
- } ) ;
215
+ getConnectionCb ( null , connInst ) ;
216
+ } ) ;
217
+ }
97
218
}
98
219
99
220
getConnectionPromisified = nodbUtil . promisify ( getConnection ) ;
@@ -258,6 +379,11 @@ function extend(oracledb) {
258
379
enumerable : true ,
259
380
writable : true
260
381
} ,
382
+ getPool : {
383
+ value : getPool ,
384
+ enumerable : true ,
385
+ writable : true
386
+ } ,
261
387
_getConnection : {
262
388
value : oracledb . getConnection
263
389
} ,
0 commit comments