繁体   English   中英

Spring Crud 存储库 - 事务性不起作用

[英]Spring Crud Repository - Transactional doesn't work

我试图在JpaRepositories中设置READ_UNCOMMITED隔离级别。 我的设置如下代码片段:

禁用JpaRepository中的默认事务设置:

@Transactional(propagation = Propagation.NEVER, isolation = Isolation.READ_UNCOMMITTED)
public interface PersonRepository extends JpaRepository<Person, Integer> {
}

设置READ_UNCOMMITED隔离级别:

@Service
@AllArgsConstructor
@Slf4j
public class PersonService {

    private PersonRepository personRepository;

    @Transactional(propagation = Propagation.REQUIRES_NEW, isolation = Isolation.READ_UNCOMMITTED)
    public void persist(String firstName, String lastName, String career) {
        log.info("Begin transaction");
        Person person = new Person();
        person.setCareer(career);
        person.setFirstName(firstName);
        person.setLastName(lastName);
        log.info("Before entity saved");
        sleepFiveSeconds();
        personRepository.save(person);
        log.info("Person saved");
        sleepFiveSeconds();
        log.info("End transaction");
    }

    @Transactional(propagation = Propagation.REQUIRES_NEW, isolation = Isolation.READ_UNCOMMITTED)
    public List<Person> findAll() {
        return personRepository.findAll();
    }
    private void sleepFiveSeconds() {
        try {
            Thread.sleep(5_000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

}

执行以下测试:

@RunWith(SpringRunner.class)
@SpringBootTest
class PersonServiceTest {
    @Autowired
    PersonService personService;
    @Autowired
    PersonRepository personRepository;
    @Test
    public void test() throws Exception {
        new Thread(() -> {
            personService.persist("John", "Johnson", "Dev");
        }).start();
        for(;;) {
            Thread.sleep(1000);
            System.out.println(personService.findAll().size());
        }
    }
}

打印 output:

2021-02-12 00:43:02.083  INFO 15452 --- [           main] c.e.t.dao.PersonServiceTest              : Started PersonServiceTest in 4.801 seconds (JVM running for 6.021)
2021-02-12 00:43:02.335  INFO 15452 --- [       Thread-2] c.e.transactionsdemo.dao.PersonService   : Begin transaction
2021-02-12 00:43:02.336  INFO 15452 --- [       Thread-2] c.e.transactionsdemo.dao.PersonService   : Before entity saved
0
0
0
0
2021-02-12 00:43:07.355  INFO 15452 --- [       Thread-2] c.e.transactionsdemo.dao.PersonService   : Person saved
0
0
0
0
0
2021-02-12 00:43:12.355  INFO 15452 --- [       Thread-2] c.e.transactionsdemo.dao.PersonService   : End transaction
1
1
1
1
2021-02-12 00:43:15.841  INFO 15452 --- [extShutdownHook] o.s.s.concurrent.ThreadPoolTaskExecutor  : Shutting down ExecutorService 'applicationTaskExecutor'

问题是:为什么我在接口PersonRepository上的注释 @Transactional 不起作用并且在persorRepository.save(person)之后执行提交?

如何在PersonService中实现READ_UNCOMMITED隔离?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM