Search code examples
spring-bootspring-transactionsisolation-level

@Transactional isolation level seems to be not working


i have some question about @Transactional, a.k.a spring-tx.

In code like this.

@Transactional(propagation = Propagation.REQUIRES_NEW, isolation = Isolation.SERIALIZABLE)
public TestObject testTransaction(Long id, String name, String content) {
    //This is find
    TestObject testObject = testObjectRepository.findById(id).orElse(null);

    if (testObject != null) {
        System.out.println(testObject.getId() + "/"
                + testObject.getName() + "/"
                + testObject.getContent());
    }

    // update something
    testObject.changeContent(name, content);
    slowQuery();
    //delay for late
    return testObject;
}

If i use postman to request something like this.

first request is http://localhost:8080/spring/test/transaction?id=1&name=123&content=123
second request is http://localhost:8080/spring/test/transaction?id=1&name=456&content=456

Then I guess, first request is hanging on transaction, then second request must be reject from Spring-tx because of first request transaction's isolation level is SERIALIZABLE, and transaction work is not complete.

But result of two request is

{
  "name":"456",
  "content":"456"
}

Is anyone know about transactional isolation I use mysql with docker, and spring-data-jpa also.


Solution

  • If you use @Transactional annotation then to ensure there are commits taken place in db use one more annotation as @EnableTransactionManagement also include the below code

    connection.setTransactionIsolation(Connection.TRANSACTION_READ_UNCOMMITTED);
    

    For more details regarding the same follow this link