Transaction isolation level and testing

I have a couple of queries that read/write values and I need to define the transaction isolation level.

Repo.query!("set transaction isolation level SERIALIZABLE;")

But when I decided to test I would have an issue saying that the transaction level should be set before any other query.

(Postgrex.Error) ERROR 25001 (active_sql_transaction): SET TRANSACTION ISOLATION LEVEL must be called before any query

This was due to the transaction used in the tests to the database.

I found this StackOverflow reply that works, but only when running that module (that only tests transactions) alone. If I run with other tests with mix test it will not work, saying that it can’t get the connection ownership (the typical error when trying to have a GenServer ran with a connection to a database).

I’ve tried anything I can relate to a shared connection, but without much success. My only option if I want to run this with other tests is to “hide” the query that sets the transaction level in the test env or have it tagged with a specific tag to be run separately.

Not sure if anyone had to able test with transaction isolation level before.

So I wasn’t using the right configuration. The second comment on the StackOverflow post solved the issues I was having.

1 Like