Spring boot Spring JPA-在多个事务中执行更新时事务不工作

Spring boot Spring JPA-在多个事务中执行更新时事务不工作,spring-boot,spring-data-jpa,transactions,spring-data,Spring Boot,Spring Data Jpa,Transactions,Spring Data,@Transactional在我的应用程序中似乎不起作用 我试图在一个过程中更新一个实体,在另一个事务中,响应应该是具有最新值的实体 所有方法都是公共的和带注释的,即使使用REQUIRED_NEW和Isolation.READ_UNCOMMITTED,结果仍然相同 执行http://localhost:8888/example 开始 召唤http://localhost:8888/example/1 我正在更新和返回: {"id":1,"value":&q

@Transactional
在我的应用程序中似乎不起作用

我试图在一个过程中更新一个实体,在另一个事务中,响应应该是具有最新值的实体

所有方法都是公共的和带注释的,即使使用REQUIRED_NEW和Isolation.READ_UNCOMMITTED,结果仍然相同

执行http://localhost:8888/example 开始

召唤http://localhost:8888/example/1 我正在更新和返回:

{"id":1,"value":"OK"}
但是,在第一笔交易结束时,结果并不正确:

{"id":1,"value":"NOT"}
org.springframework.data.jpa.repository.JpaRepository
是否与
@Transaction
一起工作

还是我遗漏了什么

例如:


您应该在
ExampleService#get(最终长id)
上删除无用的
@Transactional
,但这并不能解决问题!
@EnableTransactionManagement
@EnableJpaRepositories("com.example.demo")
@EntityScan(basePackages = {"com.example.demo"})
@SpringBootApplication
public class DemoApplication {

    public static void main(final String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }

}
@Repository
public interface ExampleRepository extends JpaRepository<Example, Long> {}
@Service
public class ExampleService {

    @Autowired
    private ExampleRepository repository;
    
    @Transactional(propagation = Propagation.REQUIRES_NEW, isolation = Isolation.READ_UNCOMMITTED)
    public Example get(final Long id) {
        return repository.findById(id).orElseThrow();
    }

    @Transactional(propagation = Propagation.REQUIRES_NEW, isolation = Isolation.READ_UNCOMMITTED)
    public Example save() {
        return repository.save(Example.builder()
                .value("NOT OK")
                .build());
    }
    
    @Transactional(propagation = Propagation.REQUIRES_NEW, isolation = Isolation.READ_UNCOMMITTED)
    public Example update(final Long id) {
        Example entity = repository.getOne(id);
        entity.setValue("OK");
        Example save = repository.save(entity);
        System.err.println(save);
        return save;
    }
}

@RestController
@RequestMapping("/example")
public class ExampleController {

    @Autowired
    private ExampleService service;

    @GetMapping
    public ResponseEntity<Example> execute(){
        Example entity = service.save();
        Long id = entity.getId();
        LocalDateTime envio = LocalDateTime.now();
        LocalDateTime timeout = envio.plusSeconds(30);
        do {
            
            try {
                Thread.sleep(5000);
            } catch (InterruptedException e) {
                 Thread.currentThread().interrupt();
                throw new IllegalStateException(e);
            }
            System.err.println(service.get(id));
        }
        while (LocalDateTime.now().isBefore(timeout));
        return ResponseEntity.ok(service.get(id));
    }
    

    @GetMapping("{id}")
    public ResponseEntity<Example> update(@PathVariable(value = "id") final Long id){
        return ResponseEntity.ok(service.update(id));
    }
}

2020-09-17 16:04:44.659 TRACE 660 --- [nio-8888-exec-1] o.s.t.i.TransactionInterceptor           : Getting transaction for [com.example.demo.ExampleService.save]
2020-09-17 16:04:44.674 TRACE 660 --- [nio-8888-exec-1] o.s.t.i.TransactionInterceptor           : Getting transaction for [org.springframework.data.jpa.repository.support.SimpleJpaRepository.save]
2020-09-17 16:04:44.693 TRACE 660 --- [nio-8888-exec-1] o.s.t.i.TransactionInterceptor           : Completing transaction for [org.springframework.data.jpa.repository.support.SimpleJpaRepository.save]
2020-09-17 16:04:44.693 TRACE 660 --- [nio-8888-exec-1] o.s.t.i.TransactionInterceptor           : Completing transaction for [com.example.demo.ExampleService.save]
2020-09-17 16:04:49.700 TRACE 660 --- [nio-8888-exec-1] o.s.t.i.TransactionInterceptor           : Getting transaction for [com.example.demo.ExampleService.get]
2020-09-17 16:04:49.704 TRACE 660 --- [nio-8888-exec-1] o.s.t.i.TransactionInterceptor           : Getting transaction for [org.springframework.data.jpa.repository.support.SimpleJpaRepository.findById]
2020-09-17 16:04:49.708 TRACE 660 --- [nio-8888-exec-1] o.s.t.i.TransactionInterceptor           : Completing transaction for [org.springframework.data.jpa.repository.support.SimpleJpaRepository.findById]
2020-09-17 16:04:49.708 TRACE 660 --- [nio-8888-exec-1] o.s.t.i.TransactionInterceptor           : Completing transaction for [com.example.demo.ExampleService.get]
Example(id=1, value=NOT OK)
2020-09-17 16:04:51.639 TRACE 660 --- [nio-8888-exec-2] o.s.t.i.TransactionInterceptor           : Getting transaction for [com.example.demo.ExampleService.update]
2020-09-17 16:04:51.640 TRACE 660 --- [nio-8888-exec-2] o.s.t.i.TransactionInterceptor           : Getting transaction for [org.springframework.data.jpa.repository.support.SimpleJpaRepository.getOne]
2020-09-17 16:04:51.641 TRACE 660 --- [nio-8888-exec-2] o.s.t.i.TransactionInterceptor           : Completing transaction for [org.springframework.data.jpa.repository.support.SimpleJpaRepository.getOne]
2020-09-17 16:04:51.644 TRACE 660 --- [nio-8888-exec-2] o.s.t.i.TransactionInterceptor           : Getting transaction for [org.springframework.data.jpa.repository.support.SimpleJpaRepository.save]
2020-09-17 16:04:51.645 TRACE 660 --- [nio-8888-exec-2] o.s.t.i.TransactionInterceptor           : Completing transaction for [org.springframework.data.jpa.repository.support.SimpleJpaRepository.save]
Example(id=1, value=OK)
2020-09-17 16:04:51.645 TRACE 660 --- [nio-8888-exec-2] o.s.t.i.TransactionInterceptor           : Completing transaction for [com.example.demo.ExampleService.update]
2020-09-17 16:04:54.715 TRACE 660 --- [nio-8888-exec-1] o.s.t.i.TransactionInterceptor           : Getting transaction for [com.example.demo.ExampleService.get]
2020-09-17 16:04:54.716 TRACE 660 --- [nio-8888-exec-1] o.s.t.i.TransactionInterceptor           : Getting transaction for [org.springframework.data.jpa.repository.support.SimpleJpaRepository.findById]
2020-09-17 16:04:54.718 TRACE 660 --- [nio-8888-exec-1] o.s.t.i.TransactionInterceptor           : Completing transaction for [org.springframework.data.jpa.repository.support.SimpleJpaRepository.findById]
2020-09-17 16:04:54.718 TRACE 660 --- [nio-8888-exec-1] o.s.t.i.TransactionInterceptor           : Completing transaction for [com.example.demo.ExampleService.get]
Example(id=1, value=NOT OK)
2020-09-17 16:04:59.722 TRACE 660 --- [nio-8888-exec-1] o.s.t.i.TransactionInterceptor           : Getting transaction for [com.example.demo.ExampleService.get]
2020-09-17 16:04:59.723 TRACE 660 --- [nio-8888-exec-1] o.s.t.i.TransactionInterceptor           : Getting transaction for [org.springframework.data.jpa.repository.support.SimpleJpaRepository.findById]
2020-09-17 16:04:59.724 TRACE 660 --- [nio-8888-exec-1] o.s.t.i.TransactionInterceptor           : Completing transaction for [org.springframework.data.jpa.repository.support.SimpleJpaRepository.findById]
2020-09-17 16:04:59.724 TRACE 660 --- [nio-8888-exec-1] o.s.t.i.TransactionInterceptor           : Completing transaction for [com.example.demo.ExampleService.get]
Example(id=1, value=NOT OK)