iT邦幫忙

0

Spring Boot Call API 堵塞調整

  • 分享至 

  • xImage

最近在學Spring Boot
不過在測試時發現如果同時Call同一個API會等前一個Request完成後才會執行第二個Request
請問一下要如何改成可以同時處理多個Request

附上重現程式:
使用Java版本

openjdk version "1.8.0-332"
OpenJDK Runtime Environment (build 1.8.0-332-b09)
OpenJDK 64-Bit Server VM (build 25.71-b09, mixed mode)

controller:

package net.microsugar.lab.microsugar.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import lombok.extern.slf4j.Slf4j;
import net.microsugar.lab.microsugar.entity.TbLabEntity;
import net.microsugar.lab.microsugar.service.TestService;

@Slf4j
@RestController
@RequestMapping(value = "/tt")
public class TestController {

    @Autowired
	private TestService testService;

    @GetMapping("/test")
    public String addMemberPage(){
        log.info("in method");
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            // e.printStackTrace();
            // handle the exception...        
            // For example consider calling Thread.currentThread().interrupt(); here.
        }
        testService.doTest();
        log.info("sql finsh");
        return "addMemberPage";
    }
}

service:

package net.microsugar.lab.microsugar.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import net.microsugar.lab.microsugar.dao.TbLabDao;

@Service
public class TestService {
    @Autowired
	private TbLabDao tbLabDao;

    public void doTest(){
        tbLabDao.doTest(1);
    }
}

dao:

package net.microsugar.lab.microsugar.dao;

import net.microsugar.lab.microsugar.entity.TbLabEntity;
import org.springframework.stereotype.Repository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;

@Repository
public interface TbLabDao extends CrudRepository<TbLabEntity, Long>{
    @Query(value="WAITFOR DELAY N'00:00:05.000' SELECT SEQNO, EMAIL from TBLAB WHERE SEQNO=?", nativeQuery = true)
    TbLabEntity doTest(int SEQNO);
}

重現方式:
瀏覽器開兩個分頁都開http://localhost:8080/tt/test
會得到

2022-08-17 23:37:11.026  INFO 3748 --- [nio-8080-exec-1] n.m.l.m.controller.TestController        : in method
Hibernate: WAITFOR DELAY N'00:00:10.000' SELECT SEQNO, EMAIL from TBLAB WHERE SEQNO=?
2022-08-17 23:37:26.230  INFO 3748 --- [nio-8080-exec-1] n.m.l.m.controller.TestController        : sql finsh
2022-08-17 23:37:26.265  INFO 3748 --- [nio-8080-exec-3] n.m.l.m.controller.TestController        : in method
Hibernate: WAITFOR DELAY N'00:00:10.000' SELECT SEQNO, EMAIL from TBLAB WHERE SEQNO=?
2022-08-17 23:37:41.287  INFO 3748 --- [nio-8080-exec-3] n.m.l.m.controller.TestController        : sql finsh

第二個Request會等第一個Request結束後才開始處理
我希望是狀況是下面這種

in method
in method
Hibernate: WAITFOR DELAY N'00:00:10.000' SELECT SEQNO, EMAIL from TBLAB WHERE SEQNO=?
Hibernate: WAITFOR DELAY N'00:00:10.000' SELECT SEQNO, EMAIL from TBLAB WHERE SEQNO=?
sql finsh
sql finsh

PS: 20220817 23:39 發現原本提供的Sample呈現問題好像不太準,有做調整

圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友回答

立即登入回答