๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

Spring๐Ÿƒ

Spring Transaction

728x90

์ถœ์ฒ˜ : ๋‚ด์ผ๋ฐฐ์›€์บ ํ”„

	@Transactional <<< ???
  public List<Folder> addFolders(List<String> folderNames, String name) {

      User user = userRepository.findByUsername(name).orElseThrow(
              () -> new IllegalArgumentException("์‚ฌ์šฉ์ž๊ฐ€ ์กด์žฌํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.")
      );

      // ์ž…๋ ฅ์œผ๋กœ ๋“ค์–ด์˜จ ํด๋” ์ด๋ฆ„์„ ๊ธฐ์ค€์œผ๋กœ, ํšŒ์›์ด ์ด๋ฏธ ์ƒ์„ฑํ•œ ํด๋”๋“ค์„ ์กฐํšŒํ•ฉ๋‹ˆ๋‹ค.
      List<Folder> existFolderList = folderRepository.findAllByUserAndNameIn(user, folderNames);

      List<Folder> folderList = new ArrayList<>();

      for (String folderName : folderNames) {
          // ์ด๋ฏธ ์ƒ์„ฑํ•œ ํด๋”๊ฐ€ ์•„๋‹Œ ๊ฒฝ์šฐ๋งŒ ํด๋” ์ƒ์„ฑ
          if (isExistFolderName(folderName, existFolderList).equals("false")) {
              Folder folder = new Folder(folderName, user);
              folderList.add(folder);
          } else {
              throw new IllegalArgumentException("์ค‘๋ณต๋œ ํด๋”๋ช… ('" + isExistFolderName(folderName, existFolderList) + "')์„ ์‚ญ์ œํ•˜๊ณ  ์žฌ์‹œ๋„ํ•ด ์ฃผ์„ธ์š”");
          }
      }

      return folderRepository.saveAll(folderList);
  }
  • ์œ„์˜ ๋กœ์ง์„ ์ž์„ธํžˆ ํ’€์–ด๋ด…์‹œ๋‹ค!
    1. ํด๋”๋“ค๊ณผ ์ด๋ฆ„์„ ์ธ์ž๋กœ ๋„˜๊ฒจ๋ฐ›๋Š”๋‹ค
    2. ์ž…๋ ฅ์œผ๋กœ ์˜จ ํด๋” ์ด๋ฆ„์„ ๊ธฐ์ค€์œผ๋กœ ํšŒ์›์ด ์ด๋ฏธ ์ƒ์„ฑํ•œ ํด๋”๋ฅผ ์กฐํšŒ
    3. ์ž…๋ ฅ์œผ๋กœ ์˜จ ํด๋”๋ฅผ ์กฐํšŒํ•œ ํด๋”๋“ค๊ณผ ๋น„๊ต
    4. ๋งŒ์•ฝ ์ด๋ฏธ ์ƒ์„ฑํ•œ ํด๋”๊ฐ€ ์•„๋‹ˆ๋ฉด ํด๋”๋ฅผ ์ƒ์„ฑ
    5. ๊ทธ๋Ÿฐ๋ฐ ๋งŒ์•ฝ ๊ทธ ์ค‘์— ํ•˜๋‚˜์˜ ํด๋”๊ฐ€ ์ด๋ฏธ ์žˆ์—ˆ์œผ๋ฉด ํด๋”๋ฅผ ๋งŒ๋“ค์ง€ ์•Š๊ณ  ์—๋Ÿฌ๋ฅผ ๋ฆฌํ„ด
    6. ๊ทธ๋Ÿฐ ๋งŒ์•ฝ 5๊ฐœ์˜ ํด๋”๋ฅผ ๋ฐ›์•„ ์ฒ˜๋ฆฌํ•˜๋˜์ค‘ ์„ธ ๋ฒˆ์งธ ํด๋”๊ฐ€ ์ค‘๋ณต์ธ ๊ฒฝ์šฐ?
    7. ์š”์ฒญ์ด ์‹คํŒจํ–ˆ์œผ๋‹ˆ, ํ•ด๋‹น ๋ฉ”์„œ๋“œ์˜ ์‹คํ–‰ ์ „๊ณผ ๋ฐ์ดํ„ฐ๊ฐ€ ๊ฐ™์•„์•ผ ํ•˜๋ฏ€๋กœ,
    8. ๋‹ค์‹œ ์ง€์›Œ์ค˜์•ผ ํ• ๊นŒ?
    9. ์ด๋Ÿฌํ•œ ๊ฒƒ๋“ค์„ ์œ„ํ•ด์„œ ํŠธ๋žœ์žญ์…˜์„ ํ™œ์šฉํ•œ๋‹ค!

Transaction์ด๋ž€?

  • ๋” ์ด์ƒ ์ชผ๊ฐค ์ˆ˜ ์—†๋Š” ์ตœ์†Œ๋‹จ์œ„์˜ ์ž‘์—…
  • ํ•˜๋‚˜์˜ ์ตœ์†Œ ๋‹จ์œ„์˜ ์ž‘์—…์— ์—ฌ๋Ÿฌ๊ฐ€์ง€ ๋ฐ์ดํ„ฐ ๋ณ€๊ฒฝ์„ ๋„ฃ์œผ๋ฉด, ๋ชจ๋‘ ์ €์žฅ๋˜๊ฑฐ๋‚˜, ์•„๋ฌด ๊ฒƒ๋„ ์ €์žฅ๋˜์ง€ ์•Š๊ฑฐ๋‚˜๋ฅผ ๋ณด์žฅํ•œ๋‹ค.

๋งŒ์•ฝ์— @Transactional์ด ์•„๋‹Œ ๋งค๋‹ˆ์ €๋ฅผ ์ด์šฉํ•œ๋‹ค๋ฉด?

		public List<Folder> addFolders(List<String> folderNames, User user) {
				// ํŠธ๋žœ์žญ์…˜์˜ ์‹œ์ž‘
        TransactionStatus status = this.transactionManager.getTransaction(new DefaultTransactionDefinition());
				try {
	        // 1) ์ž…๋ ฅ์œผ๋กœ ๋“ค์–ด์˜จ ํด๋” ์ด๋ฆ„์„ ๊ธฐ์ค€์œผ๋กœ, ํšŒ์›์ด ์ด๋ฏธ ์ƒ์„ฑํ•œ ํด๋”๋“ค์„ ์กฐํšŒํ•ฉ๋‹ˆ๋‹ค.
	        List<Folder> existFolderList = folderRepository.findAllByUserAndNameIn(user, folderNames);
	
	        List<Folder> savedFolderList = new ArrayList<>();
	        for (String folderName : folderNames) {
	            // 2) ์ด๋ฏธ ์ƒ์„ฑํ•œ ํด๋”๊ฐ€ ์•„๋‹Œ ๊ฒฝ์šฐ๋งŒ ํด๋” ์ƒ์„ฑ
	            if (isExistFolderName(folderName, existFolderList)) {
	                // Exception ๋ฐœ์ƒ!
	                throw new IllegalArgumentException("์ค‘๋ณต๋œ ํด๋”๋ช…์„ ์ œ๊ฑฐํ•ด ์ฃผ์„ธ์š”! ํด๋”๋ช…: " + folderName);
	            } else {
	                Folder folder = new Folder(folderName, user);
	                // ํด๋”๋ช… ์ €์žฅ
	                folder = folderRepository.save(folder);
	                savedFolderList.add(folder);
	            }
	        }

					// ํŠธ๋žœ์žญ์…˜ commit
          transactionManager.commit(status);

	        return savedFolderList;
        } catch (Exception ex) {
            // ํŠธ๋žœ์žญ์…˜ rollback
            transactionManager.rollback(status);
            throw ex;
        }
    }

@Transactional์„ ์‚ฌ์šฉํ•œ ๋กœ์ง

 

728x90