New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Set transaction isolation level #581
Comments
This would definitely be nice 👍 knex.transaction(function(trx) {
return trx
.raw('set transaction isolation level repeatable read;')
.then(function() {
return trx.select(/* ... */);
});
}) Does this look correct to you? knex.transaction({isolation: 'repeatable read'}, function(trx) { /* ... */ });
knex.transaction.repeatableRead(function(trx) { /* ... */ }); |
Your workaround looks correct. As for the new feature, I'd go with the first version but invert the arguments. Callback first, options second. |
Thanks! I suggested the callback as the last argument for CoffeeScript / ES6 support, where the |
@rprieto's workaround won't work for MySQL, since "SET TRANSACTION ISOLATION LEVEL" applies to the next transaction, not the current one. From: https://dev.mysql.com/doc/refman/5.0/en/set-transaction.html
|
:( This is a blocker for me, and is sad cuz I really like bookshelf/knex combination. If there where some work around to this for mysql I'll be very happy to hear. |
@rprieto's suggestion for the backwards compatible API looks good to me. #581 (comment) EDIT: deleted some "any news status update spam" if there are news, you can see them from this issue comments. |
@honestserpent I could try working on it. Which databases are you interested in? Would you consider contributing sample SQLs to illustrate what expected output is? |
@kibertoad I'm working on Postgres. |
For mysql you can simply do:
This will set the transaction isolation level to READ COMMITTED for the next transaction only. |
@Recodify That doesn't work unless you manage to make sure that those both queries are sent through same connection to DB server. Knex doesn't guarantee that even if pool size is 1 (it might automatically refresh broken/timed out connection). |
@elhigu you're right! If I print the current isolation level I can see that indeed it still prints the default
The strange thing is that it DOES change the behavior of the following code...which is what led me to believe it worked. I'm missing something, will continue to investigate |
With some luck it works, but it is not guaranteed to work. So it may fail for example if there are more concurrent queries executed at the same time. If it works it just means that knex did return the same connection for both of the queries. |
It has to be more than luck, I've just hit it with 5000 concurrent requests with connection pool settings of: pool: { min: 0, max: 20 } and it works as expected with the behavior clearly that of READ COMMITTED, removing the line that sets this isolation level results in a pretty fast fail due to REPEATABLE READ behavior. |
In next knex version you can setup listeners to pool internal operations and see why it returns always the same connection for both of the queries in that case. You can also try break it more by adding |
Anyways this is all really unrelated to this issues, which is about having real support by knex to set the isolation level. |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
Okay, so how do I do this with mysql, mssql? The |
Implemented as |
For PostgreSQL, I'm currently using this and it works fine:
Is my code above equivalent to |
Isolations level are very specific ideas relating to what happens when other transactions try to reference the same data that you're using in your current transaction, basically from no checks to full table locks / aborts The deferred constraint checking is whether checks occur immediately or at the end of the transaction |
In case you need to specify an isolation level for your transaction, you can use a config parameter isolationLevel. Not supported by oracle and sqlite, options are read uncommitted, read committed, repeatable read, snapshot (mssql only), serializable. const isolationLevel = 'read committed'; |
I came here from Google but it seems the feature is implemented by #4185 ( |
Where are you calling knex? |
Im passing it as a CB from the objection Model. |
You set the isolation level wherever you get that Transaction from knex. |
It'd be nice to have a way to specify the isolation level on the transaction
Also mentioned by @llambda in #138, it'd be nice to have both a better api for injecting connection specific config default settings (currently it's possible with an
afterCreate
hook)The text was updated successfully, but these errors were encountered: