Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java LockModeType.悲观\写入弹簧数据_Java_Spring_Junit_Transactions_Spring Data - Fatal编程技术网

Java LockModeType.悲观\写入弹簧数据

Java LockModeType.悲观\写入弹簧数据,java,spring,junit,transactions,spring-data,Java,Spring,Junit,Transactions,Spring Data,我正在尝试在悲观的write上编写JUnit测试。我的想法是在一个事务中执行这个方法,同时在另一个事务中再次运行它,并尝试修改第一个事务中从该方法返回的元素。我希望出现异常/超时,因为第一次调用应该锁定行。我已经做了类似的事情,但是第二个事务(testNewTrans方法)毫不犹豫地修改了元素。有什么地方我做错了吗 public interface RequestRepository extends CrudRepository<Object, Long> { @Lock(L

我正在尝试在悲观的write上编写JUnit测试。我的想法是在一个事务中执行这个方法,同时在另一个事务中再次运行它,并尝试修改第一个事务中从该方法返回的元素。我希望出现异常/超时,因为第一次调用应该锁定行。我已经做了类似的事情,但是第二个事务(testNewTrans方法)毫不犹豫地修改了元素。有什么地方我做错了吗

public interface RequestRepository extends CrudRepository<Object, Long> {
    @Lock(LockModeType.PESSIMISTIC_WRITE)
    @Query("select ... ")
    List<Object> findABC(Pageable pageable);
}
public interface RequestRepository扩展了crudepository{
@锁(锁模式类型。悲观写入)
@查询(“选择…”)
列表findABC(可分页);
}
少年

@RunWith(SpringRunner.class)
@DataJpaTest
@TransactionConfiguration(defaultRollback = true)
public class RepositoryTest {

@Configuration
@ComponentScan("test.package")
@ContextConfiguration
public static class SpringConfig {

}

@Test
@Transactional(propagation = Propagation.REQUIRES_NEW)
public void testLock() throws InterruptedException {
    List<Object> requests1 = requestRepository.findABC(new PageRequest(0, 2));
    test22.testNewTrans(); // return empty list
}
@RunWith(SpringRunner.class)
@DataJpaTest
@TransactionConfiguration(defaultRollback=true)
公共类存储测试{
@配置
@组件扫描(“测试包”)
@上下文配置
公共静态类SpringConfig{
}
@试验
@事务性(传播=传播。需要\u新建)
public void testLock()引发InterruptedException{
List requests1=requestRepository.findABC(新页面请求(0,2));
test22.testNewTrans();//返回空列表
}
在新课上

@Component
public class Test22 {

private final RequestRepository requestRepository;

public Test22(RequestRepository requestRepository) {
    this.requestRepository = requestRepository;
}
@Transactional(propagation = Propagation.REQUIRES_NEW)
private void testNewTrans() {
    List<Object> requests2 = requestRepository.findABC(new PageRequest(0, 2));
    Object aa = requests2.get(0);
    System.out.println("=============>" + aa);
    aa.setSomething("abc");
}
}
@组件
公共类测试22{
私有最终请求存储库请求存储库;
公共测试22(请求存储库请求存储库){
this.requestRepository=requestRepository;
}
@事务性(传播=传播。需要\u新建)
私有void testNewTrans(){
List requests2=requestRepository.findABC(新页面请求(0,2));
对象aa=requests2.get(0);
System.out.println(“===================>”+aa);
aa.设定值(“abc”);
}
}
直接从
testLock()
调用
testNewTrans()
意味着它不能被spring截获,因此调用
testNewTrans()
忽略任何
@事务性
(和其他)注释


如果您已经自动连接了一个包含
testNewTrans()
的服务并调用了它,您将看到不同的效果。

我可能仍然没有正确连接它。我正在使用spring boot和
@DataJpaTest
对其进行测试。现在我将testNewTrans()放入在一个新类中,如
@component
@ComponentScan
到主类,但它没有获得任何数据。我更新了上面的代码。@kayaman你说没有获得任何数据是什么意思?我可以想象这是一个在单个测试中测试的棘手情况,因为问题案例本质上与多个线程相关。