기록해! 정리해!

트랜잭션 처리 본문

Spring

트랜잭션 처리

zsuling 2022. 9. 22. 10:05

<?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:context="http://www.springframework.org/schema/context"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/context
 http://www.springframework.org/schema/context/spring-context-4.3.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">

<context:component-scan base-package="com.springbook.biz" />

<!--  
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
-->

<context:property-placeholder  location="classpath:config/database.properties" />
<bean id="dataSource"  class="org.apache.commons.dbcp.BasicDataSource"  destroy-method="close" >
 <property name="driverClassName"  value="${jdbc.driver}" />
 <property name="url"  value="${jdbc.url}" />
 <property name="username"  value="${jdbc.username}" />
 <property name="password"  value="${jdbc.password}" />
</bean>
 
<!-- Spring JDBC 설정 --> 
<bean id="jdbcTemplate"  class="org.springframework.jdbc.core.JdbcTemplate" >
  <property name="dataSource"  ref="dataSource" />
</bean>

<!--  트랙잭션 설정  -->
<bean id="txManager"  class="org.springframework.jdbc.datasource.DataSourceTransactionManager" >
  <property name="dataSource"  ref="dataSource" />
</bean>

<tx:advice  id="txAdvice"   transaction-manager="txManager">
  <tx:attributes>
    <tx:method name="get*" read-only="true" />
    <tx:method name="*"  />  
  </tx:attributes>

</tx:advice>

<aop:config>
  <aop:pointcut id="txPointcut" 
               expression="execution(* com.springbook.biz..*(..))" />
  <aop:advisor  pointcut-ref="txPointcut" advice-ref="txAdvice"/> 
</aop:config>

</beans>

실습 

 

1. seq 에 100 번 넣어주기

package com.springbook.biz.board;

import java.util.List;

import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.GenericXmlApplicationContext;

public class BoardInsertClient {

	public static void main(String[] args) {
		AbstractApplicationContext container=
				new GenericXmlApplicationContext("applicationContext.xml");

		BoardService service =(BoardService) container.getBean("boardService");
		BoardVo vo =new BoardVo();
		vo.setTitle("임시 제목1");
		vo.setWriter("홍길동1");
		vo.setContent("임시내용1 ....");		
		vo.setSeq(101);	
		service.insert(vo);
		
		
		List<BoardVo> li =service.getBoardList();
		for(BoardVo m :li) {
		  System.out.println("===> " +m.toString());	
		}
				
		container.close();
	}

}

 

2. 100 번 seq 받아서 저장하기


package com.springbook.biz.board.impl;


import java.util.List;
import javax.sql.DataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.support.JdbcDaoSupport;
import org.springframework.stereotype.Repository;
import com.springbook.biz.board.BoardDao;
import com.springbook.biz.board.BoardVo;

@Repository
public class BoardDaoSpring2  implements BoardDao {
	
	/*
	private final String BOARD_INSERT="insert into board(seq, title, writer, content)"
			+ " values((select nvl(max(seq),0)+1 from board), ?,?,?)";

    */
	private final String BOARD_INSERT="insert into board(seq, title, writer, content)"
			+ " values(? , ?,?,?)";

	
	private final String BOARD_SELECT=" select * from board order by seq  desc" ;

	private final String BOARD_EDIT="select seq, title, writer, content, regdate, cnt"
			+ " from board where seq = ? " ;

	private final String BOARD_DELETE=" delete from board where seq = ? " ;
	
	private final String BOARD_UPDATE="update board set title=?, writer=?, content=? where seq=?";


	
	// applicationContext.xml 에서  JdbcTemplate 형 받아오기
	@Autowired
    private  JdbcTemplate  jdbcTemplate;

	
	@Override
	public void insert(BoardVo vo) {
		Object[] args = {vo.getSeq(),vo.getTitle(), vo.getWriter(), vo.getContent()};
		jdbcTemplate.update(BOARD_INSERT, args );
	}

	@Override
	public void update(BoardVo vo) {
		Object[] args = {vo.getTitle(), vo.getWriter(), vo.getContent(),vo.getSeq() };
		jdbcTemplate.update(BOARD_UPDATE, args);
	}

	@Override
	public void delete(int seq) {
		jdbcTemplate.update(BOARD_DELETE, seq);
		
	}

	@Override
	public BoardVo getBoard(int seq) {		
		return jdbcTemplate.queryForObject(BOARD_EDIT, new BoardRowMapper(), seq);
	}

	@Override
	public List<BoardVo> getBoardList() {
		
		return  jdbcTemplate.query(BOARD_SELECT, new BoardRowMapper());

	}

}​
 
3. 서비스 에서 100번 2번 넣어주기

package com.springbook.biz.board.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.springbook.biz.board.BoardDao;
import com.springbook.biz.board.BoardService;
import com.springbook.biz.board.BoardVo;

@Service("boardService")
public class BoardServiceImpl  implements BoardService {

	@Autowired
	private BoardDao boardDao;
	
	
	@Override
	public void insert(BoardVo vo) {
	   
		boardDao.insert(vo);	
		boardDao.insert(vo);	

	}

	@Override
	public void update(BoardVo vo) {
	
		boardDao.update(vo);	

	}

	@Override
	public void delete(int seq) {

		boardDao.delete(seq);	

	}

	@Override
	public BoardVo getBoard(int seq) {	

		return boardDao.getBoard(seq);
	}

	@Override
	public List<BoardVo> getBoardList() {

		return boardDao.getBoardList();
	}

}​
 
4. 결과 : 값이 하나도 안들어가고 롤백 된다

'Spring' 카테고리의 다른 글

Model 2 : DispatcherServlet 만들기  (0) 2022.09.23
Model 1 아키텍처로 게시판 개발  (0) 2022.09.23
Spring JDBC 방법 2  (0) 2022.09.22
Spring JDBC 방법  (0) 2022.09.22
어노데이션 기본 AOP - Before 어드바이스  (0) 2022.09.22
Comments