node. JS database connection Pooling Operations Common tool (MySQL module)

Source: Internet
Author: User
Tags connection pooling

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)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.