Mepps has uploaded a new change for review. ( 
https://gerrit.wikimedia.org/r/398305 )

Change subject: Use pool in data.js, don't close pool
......................................................................

Use pool in data.js, don't close pool

Change-Id: I9bd8a48f257c0c3dc2e75372b20089ef94874c44
---
M persistence.js
M routes/data.js
2 files changed, 24 insertions(+), 13 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/wikimedia/fundraising/dash 
refs/changes/05/398305/1

diff --git a/persistence.js b/persistence.js
index 6398d58..7bfb16f 100644
--- a/persistence.js
+++ b/persistence.js
@@ -32,10 +32,22 @@
                values.push( board.id );
                values.push( i );
        }
-       return connection.query( insertWidgets + placeholders, values ).then( 
connection.end() );
+       return connection.query( insertWidgets + placeholders, values ).then(
+               function ( dbResults ) {
+                       return dbResults;
+               }
+       );
 }
 
 module.exports = {
+       /**
+        * Run a query
+        */
+       query: function( query, params, callback ) {
+               var connection = getConnection();
+
+               connection.query( query, params ).then( callback );
+       },
        /**
         * Ensures a user exists in the user table and saves the user's local 
db id
         * in session.  Creates a default board if none exists.
@@ -96,9 +108,12 @@
                                        return;
                                }
                                user.defaultBoard = dbResults[ 0 ][ 2 ][ 0 ].id;
-                               var result = connection.query( 
insertBigEnglish, [ userId ] )
-                                       .then( connection.end() );
-                               return result;
+                               return connection.query( insertBigEnglish, [ 
userId ] )
+                                       .then(
+                                               function ( dbResults ) {
+                                                       return dbResults;
+                                               }
+                                       );
                        } );
        },
        /**
@@ -127,7 +142,6 @@
                }
                return connection.query( insert, insertParams ).then( function 
( dbResults ) {
                        instance.id = dbResults[ 0 ].insertId;
-                       connection.end();
                } );
        },
        /**
@@ -143,7 +157,6 @@
                return connection.query( select, [ instanceId, userId ] )
                        .then( function ( dbResults ) {
                                var result = dbResults[ 0 ][ 0 ];
-                               connection.end();
                                if ( result.owner_id ) {
                                        return {
                                                id: instanceId,
@@ -191,7 +204,6 @@
                                                previewPath: rows[ i 
].preview_path
                                        };
                                }
-                               connection.end();
                                return result;
                        } );
        },
@@ -242,7 +254,7 @@
                                board.id = dbResults[ 0 ].insertId;
                        } )
                        .then( function () {
-                               return insertWidgetList( board, connection 
).then( connection.end() );
+                               return insertWidgetList( board, connection );
                        } );
        },
        /**
@@ -262,6 +274,7 @@
                                        widgetSelect = 'SELECT wi.id, 
wi.widget_id, w.code, wi.owner_id, wi.display_name, wi.description, 
wi.is_shared, wi.configuration FROM dash_widget_instance wi INNER JOIN 
dash_widget w on w.id = wi.widget_id INNER JOIN dash_widget_instance_board wib 
ON wi.id = wib.instance_id WHERE wib.board_id = ? ORDER BY wib.widget_position';
 
                                if ( !result.owner_id ) {
+                                       connection.end();
                                        throw new Error( 'Board ' + boardId + ' 
with owner ' + userId + ' not found' );
                                }
                                board = {
@@ -291,7 +304,6 @@
                                                configuration: JSON.parse( 
rows[ i ].configuration )
                                        };
                                }
-                               connection.end();
                                return board;
                        } );
        },
@@ -321,7 +333,6 @@
                                                isShared: rows[ i ].is_shared 
=== 1
                                        };
                                }
-                               connection.end();
                                return result;
                        } );
        },
@@ -348,7 +359,6 @@
                                                previewPath: rows[ i 
].preview_path
                                        };
                                }
-                               connection.end();
                                return result;
                        } );
        }
diff --git a/routes/data.js b/routes/data.js
index 65849c4..e02f06a 100644
--- a/routes/data.js
+++ b/routes/data.js
@@ -1,6 +1,6 @@
 var widgets = require( '../widgets' ),
        odataParser = require( 'odata-parser' ),
-       mysql = require( 'mysql' ),
+       persistence = require( '../persistence.js' ),
        config = require( '../config.js' ),
        util = require( 'util' ),
        cache = require( 'memory-cache' ),
@@ -360,7 +360,7 @@
                }
        } );
        logger.debug( 'Query: ' + sqlQuery + '\nParams: ' + sqlParams.join( ', 
' ) );
-       connection.query( sqlQuery, sqlParams, function ( error, dbResults ) {
+       persistence.query( sqlQuery, sqlParams, function ( error, dbResults ) {
                if ( error ) {
                        res.json( { error: 'Query error: ' + error } );
                        return;
@@ -372,6 +372,7 @@
                };
                logger.debug( 'Storing results at cache key ' + cacheKey );
                cache.put( cacheKey, result, config.cacheDuration );
+               connection.end();
                res.json( result );
        } );
 };

-- 
To view, visit https://gerrit.wikimedia.org/r/398305
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I9bd8a48f257c0c3dc2e75372b20089ef94874c44
Gerrit-PatchSet: 1
Gerrit-Project: wikimedia/fundraising/dash
Gerrit-Branch: master
Gerrit-Owner: Mepps <me...@wikimedia.org>
Gerrit-Reviewer: Ejegg <ej...@ejegg.com>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to