Spring - myBatis 환경에서 트랜잭션이 적용되지 않으면 아래 로그를 볼 수 있다.

Creating a new SqlSession
SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@1ad7d61e] was not registered for synchronization because synchronization is not active
Fetching JDBC Connection from DataSource
trace com.mchange.v2.resourcepool.BasicResourcePool@723b12e9 [managed: 20, unused: 19, excluded: 0] (e.g. com.mchange.v2.c3p0.impl.NewPooledConnection@6f25ab49)
JDBC Connection [com.mchange.v2.c3p0.impl.NewProxyConnection@5beadc25] will not be managed by Spring
==>  Preparing: UPDATE iot.user SET user_name = ?, nickname = ?, email = ?, is_admin = ?, update_date = NOW() WHERE seq = ?
==> Parameters: Test46(String), Test?46(String), 12@abc.com(String), 0(String), 346(Integer)
<==    Updates: 1
Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@1ad7d61e]
Returning JDBC Connection to DataSource


-- applicationContext-beans.xml
<!-- Annotation 기반의 Component Scan 필터(service package 만 검색) -->
<context:component-scan base-package="com.iot.pf">
      <context:include-filter type="regex" expression="\.*\.service\.*" />
</context:component-scan>

<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
       <!-- DB에 접속 하기 위해서 설정 -->
      <property name="dataSource" ref="dataSource" />   
      
      <!-- myBatis 기본 설정 -->
      <property name="configLocation" value="classpath:mybatis-configuration.xml" />
      
      <!-- query 적힌 xml 위치 -->
      <property name="mapperLocations" value="classpath:sql/SQL.*.xml" />
      <!-- 트랜잭션 관리를 위한 것 -->
      <property name="transactionFactory">
           <bean class="org.mybatis.spring.transaction.SpringManagedTransactionFactory" />
      </property>
</bean>

<!-- DAO(interface) 위치를 basePackage로.. -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
      <property name="basePackage" value="com.iot.pf.dao" />
</bean>

<!-- 트랜잭션 관리를 위한 bean  -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
      <property name="dataSource" ref="dataSource" />
</bean>

-- servlet.xml

<context:component-scan base-package="com.iot.pf" use-default-filters="false" >
      <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller" />
      <context:include-filter type="annotation" expression="org.springframework.stereotype.Component" />
</context:component-scan>

<!-- File Upload -->
      <property name="maxUploadSize" value="50000000" />
</bean>

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
      <property name="prefix" value="/WEB-INF/views/"/>
      <property name="suffix" value=".jsp"/>
</bean>

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

<mvc:annotation-driven />

<!-- transaction annotation -->
<tx:annotation-driven transaction-manager="transactionManager"/>

-- service file
@Service("userService")
@Transactional(rollbackFor = {Exception.class})
public class UserServiceImpl implements UserService{
 
}


트랜잭션이 적용되면 아래 로그를 볼 수 있다.
Creating new transaction with name [com.iot.pf.service.impl.UserServiceImpl.update]: PROPAGATION_REQUIRED,ISOLATION_DEFAULT; '',-com.iot.pf.exception.AnomalyException,-java.lang.Exception
trace com.mchange.v2.resourcepool.BasicResourcePool@63cfcf47 [managed: 20, unused: 19, excluded: 0] (e.g. com.mchange.v2.c3p0.impl.NewPooledConnection@3f24eec6)
Acquired Connection [com.mchange.v2.c3p0.impl.NewProxyConnection@19668759] for JDBC transaction
Switching JDBC Connection [com.mchange.v2.c3p0.impl.NewProxyConnection@19668759] to manual commit
Getting transaction for [com.iot.pf.service.impl.UserServiceImpl.update]
Creating a new SqlSession
Registering transaction synchronization for SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@5c640a11]
JDBC Connection [com.mchange.v2.c3p0.impl.NewProxyConnection@19668759] will be managed by Spring
==>  Preparing: UPDATE iot.user SET user_name = ?, nickname = ?, email = ?, is_admin = ?, update_date = NOW() WHERE seq = ?
==> Parameters: Test46(String), Test46(String), 123@abc.com(String), 0(String), 346(Integer)
<==    Updates: 1
Releasing transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@5c640a11]
Completing transaction for [com.iot.pf.service.impl.UserServiceImpl.update] after exception: com.iot.pf.exception.AnomalyException: You have an anomaly result value after [INSERT/UPDATE/DELETE] query!!!!
Applying rules to determine whether transaction should rollback on com.iot.pf.exception.AnomalyException: You have an anomaly result value after [INSERT/UPDATE/DELETE] query!!!!
Winning rollback rule is: RollbackRuleAttribute with pattern [com.iot.pf.exception.AnomalyException]
Transaction synchronization deregistering SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@5c640a11]
Transaction synchronization closing SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@5c640a11]
Initiating transaction rollback
Rolling back JDBC transaction on Connection [com.mchange.v2.c3p0.impl.NewProxyConnection@19668759]
Releasing JDBC Connection [com.mchange.v2.c3p0.impl.NewProxyConnection@19668759] after transaction
Returning JDBC Connection to DataSource




+ Recent posts