Skip to content

MySQL connections have autocommit disabled after transaction with autocommit: false. #9877

@miketroberts

Description

@miketroberts

What are you doing?

const Sequelize = require('sequelize');

async function run() {

    const s = new Sequelize('test', 'user', 'password', {
        host: 'mysql',
        port: '3606',
        dialect: 'mysql',
        pool: {
            max: 1,
            min: 1,
            acquire: 30000,
            idle: 10000,
            log: true
        }
    });

    return s.transaction({autocommit: false}).then((txn) => {
        return s.query('SELECT @@SESSION.autocommit', {transaction: txn}).spread((results, metadata) => {
            console.log(results);
        }).then(() => {
            return txn.commit();
        });
    }).then(() => {
        return s.query('SELECT @@SESSION.autocommit').spread((results, meta) => { console.log(results); });
    }).catch((err) => {
        console.log(err);
    });
}
run();

What do you expect to happen?

I expect the second SELECT statement to show autocommit=1.

What is actually happening?

autocommit=0. The 'SET AUTOCOMMIT = 0' is run inside of the transaction, but affects the session. So when the connection goes back into the pool it's in an unexpected state. Unless the application does all reads within an explicit transaction, it can lead to inconsistent reads.

Executing (b74d6416-e1ed-4e88-9a70-47f076e9bfc8): START TRANSACTION;
Executing (b74d6416-e1ed-4e88-9a70-47f076e9bfc8): SET autocommit = 0;
Executing (b74d6416-e1ed-4e88-9a70-47f076e9bfc8): SELECT @@SESSION.autocommit
[ TextRow { '@@SESSION.autocommit': 0 } ]
Executing (b74d6416-e1ed-4e88-9a70-47f076e9bfc8): COMMIT;
Executing (default): SELECT @@SESSION.autocommit
[ TextRow { '@@SESSION.autocommit': 0 } ]

Dialect: mysql
Dialect version: XXX
Database version: 5.6.34
Sequelize version: 4.38.0
Tested with latest release: Yes

Note : Your issue may be ignored OR closed by maintainers if it's not tested against latest version OR does not follow issue template.

Metadata

Metadata

Assignees

No one assigned

    Labels

    type: bugDEPRECATED: replace with the "bug" issue type

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions