[스프링프레임워크교육스프링3트랜잭션강좌/예제]트랜잭션어드바이스(tx:advice)를통한선언적인방식트랜잭션(XML설정방식),spring
framework3.X transaction, 서울 구로디지털단지 스프링교육학원
Spring2.0 이상에서 tx 스키마에
정의된 <ta:advice> 엘리먼트에 의해
Transaction의 어드바이스를 쉽게 구현할 수 있다.
tx 네임스페이스에서 tx:advice가 가장
중요한데 사용 예는 다음과 같다. 또한 스프링 AOP가
프록시 기반이므로 트랜잭션이 적용될 메소드는 public으로 선언되어야 한다.
<tx:advice id=“txAdvice” transaction-manager=“transactionManager”>
<tx:attributes>
<tx:method
name=“*dao”
propagation=“REQUIRED”/>
<tx:method
name=“*”
propagation=“REQUIRED”read-only=“true”/>
</tx:attributes>
</tx:advice>
<bean id=“transactionManager” class=“org.springframework.jdbc.datasource.DataSourceTransactionManager”>
<property
name=“dataSource”
ref=“dataSource”/>
</bean>
<tx:advice>tx:attribute에 사용되는 속성
name : 적용될 어드바이스에 대응되는 메소드명(필수), 와일드카드
(*)가능
isolation : 트랜잭션 격리 수준
no-rollback-for : 예외가 발생했을때 이를 무시하고 Transaction을 롤백하지 않고 계속 진행할 예외를 지정
propagation : Transaction 전파방식 지정, 기본값:REQUIRED
read-only : 트랜잭션이 읽기 전용인지,
기본값:FALSE
rollback-for : 예외가 발생했을 때 트랜잭션을 롤백 시킬 예외들의 구체적인
타입을 콤마로 구분하여 기술
timeout : 트랜잭션 타임아웃을 지정
<tx:advice> 자체만 보았을 때는 어느 빈에 advice가 적용되어야 하는지 알수 없다. 그러므로 PointCut이 필요하다.( AOP의 Advice에 지나지 않는다.
)
<aop:config>
<aop:advisor
pointcut=“execution(*
*..SpringBoardDAO.*dao(..))”
advice-ref=“txAdvice”/>
</aop:config>
[예제를 통해 스프링 선언적 트랜잭션에 대해 이해하자.]
1. Spring Maven Project 생성
(springtr1 이라는 이름으로)
전체 이클립스 프로젝트 구성은 다음과 같다.
:namespace prefix = v ns = "urn:schemas-microsoft-com:vml"
/>
<그림, 스프링 선언적트랜잭션예제
이클립스화면>
[pom.xml]
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.springframework.samples</groupId>
<artifactId>springtr1</artifactId>
<version>0.0.1-SNAPSHOT</version>
<properties>
<!-- Generic properties -->
<java.version>1.6</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<!-- Spring -->
<spring-framework.version>3.2.3.RELEASE</spring-framework.version>
<!-- Logging -->
<logback.version>1.0.13</logback.version>
<slf4j.version>1.7.5</slf4j.version>
<!-- Test -->
<junit.version>4.11</junit.version>
</properties>
<dependencies>
<!-- Spring and Transactions -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring-framework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${spring-framework.version}</version>
</dependency>
<!-- Logging with SLF4J & LogBack -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${slf4j.version}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>${logback.version}</version>
<scope>runtime</scope>
</dependency>
<!-- Test Artifacts -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.5.4</version>
</dependency>
<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
<version>1.4</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>3.2.3.RELEASE</version>
</dependency>
<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc6</artifactId>
<version>11.1.0.7.0</version>
</dependency>
</dependencies>
<repositories>
<repository>
<id>oracle</id>
<name>ORACLE JDBC Repository</name>
<url>http://maven.jahia.org/maven2</url>
</repository>
</repositories>
</project>
2. Emp.java
package onj.edu.tr1.model;
public class Emp {
int empno;
String ename;
int deptno;
public Emp(int empno, String ename, int deptno) {
this.empno = empno;
this.ename = ename;
this.deptno = deptno;
}
public int getEmpno() {
return empno;
}
public void setEmpno(int empno) {
this.empno = empno;
}
public String getEname() {
return ename;
}
public void setEname(String ename) {
this.ename = ename;
}
public int getDeptno() {
return deptno;
}
public void setDeptno(int deptno) {
this.deptno = deptno;
}
}
3. OnjDao.java
package onj.edu.tr1.dao;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import javax.sql.DataSource;
import onj.edu.tr1.model.Emp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;
//JdbcDaoSupport클래스는
간단한 DataSource를
지원하기
위해 JdbcTemplate 클래스를
지원.
//jdbcTemplate은 DB연결, PrepatedStatement객체생성, 파라미터주입, SQL문장실행,
//DB접속종료등을
전담하며 getJdbcTemplate()을
통해
얻는다.
@Repository("onjDao")
public class OnjDao{
private JdbcTemplate jdbaTemplate;
@Autowired
public void setDataSource(DataSource dataSource){
this.jdbaTemplate = new JdbcTemplate(dataSource);
}
public List<Emp> getEmpByDeptno(int deptno) {
String sql =
"select empno, ename, deptno from emp where deptno = ?";
List<Emp>
list = jdbaTemplate.query(
sql,
new Object[] { deptno },
new RowMapper() {
public Object mapRow(ResultSet rs, int index) throws SQLException {
int empno = rs.getInt("empno");
String
ename = rs.getString("ename");
int deptno =
rs.getInt("deptno");
return new Emp(empno, ename, deptno);
}
}
);
return list;
}
public void insertEmps() throws Exception {
String sql =
"insert into emp(empno, ename, deptno) values (?, ?, ?)";
jdbaTemplate.update(sql, new Object[] { 8771, "Onj1" , 10});
jdbaTemplate.update(sql, new Object[] { 8772, "Onj2", 10 });
jdbaTemplate.update(sql, new Object[] { 8773, "Onj3", 10 });
//같은
데이터를
넣으면
키값이
중복되어 DataAccessException 발생(런타임예외)
//jdbaTemplate.update(sql, new Object[] { 333, "Onj3", 10
});
//아래
주석을
풀면 Exception이
발생하므로
트랜잭션은
롤백
//onjoraclejava.xml에서
rollback-for="Exception"으로
정의했기
때문
//만약
정의하지
않았다면
주석을
풀더라도
트랜잭션은
롤백안됨
//throw new Exception();
}
}
4. OnjService.java
package onj.edu.tr1.service;
import java.util.List;
import onj.edu.tr1.dao.OnjDao;
import onj.edu.tr1.model.Emp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service("onjService")
public class OnjService {
@Autowired
private OnjDao dao;
public OnjService() {}
public OnjService(OnjDao onjDao) {
this.dao = onjDao;
}
public List<Emp> getEmpByDeptno(int deptno) {
return dao.getEmpByDeptno(deptno);
}
public void insertEmps() throws Exception{
dao.insertEmps();
}
}
5. onjoraclejava.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:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">
<!-- 접속할 DB의 DataSource정의 -->
<bean name="dataSource"
class="org.apache.commons.dbcp.BasicDataSource"
p:driverClassName ="oracle.jdbc.driver.OracleDriver"
p:url
= "jdbc:oracle:thin:@192.168.0.7:1521:onj"
p:username =
"scott"
p:password =
"tiger" />
<!-- 트랜잭션매니저
정의 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<!-- 트랜잭션용 Advice 구성 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<!-- 예외
중에서 Exception이
발생한
경우에도
롤백이
실행된다는
의미 -->
<!-- 스프링은
트랜잭션
설정이
된
메소드에서 RuntimeException이
발생한
경우와
Errors가
발생한
경우에만
기본적으로
롤백을
수행하며
그
외의
예외들은
아래와같이직접
설정한
경우에만
롤백을
한다. -->
<tx:method name="get*"
read-only="true"/>
<tx:method name="*"
propagation="REQUIRED" rollback-for="Exception"/>
</tx:attributes>
</tx:advice>
<!-- aop 네임스페이스를
이용한
트랙잭션이
적용될
매소드(Pointcut), 충고자(Advisor)정의 -->
<aop:config>
<aop:pointcut id="onjPointCut" __EXPRESSION__="execution(*
onj.edu.tr1.dao.*Dao.insert*(..))"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="onjPointCut" />
</aop:config>
<context:component-scan base-package="onj.edu.tr1.dao"/>
<context:component-scan base-package="onj.edu.tr1.service"/>
<context:component-scan base-package="onj.edu.tr1.service"/>
</beans>
6. 테스트를 위한 client
program
package onj.edu.tr1.client;
import java.util.List;
import onj.edu.tr1.model.Emp;
import onj.edu.tr1.service.OnjService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class OnjEmpTest {
public static void main(String[] args) throws Exception{
ApplicationContext
ctx = new ClassPathXmlApplicationContext("onjoraclejava.xml");
OnjService
onjService = (OnjService)ctx.getBean("onjService");
List<Emp>
list = onjService.getEmpByDeptno(10);
display(list);
onjService.insertEmps();
list =
onjService.getEmpByDeptno(10);
display(list);
}
public static void display(List<Emp> list) {
for(Object e: list) {
System.out.print(((Emp)e).getEmpno());
System.out.print(((Emp)e).getEname());
System.out.println(((Emp)e).getDeptno());
}
}
}
7. 결과…. 일부러 중복된 데이터를
입력했는데 정상적으로 롤백 되었다.
3월 02, 2014 3:51:35 오후 org.springframework.context.support.AbstractApplicationContext
prepareRefresh
정보: Refreshing
org.springframework.context.support.ClassPathXmlApplicationContext@830122:
startup date [Sun Mar 02 15:51:35 KST 2014]; root of context
hierarchy
3월 02, 2014 3:51:35 오후 org.springframework.beans.factory.xml.XmlBeanDefinitionReader
loadBeanDefinitions
정보: Loading XML bean definitions from class path resource
[onjoraclejava.xml]
3월 02, 2014 3:51:36 오후 org.springframework.beans.factory.support.DefaultListableBeanFactory
preInstantiateSingletons
정보: Pre-instantiating singletons in
org.springframework.beans.factory.support.DefaultListableBeanFactory@960f0e:
defining beans
[dataSource,transactionManager,txAdvice,org.springframework.aop.config.internalAutoProxyCreator,onjPointCut,org.springframework.aop.support.DefaultBeanFactoryPointcutAdvisor#0,onjDao,org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,onjService,org.springframework.context.annotation.ConfigurationClassPostProcessor.importAwareProcessor];
root of factory hierarchy
7782CLARK10
7839KING10
7934MILLER10
3월 02, 2014 3:51:37 오후 org.springframework.beans.factory.xml.XmlBeanDefinitionReader
loadBeanDefinitions
정보: Loading XML bean definitions from class path resource
[org/springframework/jdbc/support/sql-error-codes.xml]
3월 02, 2014 3:51:37 오후 org.springframework.jdbc.support.SQLErrorCodesFactory
<init>
정보: SQLErrorCodes loaded: [DB2, Derby, H2, HSQL, Informix, MS-SQL,
MySQL, Oracle, PostgreSQL, Sybase]
Exception in thread "main" org.springframework.dao.DuplicateKeyException: PreparedStatementCallback; SQL [insert into emp(empno, ename,
deptno) values (?, ?, ?)]; ORA-00001: 무결성
제약
조건(SCOTT.PK_EMP)에
위배됩니다
; nested exception is java.sql.SQLIntegrityConstraintViolationException: ORA-00001: 무결성
제약
조건(SCOTT.PK_EMP)에
위배됩니다
at
org.springframework.jdbc.support.SQLErrorCodeSQLExceptionTranslator.doTranslate(SQLErrorCodeSQLExceptionTranslator.java:245)
at
org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:72)
at
org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:605)
at
org.springframework.jdbc.core.JdbcTemplate.update(JdbcTemplate.java:818)
at
org.springframework.jdbc.core.JdbcTemplate.update(JdbcTemplate.java:874)
at
org.springframework.jdbc.core.JdbcTemplate.update(JdbcTemplate.java:882)
at
onj.edu.tr1.dao.OnjDao.insertEmps(OnjDao.java:57)
at
onj.edu.tr1.dao.OnjDao$$FastClassByCGLIB$$64f4b819.invoke(<generated>)
at
org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204)
at
org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:698)
at
org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150)
at
org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:96)
at
org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:260)
at
org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:94)
at
org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
at
org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:91)
at
org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
at
org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:631)
at
onj.edu.tr1.dao.OnjDao$$EnhancerByCGLIB$$783e0e20.insertEmps(<generated>)
at
onj.edu.tr1.service.OnjService.insertEmps(OnjService.java:26)
at
onj.edu.tr1.client.OnjEmpTest.main(OnjEmpTest.java:20)
Caused by: java.sql.SQLIntegrityConstraintViolationException: ORA-00001: 무결성
제약
조건(SCOTT.PK_EMP)에
위배됩니다
at
oracle.jdbc.driver.SQLStateMapping.newSQLException(SQLStateMapping.java:85)
at
oracle.jdbc.driver.DatabaseError.newSQLException(DatabaseError.java:133)
at
oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:206)
at
oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:455)
at
oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:413)
at
oracle.jdbc.driver.T4C8Oall.receive(T4C8Oall.java:1034)
at
oracle.jdbc.driver.T4CPreparedStatement.doOall8(T4CPreparedStatement.java:194)
at
oracle.jdbc.driver.T4CPreparedStatement.executeForRows(T4CPreparedStatement.java:953)
at
oracle.jdbc.driver.OracleStatement.doExecuteWithTimeout(OracleStatement.java:1222)
at
oracle.jdbc.driver.OraclePreparedStatement.executeInternal(OraclePreparedStatement.java:3387)
at
oracle.jdbc.driver.OraclePreparedStatement.executeUpdate(OraclePreparedStatement.java:3468)
at
oracle.jdbc.driver.OraclePreparedStatementWrapper.executeUpdate(OraclePreparedStatementWrapper.java:1350)
at
org.apache.commons.dbcp.DelegatingPreparedStatement.executeUpdate(DelegatingPreparedStatement.java:105)
at
org.apache.commons.dbcp.DelegatingPreparedStatement.executeUpdate(DelegatingPreparedStatement.java:105)
at
org.springframework.jdbc.core.JdbcTemplate$2.doInPreparedStatement(JdbcTemplate.java:824)
at
org.springframework.jdbc.core.JdbcTemplate$2.doInPreparedStatement(JdbcTemplate.java:818)
at
org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:589)
... 18 more
8. EMP TABLE SELECT 결과
SQL> select empno,
ename, sal from emp where deptno=10;
EMPNO
ENAME
SAL
---------- ---------- ----------
7782
CLARK
2450
7839
KING
5000
7934
MILLER
1300
9. 이번에는 OnjDao.java에서
중복된 데이터 입력하는 부분을 주석으로 막고 실행해보자.
[OnjDao.java]
package onj.edu.tr1.dao;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import javax.sql.DataSource;
import onj.edu.tr1.model.Emp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;
//JdbcDaoSupport클래스는
간단한 DataSource를
지원하기
위해 JdbcTemplate 클래스를
지원.
//jdbcTemplate은 DB연결, PrepatedStatement객체생성, 파라미터주입, SQL문장실행,
//DB접속종료등을
전담하며 getJdbcTemplate()을
통해
얻는다.
@Repository("onjDao")
public class OnjDao{
private JdbcTemplate jdbaTemplate;
@Autowired
public void setDataSource(DataSource dataSource){
this.jdbaTemplate = new JdbcTemplate(dataSource);
}
public List<Emp> getEmpByDeptno(int deptno) {
String sql =
"select empno, ename, deptno from emp where deptno = ?";
List<Emp>
list = jdbaTemplate.query(
sql,
new Object[] { deptno },
new RowMapper() {
public Object mapRow(ResultSet rs, int index) throws SQLException {
int empno = rs.getInt("empno");
String
ename = rs.getString("ename");
int deptno =
rs.getInt("deptno");
return new Emp(empno, ename, deptno);
}
}
);
return list;
}
public void insertEmps() throws Exception {
String sql =
"insert into emp(empno, ename, deptno) values (?, ?, ?)";
jdbaTemplate.update(sql, new Object[] { 111, "Onj1" , 10});
jdbaTemplate.update(sql, new Object[] { 222, "Onj2", 10 });
jdbaTemplate.update(sql, new Object[] { 333, "Onj3", 10 });
//같은
데이터를
넣으면
키값이
중복되어 DataAccessException 발생(런타임예외)
//jdbaTemplate.update(sql, new Object[] { 333, "Onj3", 10
});
//throw new Exception();
}
}
[OnjEmpTest.java 실행 결과]
3월 02, 2014 4:02:52 오후 org.springframework.context.support.AbstractApplicationContext
prepareRefresh
정보: Refreshing
org.springframework.context.support.ClassPathXmlApplicationContext@bf7916:
startup date [Sun Mar 02 16:02:52 KST 2014]; root of context
hierarchy
3월 02, 2014 4:02:52 오후 org.springframework.beans.factory.xml.XmlBeanDefinitionReader
loadBeanDefinitions
정보: Loading XML bean definitions from class path resource
[onjoraclejava.xml]
3월 02, 2014 4:02:52 오후 org.springframework.beans.factory.support.DefaultListableBeanFactory
preInstantiateSingletons
정보: Pre-instantiating singletons in
org.springframework.beans.factory.support.DefaultListableBeanFactory@466c30:
defining beans
[dataSource,transactionManager,txAdvice,org.springframework.aop.config.internalAutoProxyCreator,onjPointCut,org.springframework.aop.support.DefaultBeanFactoryPointcutAdvisor#0,onjDao,org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,onjService,org.springframework.context.annotation.ConfigurationClassPostProcessor.importAwareProcessor];
root of factory hierarchy
7782CLARK10
7839KING10
7934MILLER10
7782CLARK10
7839KING10
7934MILLER10
111Onj110
222Onj210
333Onj310
[ORACLE EMP 테이블 실행결과]
SQL> select empno, ename, deptno from emp where deptno =
10;
EMPNO
ENAME
DEPTNO
---------- ---------- ----------
7782
CLARK
10
7839
KING
10
7934
MILLER
10
111
Onj1
10
222
Onj2
10
333
Onj3
10
[OnjDao.java에서 //throw
new Exception(); 부분 주석을 출고 테스트]
-
onjoraclejava.xml에서 <tx:method name="*"
propagation="REQUIRED" rollback-for="Exception"/>인
경우
트랜잭션은 롤백된다.
- onjoraclejava.xml에서 <tx:method name="*" propagation="REQUIRED“/>인
경우
트앤잭션은 롤백되지 않는다. 정상
commit된다.
댓글 없음:
댓글 쓰기