Sergio Marcelino
Sergio Marcelino

Reputation: 1146

Sequelize transactions conflict

I want to know a way to create a transaction and to NOT share this between other models, the code below is throwing an exception

Unhandled rejection SequelizeDatabaseError: SQLITE_ERROR: cannot start a transaction within a transaction
    at module.exports.Query.formatError (workspace/node_modules/sequelize/lib/dialects/sqlite/query.js:239:16)
    at Statement.<anonymous> (workspace/node_modules/sequelize/lib/dialects/sqlite/query.js:47:31)
    at Statement.replacement (workspace/node_modules/sqlite3/lib/trace.js:20:31)
    at Statement.replacement (workspace/node_modules/sqlite3/lib/trace.js:20:31)

The model with name 'I will exist' must not be on transaction, but yet sequelize tries to insert it on a transaction created on another function.

return Promise.all([
    sequelize.transaction(function (transaction) {
        return new Promise(function (r) {
            setTimeout(function () {
                r();
            }, 1000);
        }).then(function () {
            User.create({ name : 'Im out of tx' });
        });
    }),
    sequelize.transaction(function (transaction) {
        return Promise.all([
            User.create({ name : 'I will exist' }),
            User.create({ name : 'I will not exist' }, { transaction : transaction })
        ]).then(function () {
            return new Promise(function (r) {
                setTimeout(function () {
                    r();
                }, 1000);
            });
        }).then(function () {
            throw new SaphyreError('ok, now rollback');
        });
    }).then(function () {
        throw new Error('an error should be thrown');
    }).catch(SaphyreError, function (err) {
        expect(err).to.have.property('message').equal('ok, now rollback');

        return User.findAll();
    })
]).spread(function (a, b) {
    expect(b).with.length(2);
});

I want to know how to create a transaction and NEVER share it globally.

Upvotes: 1

Views: 1154

Answers (1)

Sergio Marcelino
Sergio Marcelino

Reputation: 1146

This error occur only on Memory SQLLite config. I don't know but I think when it's memory, the sequelize is unable to handle transactions concurrently.

Maybe it's a bug.

Upvotes: 1

Related Questions