--- src/main/java/itn/let/mjo/msgdata/service/impl/MjonMsgDataDAO.java
+++ src/main/java/itn/let/mjo/msgdata/service/impl/MjonMsgDataDAO.java
... | ... | @@ -1,7 +1,16 @@ |
| 1 | 1 |
package itn.let.mjo.msgdata.service.impl; |
| 2 | 2 |
|
| 3 |
+import java.sql.Connection; |
|
| 4 |
+import java.sql.PreparedStatement; |
|
| 5 |
+import java.sql.SQLException; |
|
| 6 |
+import java.sql.Timestamp; |
|
| 7 |
+import java.time.LocalDateTime; |
|
| 8 |
+import java.time.format.DateTimeFormatter; |
|
| 3 | 9 |
import java.util.List; |
| 4 | 10 |
|
| 11 |
+import javax.sql.DataSource; |
|
| 12 |
+ |
|
| 13 |
+import org.springframework.beans.factory.annotation.Autowired; |
|
| 5 | 14 |
import org.springframework.stereotype.Repository; |
| 6 | 15 |
|
| 7 | 16 |
import egovframework.rte.psl.dataaccess.EgovAbstractDAO; |
... | ... | @@ -17,6 +26,10 @@ |
| 17 | 26 |
@Repository("MjonMsgDataDAO")
|
| 18 | 27 |
public class MjonMsgDataDAO extends EgovAbstractDAO {
|
| 19 | 28 |
|
| 29 |
+ |
|
| 30 |
+ @Autowired |
|
| 31 |
+ private DataSource dataSource; |
|
| 32 |
+ |
|
| 20 | 33 |
@SuppressWarnings("unchecked")
|
| 21 | 34 |
public List<MjonMsgDataVO> selectCcmCmmCodeList() throws Exception{
|
| 22 | 35 |
|
... | ... | @@ -380,4 +393,95 @@ |
| 380 | 393 |
return result; |
| 381 | 394 |
} |
| 382 | 395 |
|
| 396 |
+ |
|
| 397 |
+ /** |
|
| 398 |
+ * 다량 데이터를 Batch 처리로 MJ_MSG_DATA 테이블에 INSERT |
|
| 399 |
+ * |
|
| 400 |
+ * @param mjonMsgSendVOList - 삽입할 데이터 리스트 |
|
| 401 |
+ * @return 성공적으로 삽입된 데이터 건수 |
|
| 402 |
+ */ |
|
| 403 |
+ public int insertMsgDataInfo_jdbc_advc(List<MjonMsgSendVO> mjonMsgSendVOList) {
|
|
| 404 |
+ String sql = "INSERT INTO MJ_MSG_DATA " |
|
| 405 |
+ + "(MSG_ID, MSG_GROUP_ID, USER_ID, AGENT_CODE, CUR_STATE, REQ_DATE, CALL_TO, CALL_FROM, SUBJECT, SMS_TXT, MSG_TYPE) " |
|
| 406 |
+ + "VALUES (?, ?, ?, ?, 0, ?, ?, ?, ?, ?, ?)"; |
|
| 407 |
+ int totalInsertedCount = 0; |
|
| 408 |
+ |
|
| 409 |
+ try (Connection connection = dataSource.getConnection(); |
|
| 410 |
+ PreparedStatement pstmt = connection.prepareStatement(sql)) {
|
|
| 411 |
+ |
|
| 412 |
+ connection.setAutoCommit(false); // Auto-commit 비활성화 |
|
| 413 |
+ |
|
| 414 |
+ int batchSize = 5000; // Batch 크기 설정 |
|
| 415 |
+ int count = 0; |
|
| 416 |
+ |
|
| 417 |
+ // 데이터 리스트 순회하며 PreparedStatement에 추가 |
|
| 418 |
+ for (MjonMsgSendVO vo : mjonMsgSendVOList) {
|
|
| 419 |
+ pstmt.setString(1, vo.getMsgId()); |
|
| 420 |
+ pstmt.setString(2, vo.getMsgGroupId()); |
|
| 421 |
+ pstmt.setString(3, vo.getUserId()); |
|
| 422 |
+ pstmt.setString(4, vo.getAgentCode()); |
|
| 423 |
+ // reqDate 변환 후 설정 |
|
| 424 |
+ Timestamp timestamp = convertToTimestamp(vo.getReqDate()); |
|
| 425 |
+ pstmt.setTimestamp(5, timestamp); |
|
| 426 |
+ |
|
| 427 |
+ pstmt.setString(6, vo.getCallTo()); |
|
| 428 |
+ pstmt.setString(7, vo.getCallFrom()); |
|
| 429 |
+ pstmt.setString(8, vo.getSubject()); |
|
| 430 |
+ pstmt.setString(9, vo.getSmsTxt()); |
|
| 431 |
+ pstmt.setString(10, vo.getMsgType()); |
|
| 432 |
+ |
|
| 433 |
+ pstmt.addBatch(); // Batch에 추가 |
|
| 434 |
+ count++; |
|
| 435 |
+ |
|
| 436 |
+ // Batch 크기마다 실행 |
|
| 437 |
+ if (count % batchSize == 0) {
|
|
| 438 |
+ int[] batchResults = pstmt.executeBatch(); |
|
| 439 |
+ totalInsertedCount += getSuccessCount(batchResults); |
|
| 440 |
+ } |
|
| 441 |
+ } |
|
| 442 |
+ |
|
| 443 |
+ // 남은 데이터 실행 |
|
| 444 |
+ int[] batchResults = pstmt.executeBatch(); |
|
| 445 |
+ totalInsertedCount += getSuccessCount(batchResults); |
|
| 446 |
+ |
|
| 447 |
+ connection.commit(); // 트랜잭션 커밋 |
|
| 448 |
+ |
|
| 449 |
+ } catch (SQLException e) {
|
|
| 450 |
+ throw new RuntimeException("JDBC Batch insert failed", e);
|
|
| 451 |
+ } |
|
| 452 |
+ return totalInsertedCount; // 처리된 전체 데이터 수 반환 |
|
| 453 |
+ } |
|
| 454 |
+ |
|
| 455 |
+ |
|
| 456 |
+ |
|
| 457 |
+ public Timestamp convertToTimestamp(String reqDate) {
|
|
| 458 |
+ try {
|
|
| 459 |
+ // 입력 형식이 yyyy/MM/dd HH:mm:ss인 경우 변환 |
|
| 460 |
+ DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss");
|
|
| 461 |
+ LocalDateTime localDateTime = LocalDateTime.parse(reqDate.trim(), formatter); |
|
| 462 |
+ |
|
| 463 |
+ // LocalDateTime -> Timestamp로 변환 |
|
| 464 |
+ return Timestamp.valueOf(localDateTime); |
|
| 465 |
+ } catch (Exception e) {
|
|
| 466 |
+ throw new IllegalArgumentException("날짜 형식이 올바르지 않습니다: " + reqDate, e);
|
|
| 467 |
+ } |
|
| 468 |
+ } |
|
| 469 |
+ |
|
| 470 |
+ /** |
|
| 471 |
+ * Batch 실행 결과에서 성공한 건수 계산 |
|
| 472 |
+ * |
|
| 473 |
+ * @param batchResults - Batch 실행 결과 배열 |
|
| 474 |
+ * @return 성공적으로 실행된 건수 |
|
| 475 |
+ */ |
|
| 476 |
+ private int getSuccessCount(int[] batchResults) {
|
|
| 477 |
+ int successCount = 0; |
|
| 478 |
+ for (int result : batchResults) {
|
|
| 479 |
+ // 성공적인 실행일 경우 값이 1 또는 Statement.SUCCESS_NO_INFO |
|
| 480 |
+ if (result >= 0 || result == java.sql.Statement.SUCCESS_NO_INFO) {
|
|
| 481 |
+ successCount++; |
|
| 482 |
+ } |
|
| 483 |
+ } |
|
| 484 |
+ return successCount; |
|
| 485 |
+ } |
|
| 486 |
+ |
|
| 383 | 487 |
} |
--- src/main/java/itn/let/mjo/msgdata/service/impl/MjonMsgDataServiceImpl.java
+++ src/main/java/itn/let/mjo/msgdata/service/impl/MjonMsgDataServiceImpl.java
... | ... | @@ -4168,21 +4168,39 @@ |
| 4168 | 4168 |
|
| 4169 | 4169 |
// 시작 시간 측정 |
| 4170 | 4170 |
long startTime = System.currentTimeMillis(); |
| 4171 |
+ System.out.println("==================== insert 시작 ====================");
|
|
| 4171 | 4172 |
|
| 4172 | 4173 |
|
| 4173 | 4174 |
|
| 4174 | 4175 |
log.info("mj_msg_data insert start [{}]", mjonMsgSendVOList.size());
|
| 4175 | 4176 |
|
| 4176 | 4177 |
// 분할 최대건수가 되면 디비에 입력하기 |
| 4177 |
- int instCnt = mjonMsgDataDAO.insertMsgDataInfo_advc(mjonMsgSendVOList); |
|
| 4178 |
+// int instCnt = mjonMsgDataDAO.insertMsgDataInfo_advc(mjonMsgSendVOList); |
|
| 4179 |
+// int instCnt = mjonMsgDataDAO.insertMsgDataInfo_jdbc_advc(mjonMsgSendVOList); |
|
| 4178 | 4180 |
|
| 4181 |
+ // |
|
| 4182 |
+ int instCnt = 0; |
|
| 4183 |
+ int batchSize = 5000; |
|
| 4184 |
+ // Batch 처리 |
|
| 4185 |
+ for (int i = 0; i < mjonMsgSendVOList.size(); i += batchSize) {
|
|
| 4186 |
+ System.out.println(" i :: "+ i);
|
|
| 4187 |
+ // Batch 크기만큼 리스트를 잘라냄 |
|
| 4188 |
+ List<MjonMsgSendVO> batchList = mjonMsgSendVOList.subList( |
|
| 4189 |
+ i, Math.min(i + batchSize, mjonMsgSendVOList.size()) |
|
| 4190 |
+ ); |
|
| 4191 |
+ |
|
| 4192 |
+ // DAO 메서드 호출 |
|
| 4193 |
+ int insertedCount = mjonMsgDataDAO.insertMsgDataInfo_advc(batchList); |
|
| 4194 |
+ instCnt += insertedCount; // 총 삽입된 건수 누적 |
|
| 4195 |
+ } |
|
| 4196 |
+ |
|
| 4179 | 4197 |
|
| 4180 | 4198 |
// 종료 시간 측정 |
| 4181 | 4199 |
long endTime = System.currentTimeMillis(); |
| 4182 | 4200 |
// 실행 시간 계산 (밀리초 -> 초로 변환) |
| 4183 | 4201 |
double executionTimeInSeconds = (endTime - startTime) / 1000.0; |
| 4184 | 4202 |
// 실행 시간 출력 |
| 4185 |
- System.out.println("Execution time: " + executionTimeInSeconds + " seconds");
|
|
| 4203 |
+ System.out.println("Execution time :: " + executionTimeInSeconds + "초 " + "// insert Cnt :: "+instCnt);
|
|
| 4186 | 4204 |
// mjonMsgSendVOList.stream().forEach(t-> System.out.print(t.toString()+"\n") ); |
| 4187 | 4205 |
// mjonMsgSendVOList.stream().forEach(t-> System.out.print(t.toString()+"\n") ); |
| 4188 | 4206 |
|
Add a comment
Delete comment
Once you delete this comment, you won't be able to recover it. Are you sure you want to delete this comment?