본문 바로가기

Java/Spring

@Transactional

@Transactional

@Transactional 어노테이션을 사용하면 트랜잭션 범위를 쉽게 지정할 수 있다. 트랜잭션 범위에서 실행하고 싶은 메서드에 어노테이션을 붙이면 메서드 안의 코드를 하나의 트랜잭션 범위에서 실행한다. 

트랜잭션으로 묶인 쿼리 중 하나라도 실패하면 전체 쿼리를 실패한 것으로 판단하고 롤백(rollback)되며, 모두 성공적으로 동작하면 커밋(commit)하여 데이터베이스에 실제로 반영한다.

설정

트랜잭션 범위에서 실행할 메서드에 @Transactional 어노테이션을 붙인다.

xml

<?xml version="1.0" encoding="UTF-8"?>
 
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx.xsd">
 
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
        destroy-method="closeConnection">
        ...
    </bean>
    
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource" />
    </bean>
    
    <tx:annotation-driven transaction-manager="transactionManager"/>
    
...
    </bean>
</beans>
cs

Java

자바 설정을 이용할 땐 아래와 같다.
@EnableTransactionManagement
public class AppConfig {
    ...
    @Bean
    public PlatformTransactionManager transactionManager() {
        return new DataSourceTransactionManager(dataSource());
    }
}
cs

@Transactional 어노테이션 주요속성

속성 

타입 

 기본값

설명 

 value

String 

"" 

 

 propagation

Propagation 

Propagation.REQUIRED 

 

 isolation

Isolation 

Isolation.DEFAULT 

 

 timeout

int 

-1 

 








PropagationBehavior
PROPAGATION_REQUIREDSupport a current transaction, create a new one if none exists.This is the default setting. If an existing transaction is in progress, method will run within that transaction, else a new transaction will be started.
PROPAGATION_SUPPORTSSupport a current transaction, execute non -transactionally if none exists.
PROPAGATION_MANDATORYSupport a current transaction, throw an exception if none exists.
PROPAGATION_REQUIRES_NEWCreate a new transaction, suspend the current transaction if one exists
PROPAGATION_NOT_SUPPORTEDExecute non - transactionally, suspend the current transaction if one exists.
PROPAGATION_NEVERExecute non - transactionally, throw an exception if a transaction exists
PROPAGATION_NESTEDMethod should run within a nested transaction if an existing transaction exists.The nested transaction can be committed/roll backed independently of the enclosing transaction. If no enclosing transaction behave like PROPAGATION_REQUIRED.




Isolation LevelBehavior
DEFAULTUse the default isolation level of the underlying database.
READ_UNCOMMITTEDAllows a row changed by one transaction to be read by another transaction before any changes in that row have been committed. This means dirty readsnon-repeatable reads and phantom reads can occur. This is equivalent to Connection.TRANSACTION_READ_UNCOMMITTED. It is also the most efficient isolation level.
READ_COMMITTEDIt prohibits a transaction from reading a row with uncommitted changes in it. This prevents dirty reads, but non-repeatable reads and phantom reads can occur. This is equivalent to Connection.TRANSACTION_READ_COMMITTED.
REPEATABLE_READIt prohibits a transaction from reading a row with uncommitted changes in it, Dirty reads and non-repeatable reads are prevented; phantom reads can occur. This is equivalent to Connection.TRANSACTION_REPEATABLE_READ.
SERIALIZABLEIt prohibits dirty reads, non-repeatable reads and phantom reads. It prevent all problems but is the least efficient isolation level.