在 Springboot 中测试幻读和脏读 [英] Test phantom-read and dirty-read in Springboot

查看:159
本文介绍了在 Springboot 中测试幻读和脏读的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

幻读和脏读是初学者最大的陷阱之一,但可以通过设置事务的隔离级别来管理它们.

您可能会争辩说它们可以由 spring-boot 控制.所以不需要测试.但据我所知并非所有数据库都支持隔离级别.那么,我们如何在 springboot 的测试中模拟它们?

解决方案

我用 JUnit5 在 SpringBoot 中模拟了脏读、不可重复读、幻读、丢失更新.

测试结果如下

  1. SQL Server:

<头>
隔离级别脏读不可重复读幻读丢失更新
默认没有发生可能发生可能发生可能发生
READ_UNCOMMITTED 可能发生可能发生可能发生可能发生
READ_COMMITTED没有发生可能发生可能发生可能发生
REPEATABLE_READ没有发生没有发生可能发生没有发生
可序列化没有发生没有发生没有发生没有发生

  1. MySQL

<头>
隔离级别脏读不可重复读幻读丢失更新
默认没有发生没有发生可能发生可能发生
READ_UNCOMMITTED可能发生可能发生可能发生可能发生
READ_COMMITTED没有发生可能发生可能发生可能发生
REPEATABLE_READ没有发生没有发生可能发生可能发生
可序列化没有发生没有发生没有发生没有发生

  1. PostgreSQL

<头>
隔离级别脏读不可重复读幻读丢失更新
默认没有发生可能发生可能发生可能发生
READ_UNCOMMITTED没有发生可能发生可能发生可能发生
READ_COMMITTED没有发生可能发生可能发生可能发生
REPEATABLE_READ没有发生没有发生没有发生没有发生
可序列化没有发生没有发生没有发生没有发生

  1. H2

<头>
隔离级别脏读不可重复读幻读丢失更新
默认没有发生可能发生可能发生可能发生
READ_UNCOMMITTED 可能发生可能发生可能发生可能发生
READ_COMMITTED没有发生可能发生可能发生可能发生
REPEATABLE_READ没有发生没有发生没有发生可能发生
可序列化没有发生没有发生没有发生可能发生

JUnit5 测试示例中参考此项目.

Phantom-read and Dirty-read are one of the biggest pitfalls for beginners, however they can be managed by setting isolation-level of transactions.

you might argue that they can be controlled by spring-boot. so there is no need for a test. but as far as I know not all databases support isolation level. So, how we can simulate them in springboot's tests?

解决方案

I simulate the dirty-read, unrepeatable-read, phantom-read, lost-update in SpringBoot by JUnit5.

The test results are as follows

  1. SQL Server:

Isolation Level Dirty Read Unrepeatable Read Phantom Read Lost Update
DEFAULT not occur may occur may occur may occur
READ_UNCOMMITTED may occur may occur may occur may occur
READ_COMMITTED not occur may occur may occur may occur
REPEATABLE_READ not occur not occur may occur not occur
SERIALIZABLE not occur not occur not occur not occur

  1. MySQL

Isolation Level Dirty Read Unrepeatable Read Phantom Read Lost Update
DEFAULT not occur not occur may occur may occur
READ_UNCOMMITTED may occur may occur may occur may occur
READ_COMMITTED not occur may occur may occur may occur
REPEATABLE_READ not occur not occur may occur may occur
SERIALIZABLE not occur not occur not occur not occur

  1. PostgreSQL

Isolation Level Dirty Read Unrepeatable Read Phantom Read Lost Update
DEFAULT not occur may occur may occur may occur
READ_UNCOMMITTED not occur may occur may occur may occur
READ_COMMITTED not occur may occur may occur may occur
REPEATABLE_READ not occur not occur not occur not occur
SERIALIZABLE not occur not occur not occur not occur

  1. H2

Isolation Level Dirty Read Unrepeatable Read Phantom Read Lost Update
DEFAULT not occur may occur may occur may occur
READ_UNCOMMITTED may occur may occur may occur may occur
READ_COMMITTED not occur may occur may occur may occur
REPEATABLE_READ not occur not occur not occur may occur
SERIALIZABLE not occur not occur not occur may occur

The JUnit5 testing example

You can refer to this project in here.

这篇关于在 Springboot 中测试幻读和脏读的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆