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




[TMS] $ /bin/sh -xe /tmp/jenkins2697039887893472079.sh + /home/jyeory/app/shell/afterBuildPf.sh rm: cannot remove '/home/jyeory/app/tomcat-instances/tomcatPf/webapps/ROOT': Permission denied cp: failed to access '/home/jyeory/app/tomcat-instances/tomcatPf/webapps/ROOT.war': Permission denied Build step 'Execute shell' marked build as failure

Jenkins에서 빌드 후 스크립트를 실행 할 때 오류가 난다.

권한이 문제인데, 우선 Jenkins를 실행하는 사용자는 jenkins이고, 배포 디렉토리의 사용자는 jyeory로 권한이 없을만 하다.


이를 해결하기 위해 jenkins, jyeory 모두 dev라는 그룹으로 묶어주고.... 

배포 디렉토리의 그룹도 dev로 변경, 마지막으로 배포 디렉토리의 그룹 권한을 rwx(7)로 바꿔주어야 한다.


이후 jenkins를 재시작하면 오류가 발생하지 않는다.


java.lang.AbstractMethodError: Method com/mchange/v2/c3p0/impl/NewProxyPreparedStatement.isClosed()Z is abstract

<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->
<dependency>
    <groupId>org.mybatis</groupId>
    <artifactId>mybatis</artifactId>
    <version>3.4.6</version>
</dependency>

<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis-spring -->
<dependency>
    <groupId>org.mybatis</groupId>
    <artifactId>mybatis-spring</artifactId>
    <version>1.3.2</version>
</dependency>

위 처럼 mybatis 3.4.6, mybatis-spring 버전을 1.3.2로 사용하고 테스트를 하니 위 에러가 나더라..


<!-- https://mvnrepository.com/artifact/com.mchange/c3p0 -->
<dependency>
    <groupId>com.mchange</groupId>
    <artifactId>c3p0</artifactId>
    <version>0.9.5.2</version>
</dependency>

c3p0 라이브러리 버전이 낮아서 그런거 이므로 최신 버전으로 바꾸어 사용하면 된다.


+ Recent posts