-
-
Save janmeier/608a6593db4c34932aed to your computer and use it in GitHub Desktop.
Sequelize CLS experiments
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
Executing (d5b23ea3-8eec-40b6-8e00-29ae91371cd4): START TRANSACTION; | |
Executing (d5b23ea3-8eec-40b6-8e00-29ae91371cd4): SET SESSION TRANSACTION ISOLATION LEVEL REPEATABLE READ; | |
Executing (789a76d2-b399-4b85-b62c-41b69f07c21a): START TRANSACTION; | |
Executing (d5b23ea3-8eec-40b6-8e00-29ae91371cd4): SET autocommit = 1; | |
Executing (789a76d2-b399-4b85-b62c-41b69f07c21a): SET SESSION TRANSACTION ISOLATION LEVEL REPEATABLE READ; | |
inside transaction 1 | |
d5b23ea3-8eec-40b6-8e00-29ae91371cd4 | |
Executing (789a76d2-b399-4b85-b62c-41b69f07c21a): SET autocommit = 1; | |
inside transaction 2 | |
789a76d2-b399-4b85-b62c-41b69f07c21a | |
inside transaction 1, with timeout | |
d5b23ea3-8eec-40b6-8e00-29ae91371cd4 |
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
var cls = require('continuation-local-storage') | |
, sequelize_cls = cls.createNamespace('sequelize'); | |
var patch = require('cls-bluebird'); | |
patch(sequelize_cls); |
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
var cls = require('continuation-local-storage'); | |
var sequelize_cls = cls.getNamespace('sequelize'); | |
sequelize.sync({ | |
logging: console.log, | |
force: true | |
}).then(function() { | |
sequelize.transaction().then(function () { | |
console.log('inside transaction 1'); | |
console.log(sequelize_cls.get('transaction').id); | |
setTimeout(function () { | |
console.log('inside transaction 1, with timeout'); | |
console.log(sequelize_cls.get('transaction').id); | |
}, 2000); | |
}); | |
sequelize.transaction().then(function () { | |
console.log('inside transaction 2'); | |
console.log(sequelize_cls.get('transaction').id); | |
}); | |
}); |
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
cls = require('continuation-local-storage') | |
, sequelize_cls = cls.getNamespace('sequelize'); | |
Transaction.prototype.prepareEnvironment = function() { | |
var self = this; | |
return sequelize_cls.bind(function () { | |
return Utils.Promise.resolve( | |
self.options.transaction ? self.options.transaction.connection : self.sequelize.connectionManager.getConnection({ uuid: self.id }) | |
).then(function (connection) { | |
self.connection = connection; | |
self.connection.uuid = self.id; | |
}).then(function () { | |
return self.begin(); | |
}).then(function () { | |
return self.setIsolationLevel(); | |
}).then(function () { | |
return self.setAutocommit(); | |
}).tap(function () { | |
sequelize_cls.set('transaction', self); | |
}); | |
})(); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment