OSDN Git Service

$name[name]がundefinedになることがあった
[webchat/WebChat.git] / mysql_pool.js
1 var async = require("async");
2
3 module.exports = function(param)
4 {
5         this.query = function(query,param,callback)
6         {
7                 async.waterfall([
8                         function(next){
9                                 pool.acquire(next);
10                         },
11                         function(client,next){
12                                 client.query(query,param,function(err,result){
13                                         next(err,result,client);
14                                 });
15                         },
16                         function(result,client,next){
17                                 pool.release(client);
18                                 next(null,result);
19                         }
20                 ],callback);
21         }
22         var generic_pool = require("generic-pool");
23         var mysql = require("mysql");
24         pool = generic_pool.Pool({
25                 name : "mysql",
26                 max : 10,
27                 create : function(cb){
28                         var connection = mysql.createConnection(param);
29                         connection.connect(function(err){
30                                 cb(err,connection);
31                         });
32                 },
33                 destroy : function(db){
34                         db.end();
35                 }
36         });
37 }