2025-04-28
@Transactional
์์ฑ | ์ค๋ช | ์์๊ฐ |
---|---|---|
propagation | ํธ๋์ญ์ ์ ํ ๋ฐฉ์ ์ค์ ๊ธฐ์กด ํธ๋์ญ์ ์ด ์์ ๋ ์๋ก์ด ํธ๋์ญ์ ์์ฑ ์ฌ๋ถ ๊ฒฐ์ | REQUIRED , REQUIRES_NEW ๋ฑ |
isolation | ํธ๋์ญ์ ๊ฒฉ๋ฆฌ ์์ค ์ค์ ๋์์ฑ ์ฒ๋ฆฌ ์ ๋ฐ์ดํฐ ์ผ๊ด์ฑ ์ ์ง | READ_COMMITTED , SERIALIZABLE |
timeout | ํธ๋์ญ์ ์ต๋ ์ํ ์๊ฐ ์ค์ ์๊ฐ ์ด๊ณผ ์ ์๋ ๋กค๋ฐฑ | ์ด ๋จ์(์: timeout = 30 ) |
readOnly | ์ฝ๊ธฐ ์ ์ฉ ํธ๋์ญ์ ์ฌ๋ถ ์ค์ ์ฑ๋ฅ ์ต์ ํ ๋ชฉ์ | true / false |
rollbackFor | ๋กค๋ฐฑ ๋์ ์์ธ ์ง์ | rollbackFor = Exception.class |
noRollbackFor | ๋กค๋ฐฑ ์ ์ธ ์์ธ ์ง์ | noRollbackFor = CustomException.class |
<!-- Spring Transaction -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${org.springframework-version}</version>
</dependency>
@Configuration
@EnableTransactionManagement
public class TxConfig {
@Autowired
private DataSource dataSource3;
@Bean
public DataSourceTransactionManager transactionManager() {
return new DataSourceTransactionManager(dataSource3);
}
}
@Bean
public HikariDataSource dataSource3() {
HikariDataSource dataSource = new HikariDataSource();
dataSource.setDriverClassName("com.mysql.cj.jdbc.Driver");
dataSource.setJdbcUrl("jdbc:mysql://localhost:3306/testdb");
dataSource.setUsername("root");
dataSource.setPassword("1234");
return dataSource;
}
@Transactional(
propagation = Propagation.REQUIRED,
isolation = Isolation.READ_COMMITTED,
timeout = 30,
readOnly = false,
rollbackFor = Exception.class
)
public void addMemoTx(MemoDto dto) {
log.info("MemoService's addMemoTx Call! ");
int id = dto.getId();
memoMapper.insert(dto);
dto.setId(id);
memoMapper.insert(dto);
}
@Transactional
์ ๋จ์ํ ์ ์ธํ๋ ๊ฒ๋ฟ๋ง ์๋๋ผ, ์ํฉ์ ๋ง๋ ์์ฑ ์ค์ ์ด ์ค์.