我正在使用Spring Boot創(chuàng)建Rest Api。我正在使用Hikari的默認配置,因此它的池大小為10個連接。當我嘗試將10個并行請求發(fā)布到特定路由時遇到錯誤。錯誤表示Connection不可用,請求在30001ms后超時。 此路由將數(shù)據(jù)保存到MySQL數(shù)據(jù)庫中,通常需要幾毫秒才能完成一次保存操作。為什么會出現(xiàn)這個問題?它應該完成保存操作,然后釋放數(shù)據(jù)庫連接以進行下一個操作嗎?此錯誤似乎僅出現(xiàn)在保存功能創(chuàng)建新實體的保存操作中。屬性spring.jpa.hibernate.ddl-auto=create-drop
spring.datasource.url=jdbc:mysql://192.168.1.88:3306/question?serverTimezone=UTCspring.datasource.username=luigi
spring.datasource.password=root知識庫public interface RandomRepository extends CrudRepository<Random, Integer> {}調(diào)節(jié)器@RestControllerpublic class RandomController {
private RandomRepository randomRepository;
public RandomController(RandomRepository randomRepository) {
this.randomRepository = randomRepository;
}
@GetMapping("/")
public String createRandom() {
return String.valueOf(Math.random());
}
@PostMapping("save")
public Random save(){
Random random = new Random();
random.setNumber(Math.random());
randomRepository.save(random);
return random;
}}我通過使save方法同步找到了解決方案。這是正確的方法嗎?有人遇到過同樣的問題嗎?可能解決方案@PostMapping("save")public synchronized Random save(){
Random random = new Random();
random.setNumber(Math.random());
randomRepository.save(random);
return random;}我預計保存操作將很容易完成,但它會一直停留,直到它在30秒后崩潰
2 回答

慕碼人2483693
TA貢獻1860條經(jīng)驗 獲得超9個贊
使用Spring Boot和Couchbase時,我們遇到了一個非常類似的問題。
當請求數(shù)量很高時,與DB的連接被卡住了。
我們使用的解決方案是轉(zhuǎn)移到所有級別的Async方法調(diào)用 - 從控制器到數(shù)據(jù)庫操作。
添加回答
舉報
0/150
提交
取消