Created
April 18, 2017 06:41
-
-
Save tangxinfa/c2f27bf8681521dd907f6c0de0d051a8 to your computer and use it in GitHub Desktop.
node.js mysql2 pool will leak connection if error occured
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
"use strict"; | |
const mysql2 = require('mysql2'), | |
co = require('co'); | |
const pool = mysql2.createPoolPromise({ | |
host: "127.0.0.1", | |
port: 3306, | |
user: 'root', | |
password: "", | |
database: 'test', | |
charset: 'UTF8MB4_GENERAL_CI' | |
}); | |
co(function* () { | |
yield pool.query('CREATE TABLE IF NOT EXISTS test ( name VARCHAR(32) NOT NULL, PRIMARY KEY(name))'); | |
for(let i = 0; i < 15; ++i) { | |
try { | |
yield pool.query('INSERT INTO test SET name="hello"'); | |
} catch (e) { | |
if (e.code == 'ER_DUP_ENTRY') { | |
console.error(i + ': ER_DUP_ENTRY' + e.stack); | |
} else { | |
throw e; | |
} | |
} | |
} | |
}).then(function () { | |
console.warn("end"); | |
process.exit(0); | |
}, function (err) { | |
console.error({error: err.stack}, "error"); | |
process.exit(1); | |
}).catch(function (e) { | |
console.error({exception: e.stack}, "exception"); | |
process.exit(1); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment