0

I´ve been finding a solution for my issue of not working Spring Boot @Serializable and @Transactional on SO and I´ve found this solution

Everything works fine after removing @Synchronized and adding @Transactional with SERIALIZABLE isolation level on top of my method which causes problem in race condition.

But when I try to run @SpringBootTest I am getting error

Could not open JPA EntityManager for transaction; nested exception is org.postgresql.util.PSQLException: Cannot change transaction isolation level in the middle of a transaction.

I´ve tried to add a REQUIRES_NEW propagation to force create a brand new transaction, but that did not work either.

The test is basically only calling a controller method using MockMvc. Controller method just calls service method (on top of that method is that Transactional method) and the method simply reads data and if the data are present, update of this data is performed, otherwise the new entity is created.

And in the end I´d like to add that I am using PostgreSQL database

Does anyone have a clue why this happens and how to possibly fix this issue?

Johnczek
  • 532
  • 9
  • 30
  • Because your test is managing the transaction, if you are using `@SpringBootTest` that has `@Transactional` so your test method is the transactional boundary and thus there already is a transaction (which is what the error message is telling you). – M. Deinum May 15 '23 at 11:53
  • @M.Deinum yea, I get transactional management philosophy, but why is it failing even if I add required new strategy? In that case, whole new transaction just for my case should be created. Plus how can I, therefore, use Isolation level serializable for some method? – Johnczek May 15 '23 at 12:49
  • Make your test non-transactional. Although you can start a new transaction you cannot modify the isolation level, simply because that is (despite the place you can configure it) configured on the actual connection to the database. Once set you cannot change that inflight. – M. Deinum May 15 '23 at 12:56
  • @M.Deinum How Well I need to test it as it is- controller test that just testsbehaviour of that endpoint (that has internally only that one, isolated, transaction inside. I´ve now tried to call that endpoint manually (where the only transaction is that isolated one on the service method- no other calls from controller are not present) and I am getting the same error. – Johnczek May 15 '23 at 13:12
  • I yield. There is too little information in your question to answer this. No test, no actual full stacktrace, or configuration. – M. Deinum May 15 '23 at 13:18

0 Answers0