씨네
공부하는 개발자 강씨네
씨네
  • 분류 전체보기 (460)
    • Web (21)
      • HTML (11)
      • CSS (10)
    • JS 관련 (49)
      • JavaScript (27)
      • JQuery (22)
    • TS 관련 (15)
      • TypeScript (15)
    • NodeJS (7)
      • NodeJS (7)
    • 따라하며 배우는 시리즈 (23)
      • NodeJS & ReactJS Basic (23)
      • NodeJS & ReactJS Movie (0)
      • NodeJS & ReactJS Youtube (0)
      • NodeJS & ReactJS ChatBot (0)
    • SPA (14)
      • React (14)
      • Vue (0)
      • Anguler (0)
    • Java 관련 (118)
      • Java (52)
      • JDBC (6)
      • JSP & Servlet (18)
      • Spring Legecy (38)
      • SpringBoot (4)
    • Python (26)
      • Python (20)
      • PyMongo (1)
      • Django (5)
    • Git (24)
      • Github (24)
    • RDB (22)
      • Oracle (21)
      • MySQL (1)
    • NoSQL (5)
      • MongoDB (5)
    • OS (4)
      • Linux (4)
    • 빅데이터 (2)
      • hadoop (2)
    • IDE (20)
      • eclipse (11)
      • VSCODE (4)
      • VisualStudio (1)
      • IntelliJ (1)
      • PyCharm (1)
      • DBeaver (2)
    • Install (3)
      • Tomcat (1)
      • Docker (1)
      • Anaconda (1)
    • 오류&에러 (28)
      • TS (2)
      • NodeJS (7)
      • SQL (8)
      • Java (1)
      • Spring (4)
      • Git (6)
      • 기타 (0)
    • 알고리즘 (67)
      • 수열 (1)
      • 백준(backjoon) (39)
      • Programmers (27)
    • 자격증 (5)
      • SQLD (5)
    • 기타 (2)
    • IT유튜브로 지식쌓기 (2)

공지사항

인기 글

최근 글

티스토리

250x250
hELLO · Designed By 정상우.
씨네

공부하는 개발자 강씨네

[Spring] transation(트랜잭션 - 원자성)
Java 관련/Spring Legecy

[Spring] transation(트랜잭션 - 원자성)

2022. 4. 7. 11:54
728x90

23. (MYBoard) dao, biz, controller

​

MYBoardDao.java(interface)에 추상메소드 추가

public String test();

MYBoardDaoImpl.java(class)에 메소드 추가

	@Override
	public String test() {
		return null;
	}

MYBoardBiz.java(interface)에 추상메소드 추가

public String test();

MYBoardBizImpl.java(class)에 메소드 추가

	@Transactional
	@Override
	public String test() {
		
		dao.insert(new MYBoardDto(0, "test", "transaction test", "transaction이 뭐였는지??", null));
		
		String test = dao.test();
		test.length();
		
		return test;
	}

biz에서 다오를 2개 호출합니다.

하나의 기능을 하기 위해 2개의 query문이 최소한의 작업단위를 묶어주기 위해서 하나처럼 동작합니다.

원자성을 만족하기위해 두개의 명령중 하나만 실패해도 실패해야합니다.

​

​

MYBoardController에 테스트를 위해

	@RequestMapping("/test.do")
	public String test() {
		logger.info("[Controller] : test.do");
		
		biz.test();
		
		return "redirect:list.do";
	}

를 추가해보겠습니다.

​

24. servlet-context.xml : tx

<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:beans="http://www.springframework.org/schema/beans"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd
		http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd
		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd">

	<!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->
	
	<!-- Enables the Spring MVC @Controller programming model -->
	<annotation-driven />

	<!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->
	<resources mapping="/resources/**" location="/resources/" />

	<!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory -->
	<beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<beans:property name="prefix" value="/WEB-INF/views/" />
		<beans:property name="suffix" value=".jsp" />
	</beans:bean>
	
	<context:component-scan base-package="com.mvc.upgrade" />
	
	
	<!-- interceptor -->
	<interceptors>
		<interceptor>
			<mapping path="/*"/>
			<beans:bean class="com.mvc.upgrade.common.interceptor.LoginInterceptor" />
		</interceptor>
	</interceptors>
	
	
	<!-- transaction -->
	<tx:annotation-driven/>
	
	
</beans:beans>

servlet-context.xml로 와서 Namespaces tx를 체크합니다.

그리고 Source로 와서

이거 2줄 작성해주세요.

​

25. applicationContext.xml : transactionManager

	<!-- transaction manager -->
	<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource" ref="dataSource" />
	</bean>

지금 트랜잭션을 걸어준 것은 오라클이 가지고 있는 트랜잭션 매니저가 동작하게 되어있는데 Spring이 관리하게 해주고 싶다면 applicationContext에 tracsation을 다시 만들어 주어야한다.

​

26. index.jsp

<a href="test.do">트랜잭션 테스트...</a>

test.do로 이동하는 버튼을 만들고 실행시켜서 클릭해보면

트랜잭션 때문에 500에러 뜨는게 정상입니다!

혹시 500에러가 안뜨고 흰화면이 뜬다면 LoginInterceptor에서 test.do 추가해주세요!(interceptor 포스팅 참고!)

728x90

'Java 관련 > Spring Legecy' 카테고리의 다른 글

[Spring] 파일 업로드 / 다운로드(file upload / download)  (0) 2022.04.09
[Spring] security(비밀번호 암호화)  (0) 2022.04.08
[Spring] Interceptor(인터셉터)  (0) 2022.04.06
[Spring] Login / Regist(로그인 / 회원가입)  (0) 2022.04.05
[Spring] 스프링MVC - AOP  (0) 2022.04.04
    'Java 관련/Spring Legecy' 카테고리의 다른 글
    • [Spring] 파일 업로드 / 다운로드(file upload / download)
    • [Spring] security(비밀번호 암호화)
    • [Spring] Interceptor(인터셉터)
    • [Spring] Login / Regist(로그인 / 회원가입)
    씨네
    씨네
    개발자 씨네가 공부하는 내용을 기록 겸 공유하는 블로그입니다!

    티스토리툴바