forked from NetEase/lordofpomelo
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
xiaodong.li
committed
Jun 14, 2018
1 parent
918cdc9
commit 3d971c7
Showing
23 changed files
with
540 additions
and
586 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,49 @@ | ||
var _poolModule = require('generic-pool'); | ||
var mysql = require('mysql'); | ||
var _poolModule = require('generic-pool'); | ||
var mysql = require('mysql'); | ||
/* | ||
* Create mysql connection pool. | ||
*/ | ||
var createMysqlPool = function (app) { | ||
var mysqlConfig = app.get('mysql'); | ||
|
||
/* | ||
* Create mysql connection pool. | ||
*/ | ||
var createMysqlPool = function(app){ | ||
var mysqlConfig = app.get('mysql'); | ||
const factory = { | ||
create: function(){ | ||
return new Promise(function(resolve, reject){ | ||
var client = mysql.createConnection({ | ||
host: mysqlConfig.host, | ||
user: mysqlConfig.user, | ||
password: mysqlConfig.password, | ||
database: mysqlConfig.database | ||
}); | ||
resolve(client); | ||
}); | ||
}, | ||
destroy: function(client){ | ||
return new Promise(function(resolve){ | ||
client.on('end', function(){ | ||
resolve() | ||
}); | ||
client.disconnect() | ||
}); | ||
} | ||
} | ||
return _poolModule.createPool(factory, {max:10, min:2}); | ||
}; | ||
|
||
exports.createMysqlPool = createMysqlPool; | ||
const factory = { | ||
create: function () { | ||
return new Promise(function (resolve) { | ||
var client = mysql.createConnection({ | ||
host: mysqlConfig.host, | ||
user: mysqlConfig.user, | ||
password: mysqlConfig.password, | ||
database: mysqlConfig.database, | ||
port: mysqlConfig.port | ||
}); | ||
client.on('error', function () { | ||
client.connect(); | ||
}); | ||
client.connect(function (error) { | ||
if (error) { | ||
console.log('mysql connect error', error); | ||
} | ||
resolve(client) | ||
}); | ||
}) | ||
}, | ||
destroy: function (client) { | ||
return new Promise(function (resolve) { | ||
client.on('end', function () { | ||
resolve() | ||
}) | ||
client.end() | ||
}) | ||
} | ||
} | ||
|
||
var opts = { | ||
min: 2, | ||
max: 10, | ||
idleTimeoutMillis: 30000, | ||
log: true | ||
}; | ||
|
||
return _poolModule.createPool(factory, opts); | ||
}; | ||
exports.createMysqlPool = createMysqlPool; |
Oops, something went wrong.