Source: Osn Open Source site database Common Tools class, OSN source code address, Https://github.com/obullxl/osnode-site, Baidu Cloud Demo site, http://obullxl.duapp.com
Platform for use: node. js
Dependent projects: Osn Open Source site
/** * Database Module */var config = require (".. /config "); var options = {' Host ': Config.dbhost, ' Port ': config.port, ' user ': Config.user, ' password ': Config.password, ' Database ': config.db, ' CharSet ': Config.charset, ' connectionlimit ': config.maxconnlimit, ' supportbignumbers ': true, ' Bignumberstrings ': True};var mysql = require (' mysql '); var pool = mysql.createpool (options);/** * Release database connection */ Exports.release = function (connection) {Connection.end (function (Error) {Console.log (' connection closed ');});};/ * * Execute Query */exports.execquery = function (options) {pool.getconnection (function (error, connection) {if (error) { Console.log (' db-Get database connection Exception! '); throw error;} /* * Connection.query (' use ' + config.db, function (error, results) {if (error) {console.log (' db-' Select Database Exception! ‘); Connection.end (); Throw error; }}); *///query parameter var sql = options[' sql '];var args = options[' args '];var handler = options[' handler '];//execute query if (!args) {var q uery = connection.query (SQL, function (error, results) {if (error) {console.log (' db-') Execute the query statement exception! '); throw ErroR;} Processing result handler (results);}); Console.log (query.sql);} else {var query = connection.query (sql, args, function (error, results) {if (error) {Console.log (' db-Execute query statement exception! '); throw error;} Processing result handler (results);}); Console.log (query.sql);} Returns the connection pool Connection.release (function (Error) {if (error) {Console.log (' db-Close database connection Exception! '), throw error;});};
Node. JS database connection Pooling Operations Common tool (MySQL module)