[英]How to solve table locking on H2 using Spring Boot and JPA?
我似乎遇到了一個問題,對如何解決沒有真正的線索。
我當前的應用程序基於帶有JPA的Spring Boot,並且在第二次運行時,以下代碼被鎖定。
@RequestMapping(value="/", method = RequestMethod.GET)
public String index() {
repository.save(new RawData("test"));
repository.save(new RawData("test"));
// hangs when the method index() is run 2 sequentially
RawData rawData = rawDataRepository.findOne(1L);
System.out.println(rawData);
return "#: " + repository.count();
}
第一次運行時一切正常,但是執行兩次相同的代碼可以鎖定:
RawData rawData = rawDataRepository.findOne(1L);
當方法掛起或等待超時時,嘗試連接到數據庫也會使我超時。
在Spring Service中調用相同的代碼將導致相同的行為。
@Component
public class SyncService {
@Autowired
RawDataRepository rawDataRepository;
void syncWithRemote() {
// hang on this line...
RawData rawData = rawDataRepository.findOne(1L);
System.out.println(rawData);
}
}
您應該使用兩種技術:
@Version
字段來使用樂觀鎖定 @Transactional
注釋對方法進行注釋,從而添加事務支持。 通常,您還必須通過@EnableTransactionManagement
注釋Configuration類,但是Spring Boot會為您添加注釋 那應該解決你的問題
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.