본문 바로가기

Programming Language/Java

Mybatis Executor, PreparedStatement

Mybatis의 Executor type에는 SIMPLE, REUSE, BATCH 3가지가 존재한다.

공식문서의 설명은 아래와 같다.

  • ExecutorType.SIMPLE: 이 타입의 실행자는 아무것도 하지 않는다. 구문 실행마다 새로운 PreparedStatement를 생성한다.
  • ExecutorType.REUSE: 이 타입의 실행자는 PreparedStatements를 재사용할 것이다.
  • ExecutorType.BATCH: 이 실행자는 모든 update구문을 배치처리하고 중간에 select 가 실행될 경우 필요하다면 경계를 표시한다. 이러한 과정은 행위를 좀더 이해하기 쉽게 하기 위함이다.

매퍼 설정

  • 디폴트 실행자(executor) 설정. SIMPLE 실행자는 특별히 하는 것이 없다. REUSE 실행자는 PreparedStatement를 재사용한다. BATCH 실행자는 구문을 재사용하고 수정을 배치처리한다.

DefaultExecutor

 

SimpleExecutor

SimpleExecutor는 공식문서의 설명대로 특별한 동작을 하지 않는다.

매 호출마다 statement를 생성하고 update or query를 실행한다.

ReuseExecutor

ReuseExecutor는 Map으로 이루어진 statementMap이 존재한다.

statementMap에 해당 sql이 존재하면 해당 statement를 재사용한다. statement가 존재하지 않으면, 새로운 statement를 생성하고 statementMap에 추가한다.

BatchExecutor

BatchExecutor는 List로 이루어진 statementList가 존재한다.

doUpdate가 호출될 때 statement를 재사용한다. 현재 sql, statement가 마지막에 사용된 sql, statement와 동일할 때만 statement를 재사용한다.

Ex)

for(4){
	updateA();
}

-> statementList : [ A ]

for(4){
	updateA();
	updateB();
}

-> statementList : [ A, B, A, B, A, B, A, B ]

for(4){
	updateA();

	for(4){
		updateB();
	}
}

-> statementList : [ A, B, A, B, A, B, A, B ]

for(4){
	updateA();
}

for(4){
	updateB();
}

-> statementList : [ A, B ]

'Programming Language > Java' 카테고리의 다른 글

Java 프로그램 개발과 구동  (0) 2021.03.11
JVM, JDK, JRE 란?  (0) 2021.03.10
빌드도구란? (Maven, Gradle)  (0) 2021.02.01