0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Spring Boot トランザクション

Last updated at Posted at 2025-02-17

はじめに

spring bootのトランザクションの貼り方について
@Transactional アノテーションでトランザクション管理で学習したのでまとめます。

貼り方

コードを一部抜粋

package com.example.managingtransactions;

import java.util.List;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;

// Transactionを利用したいファイルでインポートを行う
import org.springframework.transaction.annotation.Transactional; 


@Component
public class BookingService {

    private final JdbcTemplate jdbcTemplate;

    public BookingService(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }
    
   // Transactionを貼りたい場所でアノテーションを設定する
    @Transactional

    // Stringの配列を引数で受け取り一件ずつInsert
    public void book(String... persons) {
        for(String person : persons) {
            jdbcTemplate.update("insert into BOOKINGS(FIRST_NAME) values (?)", person);
        }
    }
}

配列の要素が全てInsert成功時のみCommitされる。
失敗したらRollbackされる

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?