기존카테고리/Spring_실전1

스프링 TransactionTemplate을 사용한 트랜젝션 - 실전용

자연과벗하다 2017. 7. 16. 15:14



<?xml version="1.0" encoding="UTF-8"?>

<beans:beans xmlns="http://www.springframework.org/schema/mvc"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xmlns:beans="http://www.springframework.org/schema/beans"

xmlns:context="http://www.springframework.org/schema/context"

xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.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">


<!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->

<!-- Enables the Spring MVC @Controller programming model -->

<annotation-driven />


<!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->

<resources mapping="/resources/**" location="/resources/" />


<!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory -->

<beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">

<beans:property name="prefix" value="/WEB-INF/views/" />

<beans:property name="suffix" value=".jsp" />

</beans:bean>

<beans:bean name="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">

<beans:property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />

<beans:property name="url" value="jdbc:oracle:thin:@localhost:1521:XE"/>

<beans:property name="username" value="scott" />

<beans:property name="password" value="1234" />

</beans:bean>

<beans:bean name="template" class="org.springframework.jdbc.core.JdbcTemplate">

<beans:property name="dataSource" ref="dataSource" />

</beans:bean>

<beans:bean name="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">

<beans:property name="dataSource" ref="dataSource" />

</beans:bean>

<!-- 이 부분 추가 -->

<beans:bean name="transactionTemplate" class="org.springframework.transaction.support.TransactionTemplate">

<beans:property name="transactionManager" ref="transactionManager"/>

</beans:bean>

<context:component-scan base-package="com.test.transactionEx" />

</beans:beans>






package com.test.transactionEx;


import java.sql.Connection;

import java.sql.PreparedStatement;

import java.sql.SQLException;


import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.jdbc.core.JdbcTemplate;

import org.springframework.jdbc.core.PreparedStatementCreator;

import org.springframework.jdbc.core.PreparedStatementSetter;

import org.springframework.stereotype.Service;

import org.springframework.transaction.PlatformTransactionManager;

import org.springframework.transaction.TransactionDefinition;

import org.springframework.transaction.TransactionStatus;

import org.springframework.transaction.support.DefaultTransactionDefinition;

import org.springframework.transaction.support.TransactionCallbackWithoutResult;

import org.springframework.transaction.support.TransactionTemplate;


@Service

public class BuyDAO {

//@Autowired

JdbcTemplate template;

@Autowired

public void setTemplate(JdbcTemplate template){

this.template = template;

}

TransactionTemplate transactionTemplate;

@Autowired

public void setTransactionTemplate(TransactionTemplate transactionTemplate){

this.transactionTemplate = transactionTemplate;

}

public void buyTicket(final BuyVO buyVO){

String n = null;

System.out.println("buyTicket() 호출");

System.out.println("구매 아이디:"+buyVO.getUserId());

System.out.println("구매 수량: "+buyVO.getAmount());

transactionTemplate.execute(new TransactionCallbackWithoutResult() {

@Override

protected void doInTransactionWithoutResult(TransactionStatus arg) {

try{

template.update(new PreparedStatementCreator() {

@Override

public PreparedStatement createPreparedStatement(Connection conn) throws SQLException {

String sql = "insert into card(username, amount) values(?, ?)";

PreparedStatement pstmt = conn.prepareStatement(sql);

pstmt.setString(1, buyVO.getUserId());

pstmt.setInt(2, Integer.parseInt(buyVO.getAmount()));

return pstmt;

}

});

String sql = "insert into ticket(userName, cnt) values(?, ?)";

template.update(sql, new PreparedStatementSetter() {

@Override

public void setValues(PreparedStatement pstmt) throws SQLException {

pstmt.setString(1, buyVO.getUserId());

pstmt.setInt(2, Integer.parseInt(buyVO.getAmount()));

}

});


}catch(Exception e){

arg.setRollbackOnly();

System.out.println("롤백 처리완료");

}

} //doInTransactionWithoutResult

});

} //buyTicket

}