Transaction autocommit option
See original GitHub issueThis may be less of a bug and more of a mis-understanding. This regards the transaction autocommit
option, as detailed here (http://sequelize.readthedocs.org/en/stable/api/sequelize/#transactionoptions-promise).
First of all, it seems like this is unnecessary (as on MySQL at least a transaction is an implicit autocommit: false. See https://dev.mysql.com/doc/refman/5.0/en/commit.html : “To disable autocommit mode implicitly for a single series of statements, use the START TRANSACTION statement:”). Maybe this is not the case for other dialects, or I am missing the point of this field.
Regardless, code I am working on upgrading from 1.7 to 2.0 was was already using transaction(autocommit: false)
, and claimed to need it for certain bugs (outside the scope of this issue). While working out the kinks, I noticed that applying this flag keeps the connection it’s run on with autocommit mode disabled, even after the transaction completes. My intuition is that with the flag being supplied in the transaction call, it would be reset back at the end of the transaction. If that’s not the case, maybe fix here is as simple as some documentation as to this option.
Here is the code I am using to see this
Sequelize = require 'sequelize'
Promise = require 'bluebird'
config =
host: '127.0.0.1'
dialect: 'mysql'
sync: {force: false}
syncOnAssociation: false
define:
timestamps: false
underscored: true
freezeTableName: true
syncOnAssociation: false
maxConcurrentQueries: 250
pool: { maxConnections: 1 }
sequelize = new Sequelize 'test', 'user', 'pass', config
Test = sequelize.define 'test',
thing: Sequelize.STRING
showAutocommit = ->
sequelize.query("show variables like 'autocommit'")
.spread (results) ->
console.log results[0].Value
Test.sync(force: true)
.then showAutocommit
.then ->
sequelize.transaction(autocommit: false)
.then (t) ->
options = transaction: t
Test.create(
{
thing: "transaction"
},
options
)
.then ->
t.commit()
.catch ->
t.rollback()
# autocommit is now OFF for this session. Should this be the case?
.then showAutocommit
Also I know coffeescript isn’t this repo’s thing, LMK if you’d like the test code in Javascript, or any other info. Thanks!
Issue Analytics
- State:
- Created 8 years ago
- Comments:9 (5 by maintainers)
Just noticed this issue (After about 2 days of debugging weird concurrency issues)
Can this be ported back to 3.x ? Even under some feature flag ? The way things are currently transactions are basically broken
Also I’m not sure if this fix completely solves this issue. At least with mysql autocommit is a global setting that will leak outside of the transaction. So it goods it’s not being set by default. But if you’ll try to set autocommit to false since autocommit will not be activated back when the transaction ends this will mess all future queries. It’s probably be a bit messy to set the autocommit back to its original value but I think this can be converted to no op method since with mysql it doesn’t make any sense to set it in the transaction level and having the option there especially when it leaks is really confusing.
This only took me forever to debug this one.
Why isn’t anything getting saved to the DB even though there are perfectly good SQL statements being spit out to the log? OMG autocommit got turned off permanently in a transaction somewhere.