관리 메뉴

ふたりで

springboot @Transactional not working... 본문

Spring

springboot @Transactional not working...

graykang 2021. 7. 27. 13:59
728x90
반응형
SMALL

먼저 @Transactional 어노테이션 사용을 위해 설정이 필요하다.

DataSource 설정 부분에 아래와 같이  PlatformTransactionManager 설정을 하고 @EnableTransactionManagement

어노테이션을 명시한다.

package com.graykang.config;

import javax.sql.DataSource;

import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;
//import org.springframework.context.annotation.Primary;
import org.springframework.transaction.annotation.EnableTransactionManagement;

@Configuration
@MapperScan(basePackages="com.graykang.common.mapper.b2b",sqlSessionFactoryRef="b2bSqlSessionFactory")/*멀티DB사용시 basePackages를 DB별로 따로설정*/
@EnableTransactionManagement
public class B2BDatabaseConfig {
	
	@Bean(name="b2bDataSource")
	@ConfigurationProperties(prefix="spring.b2b.datasource.hikari")
	public DataSource b2bDataSource() {
		return DataSourceBuilder.create().build();
	}
	
	@Bean(name="b2bSqlSessionFactory")
	public SqlSessionFactory sqlSessionFactory(@Qualifier("b2bDataSource") DataSource b2bDataSource, ApplicationContext applicationContext) throws Exception{
		final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
		sessionFactory.setDataSource(b2bDataSource);
		sessionFactory.setMapperLocations(applicationContext.getResources("classpath:mybatis/b2b_mapper/*.xml"));
		return sessionFactory.getObject();
	}
	
	@Bean(name="b2bSqlSessionTemplate")
	public SqlSessionTemplate sqlSessionTemplate(SqlSessionFactory b2bsqlSessionFactory) throws Exception{
		return new SqlSessionTemplate(b2bsqlSessionFactory);
	}
	
        @Bean(name = "b2btransactionManager")
        public PlatformTransactionManager b2btransactionManager(@Qualifier("b2bDataSource") DataSource b2bDataSource) {
            return new DataSourceTransactionManager(b2bDataSource);
        }
}

@EnableTransactionManagement 설정 후 트랜잭션이 필요한 부분에 @Transactional 만 붙여 주면 알아서 롤백이 될 줄 알았다.

테스트를 해보니 Exception이 발생했는데 rollback이 되지 않았다.

구글링 결과  @Transactional 적용할 부분에 rollbackFor={Exception.class}라는 걸 추가해 주어야 한다는 걸 알게 되어 

설정하고 테스트 해본걸 정리한다.

아래와 같이 @Transactional(value = "b2btransactionManager", rollbackFor={Exception.class}) 로 설정을 변경하고

throws Excrption 추가 및 try cath에서 Exception 발생 시 throw new Exception()으로 던져주니 롤백이 아주 잘 된다.

	@Transactional(value = "b2btransactionManager", rollbackFor={Exception.class})
	public String setGoodsDelete(SupplierVO Param) throws Exception {
		String resultStr = "";
		try {
			supMapper.insertMInfo(param);
			supMapper.deletePInfo(param);
			supMapper.deleteCDetail(param);
			resultStr = "sucess";		
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			resultStr = "fail";
			throw new Exception();//Exception에 던져주어야  rollback이 되더라는.
		}
		
		return resultStr;
	}

추후 시간 날 때 AOP가 적용된 Transaction방식으로 변경 테스트를 해봐야겠다.

728x90
반응형
LIST
Comments