不要怂,就是干,撸起袖子干!

Commit 3a4ff4a6 by Jan Aagaard Meier

fix(cls) Assign transaction to CLS a bit later to prevent race issues, and clear…

… it before commit / rollback
1 parent b063d617
Showing with 14 additions and 5 deletions
...@@ -21,6 +21,7 @@ var Utils = require('./utils') ...@@ -21,6 +21,7 @@ var Utils = require('./utils')
* @mixes Associations * @mixes Associations
*/ */
var Model = function(name, attributes, options) { var Model = function(name, attributes, options) {
this.options = Utils._.extend({ this.options = Utils._.extend({
timestamps: true, timestamps: true,
instanceMethods: {}, instanceMethods: {},
...@@ -853,6 +854,7 @@ Model.prototype.refreshAttributes = function() { ...@@ -853,6 +854,7 @@ Model.prototype.refreshAttributes = function() {
this._isPrimaryKey = Utils._.memoize(function(key) { this._isPrimaryKey = Utils._.memoize(function(key) {
return self.primaryKeyAttributes.indexOf(key) !== -1; return self.primaryKeyAttributes.indexOf(key) !== -1;
}); });
}; };
/** /**
......
...@@ -127,6 +127,8 @@ Transaction.LOCK = Transaction.prototype.LOCK = { ...@@ -127,6 +127,8 @@ Transaction.LOCK = Transaction.prototype.LOCK = {
Transaction.prototype.commit = function() { Transaction.prototype.commit = function() {
var self = this; var self = this;
this.$clearCls();
return this return this
.sequelize .sequelize
.getQueryInterface() .getQueryInterface()
...@@ -148,6 +150,8 @@ Transaction.prototype.commit = function() { ...@@ -148,6 +150,8 @@ Transaction.prototype.commit = function() {
Transaction.prototype.rollback = function() { Transaction.prototype.rollback = function() {
var self = this; var self = this;
this.$clearCls();
return this return this
.sequelize .sequelize
.getQueryInterface() .getQueryInterface()
...@@ -175,10 +179,11 @@ Transaction.prototype.prepareEnvironment = function() { ...@@ -175,10 +179,11 @@ Transaction.prototype.prepareEnvironment = function() {
}).then(function () { }).then(function () {
return self.setIsolationLevel(); return self.setIsolationLevel();
}).then(function () { }).then(function () {
return self.setAutocommit();
}).tap(function () {
if (self.sequelize.constructor.cls) { if (self.sequelize.constructor.cls) {
self.sequelize.constructor.cls.set('transaction', self); self.sequelize.constructor.cls.set('transaction', self);
} }
return self.setAutocommit();
}); });
}; };
...@@ -213,15 +218,17 @@ Transaction.prototype.setIsolationLevel = function() { ...@@ -213,15 +218,17 @@ Transaction.prototype.setIsolationLevel = function() {
}; };
Transaction.prototype.cleanup = function() { Transaction.prototype.cleanup = function() {
var cls = this.sequelize.constructor.cls;
this.connection.uuid = undefined; this.connection.uuid = undefined;
return this.sequelize.connectionManager.releaseConnection(this.connection);
};
Transaction.prototype.$clearCls = function () {
var cls = this.sequelize.constructor.cls;
if (cls) { if (cls) {
if (cls.get('transaction') === this) { if (cls.get('transaction') === this) {
cls.set('transaction', null); cls.set('transaction', null);
} }
} }
return this.sequelize.connectionManager.releaseConnection(this.connection);
}; };
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!