Akh*_*mar 5 junit spring integration-testing unit-testing jpa
我在JPA信息库中使用spring。
我正在尝试从一张表中获取序列号,并在进行一些操作后对其进行更新。
在这种情况下,我必须编写多线程事务的测试代码,即,如果一个请求从选择查询中获得了序列号,那么在第一个请求完成之前,另一个请求将无法访问该序列号。所以我已经在互联网上搜索并找到了JPA的解决方案,即
@Lock(LockModeType.PESSIMISTIC_WRITE)
Run Code Online (Sandbox Code Playgroud)
我已经用我的存储库功能实现了上述声明,即
@Lock(LockModeType.PESSIMISTIC_WRITE)
@Query(value = "from FileNumberSequence where headBasicId = :headBasicId and headPrimaryId= :headPrimaryId and year = :year")
public FileNumberSequence getFileNumberSequence(@Param("headBasicId") Long headBasicId,
@Param("headPrimaryId") Long headPrimaryId, @Param("year") Integer year);
Run Code Online (Sandbox Code Playgroud)
可能上面的代码在生产环境中可以正常工作,但是我必须编写相同的测试代码。
所以我已经通过两次交易对它进行了测试,即
TransactionStatus status1 = transactionManager.getTransaction(new DefaultTransactionDefinition());
this.mockMvc
.perform(MockMvcRequestBuilders.post("/file").contentType(MediaType.APPLICATION_JSON)
.content(new ObjectMapper().writeValueAsString(this.fileDTO)))
.andExpect(MockMvcResultMatchers.status().isOk());
transactionManager.commit(status1);
DefaultTransactionDefinition transactionDefinition = new DefaultTransactionDefinition();
transactionDefinition.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW);
TransactionStatus status2 = transactionManager.getTransaction(transactionDefinition);
this.mockMvc
.perform(MockMvcRequestBuilders.post("/file").contentType(MediaType.APPLICATION_JSON)
.content(new ObjectMapper().writeValueAsString(this.fileDTO)))
.andExpect(MockMvcResultMatchers.status().isOk());
transactionManager.commit(status2);
Run Code Online (Sandbox Code Playgroud)
在上述情况下,第一个请求会触发选择查询,但此后它不再起作用。
请提供相同的解决方案。如果您知道用其他方法编写相同的测试代码,请解释。
| 归档时间: |
|
| 查看次数: |
174 次 |
| 最近记录: |