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

Commit 3a6352d9 by Sascha Depold

let the transaction set the initial transaction state

1 parent 84c89570
...@@ -464,6 +464,10 @@ module.exports = (function() { ...@@ -464,6 +464,10 @@ module.exports = (function() {
throwMethodUndefined('setAutocommitQuery') throwMethodUndefined('setAutocommitQuery')
}, },
setIsolationLevelQuery: function(value) {
throwMethodUndefined('setIsolationLevelQuery')
},
/** /**
* Returns a query that starts a transaction. * Returns a query that starts a transaction.
* *
......
...@@ -337,6 +337,10 @@ module.exports = (function() { ...@@ -337,6 +337,10 @@ module.exports = (function() {
return "SET autocommit = " + (!!value ? 1 : 0) + ";" return "SET autocommit = " + (!!value ? 1 : 0) + ";"
}, },
setIsolationLevelQuery: function(value) {
return "SET SESSION TRANSACTION ISOLATION LEVEL " + value + ";"
},
/** /**
* Returns a query that starts a transaction. * Returns a query that starts a transaction.
* *
......
...@@ -808,6 +808,15 @@ module.exports = (function() { ...@@ -808,6 +808,15 @@ module.exports = (function() {
return this.queryAndEmit([sql, null, { transaction: transaction }], 'setAutocommit') return this.queryAndEmit([sql, null, { transaction: transaction }], 'setAutocommit')
} }
QueryInterface.prototype.setIsolationLevel = function(transaction, value) {
if (!transaction || !(transaction instanceof Transaction)) {
throw new Error('Unable to set isolatipn level for a transaction without transaction object!')
}
var sql = this.QueryGenerator.setIsolationLevelQuery(value)
return this.queryAndEmit([sql, null, { transaction: transaction }], 'setIsolationLevel')
}
QueryInterface.prototype.startTransaction = function(transaction, options) { QueryInterface.prototype.startTransaction = function(transaction, options) {
if (!transaction || !(transaction instanceof Transaction)) { if (!transaction || !(transaction instanceof Transaction)) {
throw new Error('Unable to start a transaction without transaction object!') throw new Error('Unable to start a transaction without transaction object!')
......
...@@ -393,28 +393,15 @@ module.exports = (function() { ...@@ -393,28 +393,15 @@ module.exports = (function() {
, transaction = new Transaction(this, options) , transaction = new Transaction(this, options)
, self = this , self = this
var startTransaction = function() {
self
.getQueryInterface()
.startTransaction(transaction, {})
.success(function() {
callback(transaction)
})
}
var setAutocommit = function(value, cb) {
self
.getQueryInterface()
.setAutocommit(transaction, value)
.success(cb)
}
Utils.tick(function() { Utils.tick(function() {
if (options.hasOwnProperty('autocommit')) { transaction.prepareEnvironment(function() {
setAutocommit(!!options.autocommit, startTransaction) self
} else { .getQueryInterface()
startTransaction() .startTransaction(transaction, {})
} .success(function() {
callback(transaction)
})
})
}) })
return transaction return transaction
......
...@@ -3,12 +3,22 @@ var Utils = require('./utils') ...@@ -3,12 +3,22 @@ var Utils = require('./utils')
var Transaction = module.exports = function(sequelize, options) { var Transaction = module.exports = function(sequelize, options) {
this.sequelize = sequelize this.sequelize = sequelize
this.options = options || {}
this.id = Utils.generateUUID() this.id = Utils.generateUUID()
this.options = Utils._.extend({
autocommit: true,
isolationLevel: Transaction.ISOLATION_LEVELS.REPEATABLE_READ
}, options || {})
} }
util.inherits(Transaction, Utils.CustomEventEmitter) util.inherits(Transaction, Utils.CustomEventEmitter)
Transaction.ISOLATION_LEVELS = {
READ_UNCOMMITTED: "READ UNCOMMITTED",
READ_COMMITTED: "READ COMMITTED",
REPEATABLE_READ: "REPEATABLE READ",
SERIALIZABLE: "SERIALIZABLE"
}
Transaction.prototype.commit = function() { Transaction.prototype.commit = function() {
return this return this
.sequelize .sequelize
...@@ -25,3 +35,29 @@ Transaction.prototype.rollback = function() { ...@@ -25,3 +35,29 @@ Transaction.prototype.rollback = function() {
.rollbackTransaction(this, {}) .rollbackTransaction(this, {})
.proxy(this) .proxy(this)
} }
Transaction.prototype.prepareEnvironment = function(callback) {
var self = this
this.setIsolationLevel(function() {
self.setAutocommit(function() {
callback()
})
})
}
Transaction.prototype.setAutocommit = function(callback) {
this
.sequelize
.getQueryInterface()
.setAutocommit(this, this.options.autocommit)
.success(callback)
}
Transaction.prototype.setIsolationLevel = function(callback) {
this
.sequelize
.getQueryInterface()
.setIsolationLevel(this, this.options.isolationLevel)
.success(callback)
}
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!