Notice
Recent Posts
Recent Comments
Link
| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 1 | 2 | 3 | ||||
| 4 | 5 | 6 | 7 | 8 | 9 | 10 |
| 11 | 12 | 13 | 14 | 15 | 16 | 17 |
| 18 | 19 | 20 | 21 | 22 | 23 | 24 |
| 25 | 26 | 27 | 28 | 29 | 30 | 31 |
Tags
- 코테
- python
- React
- Express
- nuxt
- it
- docker
- Android
- AWS
- 광고 id
- Jenkins
- Next
- 오블완
- spring boots
- NanoHttpd
- 오퍼월
- 코딩테스트
- 티스토리챌린지
- css
- nginx
- toml
- JavaScript
- 파이썬
- kotlin
- chrome
- EC2
- 개발
- 백준
- react-native
- TypeScript
Archives
- Today
- Total
내맘대로 개발일지
[Express] mysql2 Can't add new command when connection is in closed state 본문
Javascript/React
[Express] mysql2 Can't add new command when connection is in closed state
eulBlue 2024. 4. 29. 09:01📱테스트 환경
"express": "^4.18.2"
"typescript": "^5.3.3"
Mysql 이 8시간동안 아무런 작업이없으면 연결이 자동으로 끊킨다고 한다.
그래도 Mysql 을 사용한지 꽤 오래됐는데 .. 지금까지 몰랐다 ...!!
유저가 많다면 상관없겠지만 유저가 없거나 혹시라도 연결이 끊켜서 에러페이지를
보여줄 순 없으니 .. 연결이 끊키지 않도록 해야하는데 Spring 에는 autoReconnect 라는게 있다고 하더라.
근데 Express 에는 이런게 없어서 .. 찾아보다 보니까 비슷한 성능을 낼 수 있는 Pool 이라는게 있다고 한다.
const poolConfig = {
host: "",
port: ,
user: "",
password: "",
database: "",
waitForConnections: true,
connectionLimit: 10,
queueLimit: 0,
};
const pool = mysql2.createPool(poolConfig);
export const getPoolConnection = async () => {
return pool;
};
Pool 연결을 준비해놓고 사용할때는
const db = await getPoolConnection();
const id = res.req.params.id;
await db.execute(
`
DELETE FROM \`like\`
WHERE
like_id = ?
`,
[id]
);
res.sendStatus(200);
이런식으로 사용하면 된다.
[Express] Mysql 연결하기
📱테스트 환경 "express": "^4.18.2" "typescript": "^5.3.3" Mysql 을 연결하기 위해서 일단 해당 모듈을 설치한다. npm install mysql2 그리고 DB 연결을 위한 설정을 진행한다. import mysql2 from "mysql2/promise"; const mysq
8735.tistory.com
Mysql 을 사용하는 방법을 검색해보면 대부분 이런식으로
알려줄거고 나도 이런식으로 개발하곤 했었는데
해당 글도 참고해보고 Pool 을 사용하는 것을 권장한다.
'Javascript > React' 카테고리의 다른 글
| [React] React-Quill + highlight.js (0) | 2024.05.02 |
|---|---|
| [React] searchParams 동적 라우팅 (0) | 2024.04.30 |
| [React] Warning: React does not recognize the prop on a DOM element. (1) | 2024.04.26 |
| [React] 탭 눌렀을 때 다른 곳으로 이동하는 방법 (0) | 2024.04.25 |
| [React] Html Tag 걸러내는 방법 (1) | 2024.04.18 |