question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Transaction autocommit option

See original GitHub issue

This 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:closed
  • Created 8 years ago
  • Comments:9 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
yonjahcommented, Jun 2, 2017

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.

0reactions
rsshillicommented, Apr 18, 2019

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

14.7.2.2 autocommit, Commit, and Rollback
To use multiple-statement transactions, switch autocommit off with the SQL statement SET autocommit = 0 and end each transaction with COMMIT or ROLLBACK...
Read more >
Transactions - Snowflake Documentation
In this example, the command to set AUTOCOMMIT commits any active transaction. A new transaction is not started immediately. The stored procedure contains...
Read more >
A Guide to Auto-Commit in JDBC - Baeldung
JDBC drivers turn on auto-commit mode for new database connections by default. When it's on, they automatically run each individual SQL ...
Read more >
Auto Commit (Environment setting)—ArcGIS Pro
Auto Commit—The interval at which transactions will automatically commit. If set to 0, transactions will commit only when an explicit commit is issued....
Read more >
Using Transactions - Advanced query tool
By default, every update you make to the database is applied and committed to the database immediately. This is sometimes referred to as...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found