nodejs连接mysql之使用连接池pool
2016-08-07 18:18
393 查看
不使用连接池的坏处想必试过的人都已经知道了,我这里给出一个使用连接池的范例,实现的较为简单,最好不要直接用。
连接池范例:
调用范例
连接池范例:
//导入所需模块 var mysql=require("mysql"); //导入配置文件 var cfg =require("./config/db"); var pool = mysql.createPool({ host: cfg.HOST, user: cfg.USER, password: cfg.PASS, database: cfg.NAME port: cfg.PORT }); //导出查询相关 var query=function(sql,callback){ pool.getConnection(function(err,conn){ if(err){ callback(err,null,null); }else{ conn.query(sql,function(qerr,vals,fields){ //释放连接 conn.release(); //事件驱动回调 callback(qerr,vals,fields); }); } }); }; module.exports=query;
调用范例
var query=require("./lib/pool.js"); query("select * from token ",function(err,vals,fields){ //其他信息 });
相关文章推荐
- 使用TOMCAT5.5连接池连接mysql(解决Cannot create JDBC driver of class '' for connect URL 'null')
- PHP使用连接池提高性能(mysql不需要,Oracle连接速度很慢)
- nodeJs使用连接池链接mysql
- tomcat7+ 使用org.apache.tomcat.jdbc.pool.DataSource连接池已经把mysql-jdbc放lib下,还是报找不到类错误。
- 使用TOMCAT 连接池连接mysql(解决Cannot create JDBC driver of class '' for connect URL 'null')
- 关于在使用c3p0 连接池配置数据库连接时出现 com.mchange.v2.resourcepool.CannotAcquireResourceExceptiond问题的解答
- Node.js 连接 postgreSQL数据库 (使用连接池pool)
- 一个完整的Nodejs项目 完成连接数据库(MySQL),登陆、注册功能 session的使用 和发送邮件
- 使用TOMCAT5.5连接池连接mysql(解决Cannot create JDBC driver of class '' for connect URL 'null' 及can not load org.gjt.mm.mysql.Driver)
- 使用TOMCAT5.5连接池连接mysql(解决Cannot create JDBC driver of class '' for connect URL 'null')
- nodejs使用MYSQL连接池,断线重连
- 使用 APACHE COMMON DBCP +COMMON POOL+MYSQL连接无效的问题
- nodejs的mysql模块学习(六)连接池的创建和使用
- 使用TOMCAT5.5连接池连接mysql(解决Cannot create JDBC driver of class '' for connect URL 'null')
- nodejs连接mysql及使用
- nodejs解决mysql和连接池(pool)自动断开问题
- nodejs之代码操作mysql(直连和连接池连接mysql)\mysql权限操作
- 【自】nodeJs使用express框架进行快速建站 连接mysql进行增删改查的实际入门案例
- Nodejs使用连接池连接数据库
- 关于tomcat 使用连接池连接mysql的笔记..