think-mysql
Advanced tools
Comparing version 1.4.0 to 1.4.1
37
index.js
@@ -21,3 +21,4 @@ const mysql = require('mysql'); | ||
logConnect: false, | ||
logSql: false | ||
logSql: false, | ||
acquireWaitTimeout: 0 | ||
}; | ||
@@ -34,2 +35,3 @@ | ||
const TRANSACTIONS = Symbol('transactions'); | ||
class ThinkMysql { | ||
@@ -43,3 +45,2 @@ /** | ||
this.maxRetryTimes = Math.max(config.connectionLimit + 1, 3); | ||
this.inTransaction = false; | ||
this.pool = mysql.createPool(helper.omit(config, 'logger,logConnect,logSql')); | ||
@@ -76,8 +77,17 @@ | ||
if (connection && !connection[CONNECTION_LOST]) return Promise.resolve(connection); | ||
const promise = helper.promisify(this.pool.getConnection, this.pool)(); | ||
const allConnectionLength = this.pool._allConnections.length; | ||
const queueLength = this.pool._connectionQueue.length; | ||
if (allConnectionLength && queueLength && this.inTransaction) { | ||
this.pool.releaseConnection(this.pool._allConnections[0]); | ||
return Promise.reject(new Error('cannot create more connection in transation, use db() to reuse connection!')); | ||
let promise; | ||
if (this.config.acquireWaitTimeout) { | ||
promise = new Promise((resolve, reject) => { | ||
this.pool.getConnection((err, connection) => { | ||
if (err) reject(err); | ||
else resolve(connection); | ||
}); | ||
setTimeout(() => { | ||
const err = new Error('acquireWaitTimeout: ' + this.config.acquireWaitTimeout + 'ms'); | ||
err.code = 'ACQUIRE_WAIT_TIMEOUT'; | ||
reject(err); | ||
}, this.config.acquireWaitTimeout); | ||
}); | ||
} else { | ||
promise = helper.promisify(this.pool.getConnection, this.pool)(); | ||
} | ||
@@ -96,3 +106,2 @@ if (this.config.afterConnect) { | ||
startTrans(connection) { | ||
this.inTransaction = true; | ||
return this.getConnection(connection).then(connection => { | ||
@@ -117,3 +126,2 @@ if (connection[TRANSACTIONS] === undefined) { | ||
commit(connection) { | ||
this.inTransaction = false; | ||
if (connection && connection[TRANSACTIONS]) { | ||
@@ -136,8 +144,5 @@ connection[TRANSACTIONS]--; | ||
connection[TRANSACTIONS]--; | ||
if (connection[TRANSACTIONS] !== 0) { | ||
this.inTransaction = false; | ||
return Promise.resolve(); | ||
} | ||
if (connection[TRANSACTIONS] !== 0) return Promise.resolve(); | ||
} | ||
const result = this.query({ | ||
return this.query({ | ||
sql: 'ROLLBACK', | ||
@@ -147,4 +152,2 @@ transaction: TRANSACTION.end, | ||
}, connection); | ||
this.inTransaction = false; | ||
return result; | ||
} | ||
@@ -151,0 +154,0 @@ /** |
{ | ||
"name": "think-mysql", | ||
"description": "think-mysql for ThinkJS", | ||
"version": "1.4.0", | ||
"version": "1.4.1", | ||
"author": { | ||
@@ -6,0 +6,0 @@ "name": "zhengqingxin", |
@@ -7,4 +7,2 @@ import test from 'ava'; | ||
return { | ||
_allConnections: [], | ||
_connectionQueue: [], | ||
getConnection(callback) { | ||
@@ -29,3 +27,2 @@ callback(null, { | ||
}, | ||
releaseConnection(connection) {}, | ||
on() { | ||
@@ -58,3 +55,3 @@ | ||
const keys = Object.keys(instance.config).sort(); | ||
t.deepEqual(keys, [ 'connectionLimit', 'database', 'host', 'logConnect', 'logSql', 'logger', 'password', 'port', 'user' ]); | ||
t.deepEqual(keys, [ 'acquireWaitTimeout', 'connectionLimit', 'database', 'host', 'logConnect', 'logSql', 'logger', 'password', 'port', 'user' ]); | ||
t.is(helper.isEmpty(instance.config), false); | ||
@@ -61,0 +58,0 @@ t.is(helper.isEmpty(instance.pool), false); |
24097
565