@@ -318,7 +318,6 @@ Database.prototype.query = function (query, params, cb)
318
318
! params ? params = null : '' ;
319
319
}
320
320
321
-
322
321
if ( typeof ( params ) === 'function' )
323
322
{
324
323
cb = params ;
@@ -353,10 +352,8 @@ Database.prototype.query = function (query, params, cb)
353
352
result . fetchMode = self . fetchMode ;
354
353
}
355
354
356
- exports . debug && console . log ( getElapsedTime ( ) , "odbc.js:query() => Calling result.fetchAll from conn.query():cbQuery." ) ;
357
- result . fetchAll ( function ( err , data , noresult ) {
355
+ result . fetchAll ( function ( err , data , rowcount ) {
358
356
var moreResults = false , moreResultsError = null ;
359
- exports . debug && console . log ( getElapsedTime ( ) , "odbc.js:query() => result.fetchAll Done." ) ;
360
357
361
358
// If there is any error, return it now only.
362
359
if ( err || initialErr )
@@ -374,7 +371,7 @@ Database.prototype.query = function (query, params, cb)
374
371
}
375
372
if ( multipleResultSet ) resultset . push ( data ) ;
376
373
else resultset = data ;
377
- deferred ? deferred . reject ( initialErr || err ) : cb ( initialErr || err , resultset , moreResults ) ;
374
+ deferred ? deferred . reject ( initialErr || err ) : cb ( initialErr || err , resultset ) ;
378
375
result . closeSync ( ) ;
379
376
initialErr = null ;
380
377
err = null ;
@@ -384,7 +381,7 @@ Database.prototype.query = function (query, params, cb)
384
381
// Get the result data
385
382
try
386
383
{
387
- if ( ! noresult )
384
+ if ( rowcount ) // Check for more result set.
388
385
moreResults = result . moreResultsSync ( ) ;
389
386
}
390
387
catch ( e )
@@ -1330,7 +1327,7 @@ odbc.ODBCStatement.prototype.executeNonQuery = function (params, cb)
1330
1327
{
1331
1328
if ( ! deferred )
1332
1329
{
1333
- cb ( err ) ;
1330
+ if ( cb ) cb ( err ) ;
1334
1331
}
1335
1332
else
1336
1333
{
@@ -1342,7 +1339,7 @@ odbc.ODBCStatement.prototype.executeNonQuery = function (params, cb)
1342
1339
if ( err ) {
1343
1340
if ( ! deferred )
1344
1341
{
1345
- cb ( err )
1342
+ if ( cb ) cb ( err )
1346
1343
}
1347
1344
else
1348
1345
{
@@ -1354,7 +1351,7 @@ odbc.ODBCStatement.prototype.executeNonQuery = function (params, cb)
1354
1351
self . _executeNonQuery ( function ( err , result ) {
1355
1352
if ( ! deferred )
1356
1353
{
1357
- cb ( err , result ) ;
1354
+ if ( cb ) cb ( err , result ) ;
1358
1355
}
1359
1356
else
1360
1357
{
@@ -1376,7 +1373,7 @@ odbc.ODBCStatement.prototype.executeNonQuery = function (params, cb)
1376
1373
self . _executeNonQuery ( function ( err , result ) {
1377
1374
if ( ! deferred )
1378
1375
{
1379
- cb ( err , result ) ;
1376
+ if ( cb ) cb ( err , result ) ;
1380
1377
}
1381
1378
else
1382
1379
{
@@ -1410,7 +1407,7 @@ odbc.ODBCStatement.prototype.prepare = function (sql, cb) {
1410
1407
1411
1408
self . queue . push ( function ( next ) {
1412
1409
self . _prepare ( sql , function ( err ) {
1413
- cb ( err ) ;
1410
+ if ( cb ) cb ( err ) ;
1414
1411
1415
1412
return next ( ) ;
1416
1413
} ) ;
@@ -1427,10 +1424,10 @@ odbc.ODBCStatement.prototype.bind = function (ary, cb) {
1427
1424
if ( Array . isArray ( ary ) )
1428
1425
{
1429
1426
var err = parseParams ( ary ) ;
1430
- if ( err ) cb ( err ) ;
1427
+ if ( err && cb ) cb ( err ) ;
1431
1428
}
1432
1429
self . _bind ( ary , function ( err ) {
1433
- cb ( err ) ;
1430
+ if ( cb ) cb ( err ) ;
1434
1431
1435
1432
//NOTE: we do not call next() here because
1436
1433
//we want to pop the next bind call only
@@ -1490,6 +1487,7 @@ Pool.prototype.open = function (connStr, callback)
1490
1487
{
1491
1488
db = self . availablePool [ connStr ] . shift ( ) ;
1492
1489
db . lastUsed = null ;
1490
+ self . usedPool [ connStr ] = self . usedPool [ connStr ] || [ ] ;
1493
1491
self . usedPool [ connStr ] . push ( db ) ;
1494
1492
callback ( null , db ) ;
1495
1493
}
@@ -1502,6 +1500,7 @@ Pool.prototype.open = function (connStr, callback)
1502
1500
{
1503
1501
db = self . availablePool [ connStr ] . shift ( ) ;
1504
1502
db . lastUsed = null ;
1503
+ self . usedPool [ connStr ] = self . usedPool [ connStr ] || [ ] ;
1505
1504
self . usedPool [ connStr ] . push ( db ) ;
1506
1505
clearInterval ( interval ) ;
1507
1506
callback ( null , db ) ;
@@ -1521,28 +1520,15 @@ Pool.prototype.open = function (connStr, callback)
1521
1520
{
1522
1521
db = new Database ( { odbc : self . odbc } ) ;
1523
1522
self . poolSize ++ ;
1524
- db . open ( connStr , function ( error ) {
1525
- exports . debug && console . log ( "%s odbc.js : pool[%s] : pool.db.open new connection." , getElapsedTime ( ) , self . index ) ;
1526
- if ( error )
1527
- {
1528
- self . poolSize -- ;
1529
- }
1530
- else
1531
- {
1532
- self . usedPool [ connStr ] = self . usedPool [ connStr ] || [ ] ;
1533
- db . created = Date . now ( ) ;
1534
- self . usedPool [ connStr ] . push ( db ) ;
1535
- }
1536
- callback ( error , db ) ;
1537
- } ) ; //db.open
1538
1523
1539
1524
db . realClose = db . close ;
1540
1525
db . close = function ( cb )
1541
1526
{
1527
+ var db = this ;
1542
1528
db . lastUsed = Date . now ( ) ;
1543
1529
//call back early, we can do the rest of this stuff after the client
1544
1530
//thinks that the connection is closed.
1545
- cb ( null ) ;
1531
+ if ( cb ) cb ( null ) ;
1546
1532
1547
1533
// If this connection has some active transaction, rollback the
1548
1534
// transaction to free up the held resorces before moving back to
@@ -1555,11 +1541,11 @@ Pool.prototype.open = function (connStr, callback)
1555
1541
//remove this db from the usedPool
1556
1542
self . usedPool [ connStr ] . splice ( self . usedPool [ connStr ] . indexOf ( db ) , 1 ) ;
1557
1543
1558
- //move this connection back to the connection pool
1544
+ //move this connection back to the connection pool at the end.
1559
1545
if ( db . conn )
1560
1546
{
1561
1547
self . availablePool [ connStr ] = self . availablePool [ connStr ] || [ ] ;
1562
- self . availablePool [ connStr ] . unshift ( db ) ;
1548
+ self . availablePool [ connStr ] . push ( db ) ;
1563
1549
1564
1550
//start cleanUp if enabled
1565
1551
if ( self . options . autoCleanIdle ) self . cleanUp ( connStr ) ;
@@ -1570,6 +1556,20 @@ Pool.prototype.open = function (connStr, callback)
1570
1556
}
1571
1557
} ; // db.close function
1572
1558
1559
+ db . open ( connStr , function ( error ) {
1560
+ exports . debug && console . log ( "%s odbc.js : pool[%s] : pool.db.open new connection." , getElapsedTime ( ) , self . index ) ;
1561
+ if ( error )
1562
+ {
1563
+ self . poolSize -- ;
1564
+ }
1565
+ else
1566
+ {
1567
+ self . usedPool [ connStr ] = self . usedPool [ connStr ] || [ ] ;
1568
+ db . created = Date . now ( ) ;
1569
+ self . usedPool [ connStr ] . push ( db ) ;
1570
+ }
1571
+ callback ( error , db ) ;
1572
+ } ) ; //db.open
1573
1573
}
1574
1574
} ;
1575
1575
@@ -1604,6 +1604,7 @@ Pool.prototype.init = function(count, connStr)
1604
1604
exports . debug && console . log ( "%s odbc.js: %d connection(s) initialized.\n" , getElapsedTime ( ) , self . poolSize ) ;
1605
1605
return ret ;
1606
1606
}
1607
+ if ( ret !== true ) break ;
1607
1608
exports . debug && console . log ( "%s odbc.js : pool[%s] : pool.init %d" , getElapsedTime ( ) , self . index , i ) ;
1608
1609
1609
1610
self . availablePool [ connStr ] = self . availablePool [ connStr ] || [ ] ;
@@ -1612,8 +1613,9 @@ Pool.prototype.init = function(count, connStr)
1612
1613
db . realClose = db . close ;
1613
1614
db . close = function ( cb )
1614
1615
{
1616
+ var db = this ;
1615
1617
db . lastUsed = Date . now ( ) ;
1616
- cb ( null ) ;
1618
+ if ( cb ) cb ( null ) ;
1617
1619
if ( db . conn && db . conn . inTransaction )
1618
1620
{
1619
1621
db . rollbackTransaction ( function ( err ) { } ) ;
@@ -1622,13 +1624,9 @@ Pool.prototype.init = function(count, connStr)
1622
1624
if ( db . conn )
1623
1625
{
1624
1626
self . availablePool [ connStr ] = self . availablePool [ connStr ] || [ ] ;
1625
- self . availablePool [ connStr ] . unshift ( db ) ;
1627
+ self . availablePool [ connStr ] . push ( db ) ;
1626
1628
if ( self . options . autoCleanIdle ) self . cleanUp ( connStr ) ;
1627
1629
}
1628
- if ( exports . debug ) {
1629
- process . stdout . write ( getElapsedTime ( ) ) ;
1630
- console . dir ( self ) ;
1631
- }
1632
1630
} ; // db.close function
1633
1631
self . availablePool [ connStr ] . push ( db ) ;
1634
1632
}
@@ -1712,7 +1710,8 @@ Pool.prototype.close = function (callback)
1712
1710
1713
1711
if ( Object . keys ( pools ) . length === 0 )
1714
1712
{
1715
- return callback ( ) ;
1713
+ if ( callback ) return callback ( ) ;
1714
+ else return null ;
1716
1715
}
1717
1716
1718
1717
for ( key in pools )
@@ -1747,7 +1746,7 @@ Pool.prototype.close = function (callback)
1747
1746
1748
1747
if ( received === required )
1749
1748
{
1750
- callback ( ) ;
1749
+ if ( callback ) callback ( ) ;
1751
1750
1752
1751
//prevent mem leaks
1753
1752
self = null ;
0 commit comments