Notice
Recent Posts
Recent Comments
Link
- Arawn's Dev Blog
- Outsider's Dev Story
- Toby's Epril
- Benelog
- NHN 개발자 블로그
- SK 플래닛 기술 블로그
- OLC CENTER
- 소프트웨어 경영/공학 블로그
- 모바일 컨버전스
- KOSR - Korea Operating System …
- 넥스트리 블로그
- 리버스코어 ReverseCore
- SLiPP
- 개발자를 위하여... (Nextree 임병인 수석)
- "트위터 부트스트랩: 디자이너도 놀라워할 매끈하고 직관…
- Learning English - The English…
- real-english.com
- 'DataScience/Deep Learning' 카테…
- Deep Learning Summer School, M…
- Deep Learning Courses
민서네집
Transaction 설정 파일 본문
/WebContent/WEB-INF/spring/applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:beans="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd
http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:lang="http://www.springframework.org/schema/lang" xmlns:p="http://www.springframework.org/schema/p"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:util="http://www.springframework.org/schema/util">
<context:property-placeholder location="classpath*:*.properties" />
<context:component-scan base-package="com.examples.mgmt">
<context:exclude-filter expression="org.springframework.stereotype.Controller" type="annotation" />
</context:component-scan>
<!-- <tx:annotation-driven /> -->
<!-- @Transactional 어노테이션에 대한 트랜잭션 처리시 트랜잭션 매니저 txManager 빈으로 등록 -->
<tx:annotation-driven transaction-manager="txManager" />
<!--
트랜잭션의 propagation 설정에 대한 설명
예제(별도의 트랜잭션을 사용)
@Transactional(propagation=Propagation.REQUIRES_NEW)
public String buildCode(String type, String... args) {
...
}
- MADATORY
: 반드시 트랜잭션 내에서 메소드가 실행되야 함
: 트랜잭션이 없는 경우에는 예외 발생
- NESTED
: 트랜잭션에 있는 경우, 기존 트랜잭션 내의 nested transaction 형태로 메소드 실행
: nested transaction 자체적으로 commit, rollback 가능
: 트랜잭션이 없는 경우, PROPAGATION_REQUIRED 속성으로 행동
- NEVER
: 트랜잭션 컨텍스트가 없이 실행되야 함
: 트랜잭션이 있으면 예외 발생
- NOT_SUPPORTED
: 트랜잭션이 없이 메소드 실행
: 기존의 트랜잭션이 있는 경우에는 이 트랜잭션을 호출된 메소드가 끝날때까지 잠시 보류
: JTATransactionManager를 사용하는 경우에는, TransactionManager가 필요
- REQUIRED
: 트랜잭션 컨텍스트 내에서 메소드가 실행되야 함
: 기존 트랜잭션이 있는 경우, 기존 트랜잭션 내에서 실행
: 기존 트랜잭션이 없는 경우, 새로운 트랜잭션 생성
- REQUIRES_NEW
: 호출되는 메소드는 자신 만의 트랜잭션을 가지고 실행
: 기존의 트랜잭션들은 보류됨
: JTATransactionManager를 사용하는 경우에는, TransactionManager가 필요
- SUPPORTS
: 새로운 트랜잭션을 필요로 하지는 않지만, 기존의 트랜잭션이 있는 경우에는 트랜잭션 내에서 실행
-->
<!-- DataSource를 활용하는 트랜잭션 매니저를 등록 -->
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!-- dataSource 빈을 DataSourceTransactionManager 트랜잭션 매니저의 dataSource 속성에 지정 -->
<property name="dataSource" ref="dataSource" />
</bean>
<!-- AOP advisor 를 활용하여 트랜잭션을 처리함 -->
<tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
<!--
모든 메소드에 대해서 트랜잭션을 활용함
기본 propagation 은 REQUIRED 으로 지정됨
예외사항의 종류에 따라 롤백을 하지 않는 부분을 지정할 수도 있음
-->
<tx:method name="*" />
</tx:attributes>
</tx:advice>
<aop:config>
<!--
execution(modifiers-pattern? ret-type-pattern declaring-type-pattern? name-pattern(param-pattern) throws-pattern?)
위와 같은 패턴으로 아래의 경우에는
하위 패키지 명이 service.impl 이며 이름의 마지막이 ServiceImpl 로 시작되는 모든 클래스의 모든 메소드에
txAdvice 빈을 사용하여 트랜잭션을 사용하도록 한다.
-->
<aop:advisor pointcut="execution(* *..service.impl.*ServiceImpl.*(..))" advice-ref="txAdvice" />
</aop:config>
<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName" value="jdbc/exampleDataSource" />
<property name="resourceRef" value="true" />
</bean>
<!-- transaction manager, use JtaTransactionManager for global tx -->
<!-- <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean> -->
<!-- define the SqlSessionFactory -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="typeAliasesPackage" value="com.examples.mgmt" />
</bean>
<!-- scan for mappers and let them be autowired -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.examples.mgmt" />
<property name="annotationClass" value="org.springframework.stereotype.Repository"/>
</bean>
<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basenames">
<list>
<value>message</value>
<!-- <value>Messages</value> -->
</list>
</property>
</bean>
<bean id="msgSrcAccessor" class="org.springframework.context.support.MessageSourceAccessor">
<constructor-arg ref="messageSource" />
</bean>
<!-- Only needed because we require fileupload in the org.springframework.samples.mvc.fileupload package -->
<beans:bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver" />
</beans>
Default Transaction(REQUIRED) 말고, 다른 트랜잭션을 걸고 싶은 경우는 Service 메서드 위에
@Transactional(propagation = Propagation.REQUIRES_NEW)
라고 특정 Transaction 속성을 넣어준다.
그러면 Service 내의 모든 메서드는 @Transactional 어노테이션 없이도, 기본 트랜잭션(REQUIRED) 가 적용되고, 어노테이션이 붙은 서비스의 메서드의 경우는 그 어노테이션에 해당하는 트랜잭션이 적용된다.
'Spring' 카테고리의 다른 글
| [mybatis] mapper xml 파일을 서버 재시작 없이 reloading 하기. (0) | 2013.07.22 |
|---|---|
| [spring] 어느 곳에서나 request 를 얻기. (0) | 2013.07.09 |
| WebApplication 에서 Spring Bean 가져오기 (0) | 2013.05.06 |
| MyBatis 사용시 주의점 (0) | 2013.04.24 |
| Java Custom Annotation Example (0) | 2013.04.23 |
Comments