티스토리 뷰
Spring + Mybatis 사용 시, @Mapper를 사용해 Auto Commit을 한다면 1 row를 insert 할 때, 1번의 Transaction 이 발생하게 됩니다. 이는 DB의 과도한 Transaction을 발생시켜 문제를 야기하기도 합니다. 이를 해결하기 위해 1,000건씩 잘라 Update 하는 방법에 대해 간략히 알아보겠습니다.
1. SqlSessionFactory 주입
@Autowired
private SqlSessionFactory sqlSessionFactory;
2. ExcutorType 작성
SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH);
3. 1000건씩 Batch Update
int cursor = 0;
try{
for(Dto dto : list){
cursor++;
sqlSession.insert("Mapper Namespace 경로.Method 명", dto);
// ex) sqlSession.insert("com.my.api.mapper.BatchTest.insertTest", dto);
if(cursor % 1000 == 0){
sqlSession.flushStatements(); // 쿼리전송
sqlSession.commit(); // 커밋
}
}
} catch(Exception e){
throw e;
} finally {
sqlSession.flushStatements(); // 쿼리전송
sqlSession.commit(); // 커밋
sqlSession.close(); // 닫기
sqlSession.clearCache(); // 캐시비우기
}
감사합니다.
'프레임워크 > SpringBoot' 카테고리의 다른 글
[SpringBoot] Redis 연동하기 (0) | 2024.05.14 |
---|---|
[SpringBoot] TextWebSocketHandler 내부에서 HttpSession 사용하기 (0) | 2024.05.14 |
[SpringBoot] application.yml 멀티 프로필 설정 (0) | 2023.07.13 |
[SpringBoot] properties 파일 static 변수 맵핑하기 (0) | 2023.07.13 |
[SpringBoot] CORS Filter 생성 (0) | 2023.07.04 |
최근에 올라온 글
- Total
- Today
- Yesterday