SpringBoot事务隔离级别



我有一个项目试图检查事务隔离。我从READ_UNCOMITTED级别开始,但它不起作用。代码非常简单。

主要类别

@SpringBootApplication
@EnableTransactionManagement
public class HibernateTransactionsLocksTestApplication {
public static void main(String[] args) {
SpringApplication.run(HibernateTransactionsLocksTestApplication.class, args);
}
}

控制器

@RestController
public class HomeController {
private final AccountService accountService;
public HomeController(AccountService accountService) {
this.accountService = accountService;
}
@GetMapping("/updateAccount2RU")
public String updateAccount2RU() throws InterruptedException {
accountService.updateAccount2RU();
return "done!";
}
@GetMapping("/update2Account2RU")
public String update2Account2RU() throws InterruptedException {
accountService.update2Account2RU();
return "done!";
}
}

服务

@Service
public class AccountService {
private final AccountRepository accountRepository;
public AccountService(AccountRepository accountRepository) {
this.accountRepository = accountRepository;
}
@Transactional(isolation = Isolation.READ_UNCOMMITTED)
public void updateAccount2RU() throws InterruptedException {
Account a = accountRepository.findById(2).get();
System.out.println("Account amount: " + a.getAmount());
a.setAmount(a.getAmount()+1);
accountRepository.save(a);
Thread.currentThread().sleep(5000);
}
@Transactional(isolation = Isolation.READ_UNCOMMITTED)
public void update2Account2RU() throws InterruptedException {
Account a = accountRepository.findById(2).get();
System.out.println("Account amount: " + a.getAmount());
a.setAmount(a.getAmount()+1);
Thread.currentThread().sleep(5000);
}
}

Repository是一个简单的SpringData存储库

@Repository
public interface AccountRepository extends JpaRepository<Account, Integer> {
@Transactional(propagation = Propagation.MANDATORY, isolation = Isolation.READ_UNCOMMITTED)
Account findByName(String name);
}

application.properties

server.contextPath=/
spring.datasource.url=jdbc:mysql://localhost:3306/trasactions-locks-tests
spring.datasource.username=
spring.datasource.password=
spring.jpa.properties.hibernate.dialect =org.hibernate.dialect.MySQL5Dialect
spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true
#spring.jpa.properties.hibernate.format_sql=true
logging.level.org.hibernate.SQL=DEBUG
logging.level.org.hibernate.type.descriptor.sql.BasicBinder=TRACE
spring.jpa.open-in-view=false
#Check transactions behaviour
logging.level.org.springframework.transaction.interceptor=TRACE

基本上,我在Chrome中打开2个选项卡并访问updateAccount2RU(这应该读取帐户并增加金额,线程应该在醒来并提交交易之前睡眠5秒(,同时我访问第二个方法update2Account2RU,它读取相同的帐户,金额与从第一个方法读取的相同,而不是更新的方法。

您很可能需要在第一个方法中使用accountRepository.save(a);

SQL更新通常由Hibernate缓冲,直到事务提交,即事务方法返回。

因此,在T2读取记录时,不存在挂起的未提交数据库更改。

@Transactional(isolation = Isolation.READ_UNCOMMITTED)
public void updateAccount2RU() throws InterruptedException {
Account a = accountRepository.findById(2).get();
System.out.println("Account amount: " + a.getAmount());
a.setAmount(a.getAmount()+1);
// force explicit flush so T2 should now see the uncommitted change
accountRepository.saveAndFlush(a);
Thread.currentThread().sleep(5000);
}

参见:

https://docs.jboss.org/hibernate/stable/core.old/reference/en/html/objectstate-flushing.html

会话将不时执行所需的SQL语句将JDBC连接的状态与对象的状态同步保存在内存中。此过程flush默认情况下发生在

  • 在执行某些查询之前
  • 来自org.hibernate.Transaction.commit((
  • 来自会话.flush((

最新更新