spring 隔离级别 测试代码
程序员文章站
2022-06-14 16:40:53
...
@Controller
@RequestMapping("/test")
@Api(value = "测试", description = "测试")
public class TestController {
@Autowired
private TestService testService;
@RequestMapping(value = "listForDirtyRead", method = RequestMethod.GET)
@ResponseBody
@ApiImplicitParams({})
@ApiOperation(value="listForDirtyRead")
public Object listForDirtyRead() {
return testService.listForDirtyRead();
}
@RequestMapping(value = "insertForDirtyReadAndIllusion", method = RequestMethod.POST)
@ResponseBody
@ApiImplicitParams({})
@ApiOperation(value="insertForDirtyReadAndIllusion")
public void insertForDirtyReadAndIllusion() {
testService.insertForDirtyReadAndIllusion();
}
@RequestMapping(value = "listForIllusionRead", method = RequestMethod.GET)
@ResponseBody
@ApiImplicitParams({})
@ApiOperation(value="listForIllusionRead")
public Object listForIllusionRead() {
return testService.listForIllusionRead();
}
@RequestMapping(value = "updateForNoRepeat", method = RequestMethod.POST)
@ResponseBody
@ApiImplicitParams({})
@ApiOperation(value="updateForNoRepeat")
public void updateForNoRepeat() {
testService.updateForNoRepeat();
}
@RequestMapping(value = "deleteForNoRepeat", method = RequestMethod.POST)
@ResponseBody
@ApiImplicitParams({})
@ApiOperation(value="deleteForNoRepeat")
public void deleteForNoRepeat() {
testService.deleteForNoRepeat();
}
}
@Service
public class TestService {
@Autowired
private JdbcTemplate jdbcTemplate;
@Transactional(isolation = Isolation.READ_COMMITTED)
public List<Map<String,Object>> listForDirtyRead() {
List<Map<String,Object>> map = jdbcTemplate.queryForList("select * from tao");
return map;
}
@Transactional
public void insertForDirtyReadAndIllusion () {
jdbcTemplate.execute("insert into tao values (1,'d')");
try {
Thread.sleep(00000);
} catch (InterruptedException e) {
e.printStackTrace();
}
// int a = 1/0;
}
@Transactional(isolation = Isolation.REPEATABLE_READ)
public Object listForIllusionRead() {
List<Map<String,Object>> map = jdbcTemplate.queryForList("select * from tao");
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
e.printStackTrace();
}
List<Map<String,Object>> map2 = jdbcTemplate.queryForList("select * from tao");
Map<String,Object> res = new HashMap<String, Object>();
res.put("before", map);
res.put("after", map2);
return res;
}
public void updateForNoRepeat () {
jdbcTemplate.execute("update tao set col2 = 'e'");
}
public void deleteForNoRepeat() {
jdbcTemplate.execute("delete from tao ");
}
}
数据库:
- CREATE TABLE `tao` (
- `col1` tinyint(3) unsigned NOT NULL DEFAULT '0',
- `col2` varchar(20) DEFAULT NULL,
- PRIMARY KEY (`col1`)
- ) ENGINE=InnoDB DEFAULT CHARSET=utf8
推荐阅读
-
Hibernate的Session_flush与隔离级别代码详解
-
Spring+Junit4进行接口测试实例代码
-
RR与RC隔离级别下索引和锁的测试脚本示例代码
-
什么是事务、事务特性、事务隔离级别、spring事务传播特性
-
Spring事务传播属性和隔离级别
-
SQL Server死锁中的会话隔离级别为序列化(Serializable)实验测试
-
Spring+SpringMVC+MyBatis+easyUI整合优化篇(三)代码测试
-
Spring+SpringMVC+MyBatis+easyUI整合优化篇(三)代码测试
-
spring事务的传播行为与隔离级别(一)
-
[spring]事务传播级别隔离级别以及高并发下的应用经验