반응형
이번에는 @어노테이션으로 트랜잭션을 걸어보려고 한다.
[context-datasource.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"
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/jdbc
http://www.springframework.org/schema/jdbc/spring-jdbc-4.3.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">
<bean id="jdbcProp" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="classpath:/jdbc.properties" />
</bean>
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="org.mariadb.jdbc.Driver" />
<property name="url" value="" />
<property name="username" value="" />
<property name="password" value="" />
</bean>
<bean id="dataSourceLog" class="net.sf.log4jdbc.Log4jdbcProxyDataSource">
<constructor-arg ref="dataSource"/>
<property name="logFormatter">
<bean class="net.sf.log4jdbc.tools.Log4JdbcCustomFormatter">
<property name="loggingType" value="MULTI_LINE"/>
<property name="sqlPrefix" value="SQL:::"/>
</bean>
</property>
</bean>
<!-- transaction manager -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="datasource"></property>
</bean>
<tx:annotation-driven transaction-manager="transactionManager"/>
</beans>
어노테이션에 대한 설명 -> @transactional 설명
- readOnly의 기본값은 false
- 스프링의 트랜잭션은 Service 계층에서 사용하는 것이 바람직!
[BoardServiceImp.java]
package com.sts.hello.service;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.sts.hello.dao.BoardDAO;
import com.sts.hello.domain.BoardVO;
@Service(value="boardService")
@Transactional
public class BoardServiceImp implements BoardService {
@Autowired
private BoardDAO boardDAO;
@Override
@Transactional(rollbackFor=Exception.class)
public void delete(Integer b_no) throws Exception {
// TODO Auto-generated method stub
boardDAO.delete(b_no);
throw new Exception("강제오류!!");
}
}
반응형
LIST
'Spring Framework > spring' 카테고리의 다른 글
스프링 tiles(타일즈) 설정하기 (0) | 2019.08.12 |
---|---|
스프링 MVC 설정으로 게시판 만들기 Part_2 (0) | 2019.08.09 |
스프링 MVC 설정으로 게시판 만들기 Part_1 (0) | 2019.08.09 |
스프링 프로젝트 - 트랜잭션 설정 Part_1 (0) | 2019.08.08 |
처음 시작하는 스프링 프로젝트 (0) | 2019.08.08 |