본문 바로가기

개발

[Spring] mysql+mybatis @transaction 설정

[spring] root-context.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"

xsi:schemaLocation="http://www.springframework.org/schema/beans 

http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> 

<!-- Root Context: defines shared resources visible to all other web components -->

<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">

<property name="locations">

<list>

<value>classpath:conf/db.properties</value>

</list>

</property>

</bean>

<!-- db setting -->

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">

   <property name="driverClassName" value="${db.driverClassName}"/>

   <property name="url" value="${db.url}"/>

   <property name="username" value="${db.username}"/>

   <property name="password" value="${db.password}"/>

  </bean>

 

  <bean id="sqlSessionFactory" class="net.rideus.app.common.RefreshableSqlSessionFactoryBean">

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

 <property name="mapperLocations" value="classpath:conf/**/*.xml" />

</bean>

<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">

 <constructor-arg index="0" ref="sqlSessionFactory" />

</bean>

 

  <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">

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

      </bean>

</beans>



[servlet] servlet-context.xml 설정에 아래 내용(빨간글씨) 추가.
    - tx에 에러표시가 뜰경우 녹색글씨 부분이 추가되었는지 확인


<?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-3.0.xsd

http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd

http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd

http://www.springframework.org/schema/tx

     http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"

xmlns:tx="http://www.springframework.org/schema/tx" >


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

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

<annotation-driven />

<tx: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>

<context:component-scan base-package="net.rideus.app" />

</beans:beans>



[사용방법]

contrller에서 service interface 호출

@RequestMapping(value="/joinProc.do")

public void webJoinProc(Model model, MemberVO params, HttpSession session) throws Exception{

int resultCnt = memberService.webJoinMeber(params);

}


service interface에서 @transactional 어노테이션 추가

public interface MemberService {

       @Transactional

public int webJoinMeber(MemberVO params) throws Exception;

}



[주의사항]

interface 구현부분에서 에러발생시 exception을 throw 시켜줘야 한다. (try catch로 처리 해버리면 rollback이 되질 않는다.)

public int webJoinMeber(MemberVO params) throws Exception {

MemberDao dao = sqlSession.getMapper(MemberDao.class);

if( dao.webJoinMember(params) == 0 ){

throw new Exception();

}

                 

                if(true){ // 테스트용 Exception throws

//if( dao.webInsertNickHistory(params) == 0 ){

throw new RuntimeException();

}

}


  • mysql일경우 table type이 innoDB 인지부터 확인한다. 아닐경우 transaction을 지원하지 않는다.