objection.js icon indicating copy to clipboard operation
objection.js copied to clipboard

Cannot set transaction isolation level

Open AleG94 opened this issue 6 years ago • 2 comments

The official documentation shows the following example code to set the isolation level for a transaction:

const scrappy = await Person.transaction(async trx => {
    await trx.raw('SET TRANSACTION ISOLATION LEVEL SERIALIZABLE');
});

But when performing the raw query, the transaction has already began so I get the following error:

Transaction characteristics can't be changed while a transaction is in progress

I'm using the mysql2 driver along with MariaDB 10.4.8.

Am I missing something? How can I set the isolation level for the transaction?

AleG94 avatar Oct 21 '20 13:10 AleG94

The example works on postgres. See the mysql documentation. Weirdly it seems that on mysql, you can't set the isolation level of an ongoing transaction. You can only set the isolation level of the next transaction, which is insanely unsafe in a multi-threaded database. Another transaction may get executed before the one you meant and things will break.

Unfortunately it would seem that you cannot use mysql isolation levels with knex or objection, unless you set the connection pool size to 1.

koskimas avatar Oct 21 '20 21:10 koskimas

https://github.com/knex/knex/issues/581

koskimas avatar Oct 21 '20 21:10 koskimas